|
21 | 21 |
|
22 | 22 | import org.springframework.context.annotation.Bean;
|
23 | 23 | import org.springframework.context.annotation.Configuration;
|
24 |
| -import org.springframework.context.annotation.ImportResource; |
25 | 24 | import org.springframework.context.ApplicationListener;
|
| 25 | +import org.springframework.http.HttpMethod; |
26 | 26 |
|
| 27 | +import org.springframework.context.MessageSource; |
| 28 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 29 | +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; |
| 30 | +import org.springframework.security.authentication.dao.ReflectionSaltSource; |
| 31 | +import org.springframework.security.authentication.dao.SaltSource; |
27 | 32 | import org.springframework.security.authentication.encoding.PasswordEncoder;
|
28 | 33 | import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
|
29 | 34 | import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
|
| 35 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 36 | +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
| 37 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 38 | +import org.springframework.security.config.annotation.web.builders.WebSecurity; |
| 39 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 40 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
30 | 41 | import org.springframework.security.core.userdetails.UserDetailsService;
|
31 |
| -import org.springframework.security.web.AuthenticationEntryPoint; |
32 | 42 |
|
33 | 43 | import ru.mystamps.web.config.ServicesConfig;
|
| 44 | +import ru.mystamps.web.Url; |
34 | 45 |
|
35 | 46 | @Configuration
|
36 |
| -@ImportResource("classpath:spring/security.xml") |
37 |
| -public class SecurityConfig { |
| 47 | +@EnableWebSecurity |
| 48 | +@EnableGlobalMethodSecurity(prePostEnabled = true) |
| 49 | +public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 50 | + |
| 51 | + @Inject |
| 52 | + private MessageSource messageSource; |
38 | 53 |
|
39 | 54 | @Inject
|
40 | 55 | private ServicesConfig servicesConfig;
|
41 | 56 |
|
42 |
| - @Bean |
43 |
| - public ApplicationListener<AuthenticationFailureBadCredentialsEvent> getApplicationListener() { |
44 |
| - return new AuthenticationFailureListener(); |
| 57 | + @Override |
| 58 | + @SuppressWarnings("PMD.SignatureDeclareThrowsException") |
| 59 | + public void configure(WebSecurity web) throws Exception { |
| 60 | + web.ignoring().antMatchers("/static/**"); |
45 | 61 | }
|
46 | 62 |
|
47 |
| - // Explicitly specified bean names due to its usage in XML config |
| 63 | + @Override |
| 64 | + @SuppressWarnings("PMD.SignatureDeclareThrowsException") |
| 65 | + protected void configure(HttpSecurity http) throws Exception { |
| 66 | + http |
| 67 | + .authorizeRequests() |
| 68 | + .antMatchers(Url.ADD_CATEGORY_PAGE).hasAuthority("CREATE_CATEGORY") |
| 69 | + .antMatchers(Url.ADD_COUNTRY_PAGE).hasAuthority("CREATE_COUNTRY") |
| 70 | + .antMatchers(Url.ADD_SERIES_PAGE).hasAuthority("CREATE_SERIES") |
| 71 | + .regexMatchers(HttpMethod.POST, "/series/[0-9]+").hasAuthority("UPDATE_COLLECTION") |
| 72 | + .anyRequest().permitAll() |
| 73 | + .and() |
| 74 | + .formLogin() |
| 75 | + .loginPage(Url.AUTHENTICATION_PAGE) |
| 76 | + .usernameParameter("login") |
| 77 | + .passwordParameter("password") |
| 78 | + .loginProcessingUrl(Url.LOGIN_PAGE) |
| 79 | + .failureUrl(Url.AUTHENTICATION_PAGE + "?failed") |
| 80 | + .defaultSuccessUrl(Url.INDEX_PAGE, true) |
| 81 | + .permitAll() |
| 82 | + .and() |
| 83 | + .logout() |
| 84 | + .logoutUrl(Url.LOGOUT_PAGE) |
| 85 | + .logoutSuccessUrl(Url.INDEX_PAGE) |
| 86 | + .invalidateHttpSession(true) |
| 87 | + .permitAll() |
| 88 | + .and() |
| 89 | + .exceptionHandling() |
| 90 | + .accessDeniedPage(Url.UNAUTHORIZED_PAGE) |
| 91 | + // This entry point handles when you request a protected page and you are |
| 92 | + // not yet authenticated (defaults to Http403ForbiddenEntryPoint) |
| 93 | + .authenticationEntryPoint(new Http401UnauthorizedEntryPoint()) |
| 94 | + .and() |
| 95 | + .rememberMe() |
| 96 | + // TODO: GH #27 |
| 97 | + .disable() |
| 98 | + .csrf() |
| 99 | + // TODO: GH #25 |
| 100 | + .disable() |
| 101 | + .headers() |
| 102 | + // TODO |
| 103 | + .disable(); |
| 104 | + } |
48 | 105 |
|
49 |
| - @Bean(name = "passwordEncoder") |
| 106 | + @Inject |
| 107 | + protected void configure(AuthenticationManagerBuilder auth) { |
| 108 | + auth.authenticationProvider(getAuthenticationProvider()); |
| 109 | + } |
| 110 | + |
| 111 | + // Used in ServicesConfig.getUserService() |
50 | 112 | public PasswordEncoder getPasswordEncoder() {
|
51 | 113 | return new ShaPasswordEncoder();
|
52 | 114 | }
|
| 115 | + |
| 116 | + @Bean |
| 117 | + public ApplicationListener<AuthenticationFailureBadCredentialsEvent> getApplicationListener() { |
| 118 | + return new AuthenticationFailureListener(); |
| 119 | + } |
53 | 120 |
|
54 |
| - @Bean(name = "customUserDetailsService") |
55 |
| - public UserDetailsService getUserDetailsService() { |
| 121 | + private UserDetailsService getUserDetailsService() { |
56 | 122 | return new CustomUserDetailsService(servicesConfig.getUserService());
|
57 | 123 | }
|
58 | 124 |
|
59 |
| - @Bean(name = "http401UnauthorizedEntryPoint") |
60 |
| - public AuthenticationEntryPoint getHttp401UnauthorizedEntryPoint() { |
61 |
| - return new Http401UnauthorizedEntryPoint(); |
| 125 | + private SaltSource getSaltSource() { |
| 126 | + ReflectionSaltSource saltSource = new ReflectionSaltSource(); |
| 127 | + saltSource.setUserPropertyToUse("salt"); |
| 128 | + return saltSource; |
| 129 | + } |
| 130 | + |
| 131 | + private AuthenticationProvider getAuthenticationProvider() { |
| 132 | + DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); |
| 133 | + provider.setPasswordEncoder(getPasswordEncoder()); |
| 134 | + provider.setSaltSource(getSaltSource()); |
| 135 | + provider.setUserDetailsService(getUserDetailsService()); |
| 136 | + provider.setMessageSource(messageSource); |
| 137 | + return provider; |
62 | 138 | }
|
63 | 139 |
|
64 | 140 | }
|
0 commit comments