|
| 1 | +package com.baeldung.securityextrafields; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.context.annotation.PropertySource; |
| 5 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 6 | +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; |
| 7 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 8 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 9 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 10 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 11 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 12 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 13 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 14 | +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
| 15 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 16 | + |
| 17 | +@EnableWebSecurity |
| 18 | +@PropertySource("classpath:/application-extrafields.properties") |
| 19 | +public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private UserDetailsService userDetailsService; |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void configure(HttpSecurity http) throws Exception { |
| 26 | + |
| 27 | + http |
| 28 | + .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class) |
| 29 | + .authorizeRequests() |
| 30 | + .antMatchers("/css/**", "/index").permitAll() |
| 31 | + .antMatchers("/user/**").authenticated() |
| 32 | + .and() |
| 33 | + .formLogin().loginPage("/login") |
| 34 | + .and() |
| 35 | + .logout() |
| 36 | + .logoutUrl("/logout"); |
| 37 | + } |
| 38 | + |
| 39 | + public CustomAuthenticationFilter authenticationFilter() throws Exception { |
| 40 | + CustomAuthenticationFilter filter = new CustomAuthenticationFilter(); |
| 41 | + filter.setAuthenticationManager(authenticationManagerBean()); |
| 42 | + filter.setAuthenticationFailureHandler(failureHandler()); |
| 43 | + return filter; |
| 44 | + } |
| 45 | + |
| 46 | + @Autowired |
| 47 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 48 | + auth.authenticationProvider(authProvider()); |
| 49 | + } |
| 50 | + |
| 51 | + public AuthenticationProvider authProvider() { |
| 52 | + DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); |
| 53 | + provider.setUserDetailsService(userDetailsService); |
| 54 | + provider.setPasswordEncoder(passwordEncoder()); |
| 55 | + return provider; |
| 56 | + } |
| 57 | + |
| 58 | + public SimpleUrlAuthenticationFailureHandler failureHandler() { |
| 59 | + return new SimpleUrlAuthenticationFailureHandler("/login?error=true"); |
| 60 | + } |
| 61 | + |
| 62 | + public PasswordEncoder passwordEncoder() { |
| 63 | + return new BCryptPasswordEncoder(); |
| 64 | + } |
| 65 | +} |
0 commit comments