|
1 | 1 | package org.baeldung.spring; |
2 | 2 |
|
| 3 | +import org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.context.annotation.Bean; |
3 | 6 | import org.springframework.context.annotation.Configuration; |
4 | | -import org.springframework.context.annotation.ImportResource; |
| 7 | +import org.springframework.security.authentication.AuthenticationManager; |
| 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.configuration.EnableWebSecurity; |
| 11 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 12 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
5 | 13 |
|
6 | 14 | @Configuration |
7 | | -@ImportResource({ "classpath:webSecurityConfig.xml" }) |
8 | | -public class SecSecurityConfig { |
| 15 | +//@ImportResource({ "classpath:webSecurityConfig.xml" }) |
| 16 | +@EnableWebSecurity |
| 17 | +public class SecSecurityConfig extends WebSecurityConfigurerAdapter { |
9 | 18 |
|
10 | | - |
11 | | - |
12 | 19 | public SecSecurityConfig() { |
13 | 20 | super(); |
14 | 21 | } |
15 | 22 |
|
| 23 | + @Bean("authenticationManager") |
| 24 | + @Override |
| 25 | + public AuthenticationManager authenticationManagerBean() throws Exception { |
| 26 | + return super.authenticationManagerBean(); |
| 27 | + } |
| 28 | + |
| 29 | + @Autowired |
| 30 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 31 | + // @formatter:off |
| 32 | + auth.inMemoryAuthentication() |
| 33 | + .withUser("user1").password("{noop}user1Pass").roles("USER") |
| 34 | + .and() |
| 35 | + .withUser("admin1").password("{noop}admin1Pass").roles("ADMIN"); |
| 36 | + // @formatter:on |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void configure(final HttpSecurity http) throws Exception { |
| 41 | + // @formatter:off |
| 42 | + http.authorizeRequests() |
| 43 | + .antMatchers("/anonymous*").anonymous() |
| 44 | + .antMatchers("/login*").permitAll() |
| 45 | + .anyRequest().authenticated() |
| 46 | + |
| 47 | + .and() |
| 48 | + .formLogin() |
| 49 | + .loginPage("/login.html") |
| 50 | + .loginProcessingUrl("/login") |
| 51 | + .successHandler(myAuthenticationSuccessHandler()) |
| 52 | + .failureUrl("/login.html?error=true") |
| 53 | + |
| 54 | + .and() |
| 55 | + .logout().deleteCookies("JSESSIONID") |
| 56 | + |
| 57 | + .and() |
| 58 | + .rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400) |
| 59 | + |
| 60 | + .and() |
| 61 | + .csrf().disable() |
| 62 | + ; |
| 63 | + // @formatter:on |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + public AuthenticationSuccessHandler myAuthenticationSuccessHandler(){ |
| 68 | + return new MySimpleUrlAuthenticationSuccessHandler(); |
| 69 | + } |
16 | 70 | } |
0 commit comments