|
4 | 4 | import org.springframework.context.annotation.Bean; |
5 | 5 | import org.springframework.context.annotation.ComponentScan; |
6 | 6 | import org.springframework.context.annotation.Configuration; |
7 | | -import org.springframework.context.annotation.ImportResource; |
| 7 | +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; |
| 8 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 9 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 10 | +import org.springframework.security.config.annotation.web.builders.WebSecurity; |
| 11 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 12 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
8 | 13 | import org.springframework.security.core.userdetails.UserDetailsService; |
9 | 14 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
10 | 15 | import org.springframework.security.crypto.password.PasswordEncoder; |
11 | | -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; |
| 16 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
12 | 17 |
|
13 | 18 | @Configuration |
14 | 19 | @ComponentScan(basePackages = { "org.baeldung.security" }) |
15 | | -@ImportResource({ "classpath:webSecurityConfig.xml" }) |
16 | | -public class SecSecurityConfig { |
| 20 | +@EnableWebSecurity |
| 21 | +public class SecSecurityConfig extends WebSecurityConfigurerAdapter { |
17 | 22 |
|
18 | 23 | @Autowired |
19 | 24 | private UserDetailsService userDetailsService; |
20 | 25 |
|
| 26 | + @Autowired |
| 27 | + private AuthenticationSuccessHandler myAuthenticationSuccessHandler; |
| 28 | + |
21 | 29 | public SecSecurityConfig() { |
22 | 30 | super(); |
23 | 31 | } |
24 | 32 |
|
| 33 | + @Override |
| 34 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 35 | + auth.authenticationProvider(authProvider()); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void configure(WebSecurity web) throws Exception { |
| 40 | + web.ignoring().antMatchers("/resources/**"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void configure(HttpSecurity http) throws Exception { |
| 45 | + // @formatter:off |
| 46 | + http |
| 47 | + .csrf().disable() |
| 48 | + .authorizeRequests() |
| 49 | + .antMatchers("/j_spring_security_check*","/login*", "/logout*", "/signin/**", "/signup/**", |
| 50 | + "/user/registration*", "/regitrationConfirm*", "/expiredAccount*", "/registration*", |
| 51 | + "/badUser*", "/user/resendRegistrationToken*" ,"/forgetPassword*", "/user/resetPassword*", |
| 52 | + "/user/changePassword*", "/emailError*", "/resources/**").permitAll() |
| 53 | + .antMatchers("/invalidSession*").anonymous() |
| 54 | + .anyRequest().authenticated() |
| 55 | + .and() |
| 56 | + .formLogin() |
| 57 | + .loginPage("/login.html") |
| 58 | + .loginProcessingUrl("/j_spring_security_check") |
| 59 | + .defaultSuccessUrl("/homepage.html") |
| 60 | + .failureUrl("/login.html?error=true") |
| 61 | + .successHandler(myAuthenticationSuccessHandler) |
| 62 | + .usernameParameter("j_username") |
| 63 | + .passwordParameter("j_password") |
| 64 | + .permitAll() |
| 65 | + .and() |
| 66 | + .sessionManagement() |
| 67 | + .invalidSessionUrl("/invalidSession.html") |
| 68 | + .sessionFixation().none() |
| 69 | + .and() |
| 70 | + .logout() |
| 71 | + .invalidateHttpSession(false) |
| 72 | + .logoutUrl("/j_spring_security_logout") |
| 73 | + .logoutSuccessUrl("/logout.html?logSucc=true") |
| 74 | + .deleteCookies("JSESSIONID") |
| 75 | + .permitAll(); |
| 76 | + // @formatter:on |
| 77 | + } |
| 78 | + |
25 | 79 | // beans |
26 | 80 |
|
27 | 81 | @Bean |
|
0 commit comments