|
| 1 | +package org.woehlke.greenshop.conf; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.context.annotation.Import; |
| 8 | +import org.springframework.data.web.config.EnableSpringDataWebSupport; |
| 9 | +import org.springframework.security.authentication.AuthenticationManager; |
| 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.configuration.EnableWebSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 15 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 16 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 17 | +import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; |
| 18 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 19 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 20 | +import org.woehlke.greenshop.oodm.admin.service.AdministratorService; |
| 21 | + |
| 22 | + |
| 23 | +@SuppressWarnings("deprecation") |
| 24 | +@Configuration |
| 25 | +@EnableWebSecurity |
| 26 | +@EnableSpringDataWebSupport |
| 27 | +@Import({ |
| 28 | + GreenshopWebMvcConfig.class |
| 29 | +}) |
| 30 | +@EnableWebMvc |
| 31 | +@EnableAutoConfiguration |
| 32 | +public class GreenshopWebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + public GreenshopWebSecurityConfig( |
| 36 | + AuthenticationManagerBuilder auth, |
| 37 | + AdministratorService administratorService, |
| 38 | + GreenshopProperties greenshopProperties |
| 39 | + ) { |
| 40 | + this.authenticationManagerBuilder = auth; |
| 41 | + this.administratorService = administratorService; |
| 42 | + this.greenshopProperties = greenshopProperties; |
| 43 | + } |
| 44 | + |
| 45 | + private final AuthenticationManagerBuilder authenticationManagerBuilder; |
| 46 | + private final UserDetailsService administratorService; |
| 47 | + private final GreenshopProperties greenshopProperties; |
| 48 | + |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void configure(HttpSecurity http) throws Exception { |
| 52 | + http |
| 53 | + .headers() |
| 54 | + .disable() |
| 55 | + .authorizeRequests() |
| 56 | + .antMatchers( |
| 57 | + this.greenshopProperties.getWebSecurity().getAntMatchersPermitAll() |
| 58 | + ) |
| 59 | + .permitAll() |
| 60 | + .antMatchers( |
| 61 | + this.greenshopProperties.getWebSecurity().getAntMatchersFullyAuthenticated() |
| 62 | + ) |
| 63 | + .fullyAuthenticated().anyRequest().authenticated() |
| 64 | + .and() |
| 65 | + .formLogin() |
| 66 | + .loginPage( |
| 67 | + this.greenshopProperties.getWebSecurity().getLoginPage() |
| 68 | + ) |
| 69 | + .usernameParameter(this.greenshopProperties.getWebSecurity().getUsernameParameter()) |
| 70 | + .passwordParameter(this.greenshopProperties.getWebSecurity().getPasswordParameter()) |
| 71 | + .defaultSuccessUrl(this.greenshopProperties.getWebSecurity().getDefaultSuccessUrl()) |
| 72 | + .failureForwardUrl(this.greenshopProperties.getWebSecurity().getFailureForwardUrl()) |
| 73 | + .loginProcessingUrl(this.greenshopProperties.getWebSecurity().getLoginProcessingUrl()) |
| 74 | + //.successHandler(this.authenticationSuccessHandler) |
| 75 | + .permitAll() |
| 76 | + .and() |
| 77 | + .logout() |
| 78 | + .logoutUrl(this.greenshopProperties.getWebSecurity().getLogoutUrl()) |
| 79 | + .deleteCookies(this.greenshopProperties.getWebSecurity().getDeleteCookies()) |
| 80 | + .invalidateHttpSession(this.greenshopProperties.getWebSecurity().getInvalidateHttpSession()) |
| 81 | + .permitAll(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @see <a href="https://asecuritysite.com/encryption/PBKDF2">Encrypt with PBKDF2</a> |
| 86 | + * @return PasswordEncoder encoder |
| 87 | + */ |
| 88 | + @Bean |
| 89 | + public PasswordEncoder encoder(){ |
| 90 | + CharSequence secret=this.greenshopProperties.getWebSecurity().getSecret(); |
| 91 | + int iterations=this.greenshopProperties.getWebSecurity().getIterations(); |
| 92 | + int hashWidth=this.greenshopProperties.getWebSecurity().getHashWidth(); |
| 93 | + Pbkdf2PasswordEncoder encoder = (new Pbkdf2PasswordEncoder(secret,iterations,hashWidth)); |
| 94 | + encoder.setEncodeHashAsBase64(true); |
| 95 | + return encoder; |
| 96 | + } |
| 97 | + |
| 98 | + @Bean |
| 99 | + public DaoAuthenticationProvider authProvider() { |
| 100 | + DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); |
| 101 | + authProvider.setUserDetailsService( this.administratorService); |
| 102 | + authProvider.setPasswordEncoder(encoder()); |
| 103 | + return authProvider; |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public AuthenticationManager authenticationManager() throws Exception { |
| 108 | + return this.authenticationManagerBuilder.userDetailsService(administratorService).passwordEncoder(encoder()).and().build(); |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + public UsernamePasswordAuthenticationFilter authenticationFilter() throws Exception { |
| 113 | + UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter(); |
| 114 | + filter.setAuthenticationManager(authenticationManager()); |
| 115 | + filter.setFilterProcessesUrl(this.greenshopProperties.getWebSecurity().getLoginProcessingUrl()); |
| 116 | + return filter; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments