|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import javax.annotation.PostConstruct; |
| 4 | +import javax.sql.DataSource; |
| 5 | + |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.ComponentScan; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; |
| 11 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 12 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 13 | +import org.springframework.security.config.annotation.web.builders.WebSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 15 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 16 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 17 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 18 | +import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; |
| 19 | +import org.springframework.web.context.WebApplicationContext; |
| 20 | + |
| 21 | +import com.baeldung.security.AuthenticationSuccessHandlerImpl; |
| 22 | +import com.baeldung.security.CustomUserDetailsService; |
| 23 | + |
| 24 | +@Configuration |
| 25 | +@EnableWebSecurity |
| 26 | +@ComponentScan("com.baeldung.security") |
| 27 | +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private WebApplicationContext applicationContext; |
| 31 | + private CustomUserDetailsService userDetailsService; |
| 32 | + @Autowired |
| 33 | + private AuthenticationSuccessHandlerImpl successHandler; |
| 34 | + @Autowired |
| 35 | + private DataSource dataSource; |
| 36 | + |
| 37 | + @PostConstruct |
| 38 | + public void completeSetup() { |
| 39 | + userDetailsService = applicationContext.getBean(CustomUserDetailsService.class); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { |
| 44 | + auth.userDetailsService(userDetailsService) |
| 45 | + .passwordEncoder(encoder()) |
| 46 | + .and() |
| 47 | + .authenticationProvider(authenticationProvider()) |
| 48 | + .jdbcAuthentication() |
| 49 | + .dataSource(dataSource); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void configure(WebSecurity web) throws Exception { |
| 54 | + web.ignoring() |
| 55 | + .antMatchers("/resources/**"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void configure(final HttpSecurity http) throws Exception { |
| 60 | + http.authorizeRequests() |
| 61 | + .antMatchers("/login") |
| 62 | + .permitAll() |
| 63 | + .and() |
| 64 | + .formLogin() |
| 65 | + .permitAll() |
| 66 | + .successHandler(successHandler) |
| 67 | + .and() |
| 68 | + .csrf() |
| 69 | + .disable(); |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + public DaoAuthenticationProvider authenticationProvider() { |
| 74 | + final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); |
| 75 | + authProvider.setUserDetailsService(userDetailsService); |
| 76 | + authProvider.setPasswordEncoder(encoder()); |
| 77 | + return authProvider; |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + public PasswordEncoder encoder() { |
| 82 | + return new BCryptPasswordEncoder(11); |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + public SecurityEvaluationContextExtension securityEvaluationContextExtension() { |
| 87 | + return new SecurityEvaluationContextExtension(); |
| 88 | + } |
| 89 | +} |
0 commit comments