[d14176d] | 1 | package project.fmo.app.projcetfmo.config;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.context.annotation.Configuration;
|
---|
| 4 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
| 5 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
---|
| 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
| 9 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 10 |
|
---|
| 11 | @Configuration
|
---|
| 12 | @EnableWebSecurity
|
---|
| 13 | @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
---|
| 14 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | private final PasswordEncoder passwordEncoder;
|
---|
| 18 | private final CustomUsernamePasswordAuthenticationProvider authenticationProvider;
|
---|
| 19 |
|
---|
| 20 | public WebSecurityConfig(PasswordEncoder passwordEncoder,
|
---|
| 21 | CustomUsernamePasswordAuthenticationProvider authenticationProvider) {
|
---|
| 22 | this.passwordEncoder = passwordEncoder;
|
---|
| 23 | this.authenticationProvider = authenticationProvider;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | @Override
|
---|
| 27 | protected void configure(HttpSecurity http) throws Exception {
|
---|
| 28 |
|
---|
| 29 | http.csrf().disable()
|
---|
| 30 | .authorizeRequests()
|
---|
| 31 | .antMatchers("/","/**", "/home", "/register", "/products").permitAll()
|
---|
| 32 | .antMatchers("/admin/**").hasRole("ADMIN")
|
---|
| 33 | .anyRequest()
|
---|
| 34 | .authenticated()
|
---|
| 35 | .and()
|
---|
| 36 | .formLogin()
|
---|
| 37 | .permitAll()
|
---|
| 38 | .failureUrl("/login?error=BadCredentials")
|
---|
| 39 | .defaultSuccessUrl("/products", true)
|
---|
| 40 | .and()
|
---|
| 41 | .logout()
|
---|
| 42 | .clearAuthentication(true)
|
---|
| 43 | .invalidateHttpSession(true)
|
---|
| 44 | .deleteCookies("JSESSIONID")
|
---|
| 45 | .logoutSuccessUrl("/home")
|
---|
| 46 | .and()
|
---|
| 47 | .exceptionHandling().accessDeniedPage("/access_denied");
|
---|
| 48 |
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | protected void configure(AuthenticationManagerBuilder auth) {
|
---|
| 53 | auth.authenticationProvider(authenticationProvider);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | } |
---|