-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathSecurityConfiguration.java
More file actions
108 lines (92 loc) · 4.56 KB
/
SecurityConfiguration.java
File metadata and controls
108 lines (92 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.jtspringproject.JtSpringProject.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.jtspringproject.JtSpringProject.models.User;
import com.jtspringproject.JtSpringProject.services.UserService;
@Configuration
public class SecurityConfiguration {
UserService UserService;
public SecurityConfiguration(UserService UserService) {
this.UserService = UserService;
}
@Configuration
@Order(1)
public static class AdminConfigurationAdapter{
@Bean
SecurityFilterChain adminFilterChain(HttpSecurity http) throws Exception {
http.antMatcher("/admin/**")
.authorizeHttpRequests(requests -> requests
.requestMatchers(new AntPathRequestMatcher("/admin/login")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/setprice")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/**")).hasRole("ADMIN")
)
.formLogin(login -> login
.loginPage("/admin/login")
.loginProcessingUrl("/admin/loginvalidate")
.successHandler((request, response, authentication) -> {
response.sendRedirect("/admin/"); // Redirect on success
})
.failureHandler((request, response, exception) -> {
response.sendRedirect("/admin/login?error=true"); // Redirect on failure
}))
.logout(logout -> logout.logoutUrl("/admin/logout")
.logoutSuccessUrl("/admin/login")
.deleteCookies("JSESSIONID"))
.exceptionHandling(exception -> exception
.accessDeniedPage("/403") // Custom 403 page
);
http.csrf(csrf -> csrf.disable());
return http.build();
}
}
@Configuration
@Order(2)
public static class UserConfigurationAdapter{
@Bean
SecurityFilterChain userFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(requests -> requests
.antMatchers("/login", "/register", "/newuserregister" ,"/test", "/test2","/products","/allusers","/searchproducts","/logs","/Placeorder","/addtocart","/getallcarts","/newuser","/allorders","/Discounts").permitAll()
.antMatchers("/**").hasRole("USER"))
.formLogin(login -> login
.loginPage("/login")
.loginProcessingUrl("/userloginvalidate")
.successHandler((request, response, authentication) -> {
response.sendRedirect("/"); // Redirect on success
})
.failureHandler((request, response, exception) -> {
response.sendRedirect("/login?error=true"); // Redirect on failure
}))
.logout(logout -> logout.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.deleteCookies("JSESSIONID"))
.exceptionHandling(exception -> exception
.accessDeniedPage("/403") // Custom 403 page
);
http.csrf(csrf -> csrf.disable());
return http.build();
}
}
@Bean
UserDetailsService userDetailsService() {
return username -> {
User user = UserService.getUserByUsername(username);
if(user == null) {
throw new UsernameNotFoundException("User with username " + username + " not found.");
}
String role = user.getRole().equals("ROLE_ADMIN") ? "ADMIN":"USER";
return org.springframework.security.core.userdetails.User
.withUsername(username)
.password(user.getPassword())
.roles(role)
.build();
};
}
}