|
| 1 | +package org.woehlke.humanrights.digital.defense.defcon1.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 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.web.authentication.RememberMeServices; |
| 12 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 13 | +import org.springframework.session.data.redis.RedisOperationsSessionRepository; |
| 14 | +import org.springframework.session.security.SpringSessionBackedSessionRegistry; |
| 15 | +import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices; |
| 16 | +import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; |
| 17 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 18 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 19 | +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; |
| 20 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 21 | +import org.woehlke.humanrights.digital.defense.defcon1.config.properties.ApplicationProperties; |
| 22 | + |
| 23 | +//import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * Created by tw on 24.06.18. |
| 28 | + */ |
| 29 | +@Configuration |
| 30 | +@EnableWebMvc |
| 31 | +@EnableWebSecurity |
| 32 | +//@EnableRedisHttpSession |
| 33 | +//@EnableSpringDataWebSupport |
| 34 | +@EnableConfigurationProperties({ |
| 35 | + //AllProperties.class, |
| 36 | + //StorageProperties.class, |
| 37 | + ApplicationProperties.class |
| 38 | +}) |
| 39 | +public class WebMvcWebSecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{ |
| 40 | + |
| 41 | + @Override |
| 42 | + public void addViewControllers(ViewControllerRegistry registry) { |
| 43 | + registry.addViewController("/").setViewName("redirect:/welcome"); |
| 44 | + registry.addViewController("/adm").setViewName("redirect:/adm/dashboard"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 49 | + registry.addResourceHandler("/css/*").addResourceLocations("classpath:/public/css/"); |
| 50 | + registry.addResourceHandler("/css/**").addResourceLocations("classpath:/public/css/"); |
| 51 | + registry.addResourceHandler("/icon/*").addResourceLocations("classpath:/public/icon/"); |
| 52 | + registry.addResourceHandler("/icon/**").addResourceLocations("classpath:/public/icon/"); |
| 53 | + registry.addResourceHandler("/img/*").addResourceLocations("classpath:/public/img/"); |
| 54 | + registry.addResourceHandler("/img/**").addResourceLocations("classpath:/public/img/"); |
| 55 | + registry.addResourceHandler("/js/*").addResourceLocations("classpath:/public/js/"); |
| 56 | + registry.addResourceHandler("/js/**").addResourceLocations("classpath:/public/js/"); |
| 57 | + registry.addResourceHandler("/map-icons/*").addResourceLocations("classpath:/public/map-icons/"); |
| 58 | + registry.addResourceHandler("/map-icons/**").addResourceLocations("classpath:/public/map-icons/"); |
| 59 | + registry.addResourceHandler("/webjars/*").addResourceLocations("/webjars/"); |
| 60 | + registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/"); |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public MethodValidationPostProcessor methodValidationPostProcessor() { |
| 65 | + return new MethodValidationPostProcessor(); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public SpringSessionBackedSessionRegistry sessionRegistry() { |
| 70 | + return new SpringSessionBackedSessionRegistry<>(this.sessionRepository); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +/* |
| 75 | + @Override |
| 76 | + protected void configure(HttpSecurity http) throws Exception { |
| 77 | + http |
| 78 | + .authorizeRequests() |
| 79 | + .antMatchers( |
| 80 | + applicationProperties.getWebSecurityConfigPublicPathsAsArray() |
| 81 | + ) |
| 82 | + .permitAll() |
| 83 | + .anyRequest().authenticated() |
| 84 | + .and() |
| 85 | + .formLogin() |
| 86 | + .loginPage(applicationProperties.getLoginPage()) |
| 87 | + .failureForwardUrl(applicationProperties.getFailureForwardUrl()) |
| 88 | + .defaultSuccessUrl(applicationProperties.getDefaultSuccessUrl()) |
| 89 | + .permitAll() |
| 90 | + .and() |
| 91 | + .rememberMe() |
| 92 | + .rememberMeServices(rememberMeServices()) |
| 93 | + .and() |
| 94 | + .logout() |
| 95 | + .logoutRequestMatcher(new AntPathRequestMatcher(applicationProperties.getLogoutRequestMatcher())) |
| 96 | + .logoutSuccessUrl(applicationProperties.getLogoutSuccessUrl()) |
| 97 | + .permitAll(); |
| 98 | + } |
| 99 | +
|
| 100 | + @Autowired |
| 101 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 102 | + String loginUsername = applicationProperties.getLoginUsername(); |
| 103 | + String loginPassword = applicationProperties.getLoginPassword(); |
| 104 | + String loginRole = applicationProperties.getLoginRole(); |
| 105 | + auth |
| 106 | + .inMemoryAuthentication() |
| 107 | + .withUser(loginUsername) |
| 108 | + .password(loginPassword) |
| 109 | + .roles(loginRole); |
| 110 | + } |
| 111 | + */ |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void configure(HttpSecurity http) throws Exception { |
| 115 | + http |
| 116 | + .authorizeRequests() |
| 117 | + .antMatchers( |
| 118 | + applicationProperties.getWebSecurityConfigPublicPathsAsArray() |
| 119 | + ) |
| 120 | + .permitAll() |
| 121 | + .anyRequest().authenticated() |
| 122 | + .and() |
| 123 | + .formLogin() |
| 124 | + .loginPage("/login") |
| 125 | + .failureForwardUrl("/login") |
| 126 | + .defaultSuccessUrl("/adm") |
| 127 | + .permitAll() |
| 128 | + .and() |
| 129 | + .rememberMe() |
| 130 | + .rememberMeServices(rememberMeServices()) |
| 131 | + .and() |
| 132 | + .logout() |
| 133 | + .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) |
| 134 | + .logoutSuccessUrl("/logout_success") |
| 135 | + .permitAll(); |
| 136 | + } |
| 137 | + |
| 138 | + @Autowired |
| 139 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 140 | + String user = "user" ; //applicationProperties.getLoginUsername(); |
| 141 | + String pwd = "password"; // applicationProperties.getLoginPassword(); |
| 142 | + String role = "USER"; |
| 143 | + auth |
| 144 | + .inMemoryAuthentication() |
| 145 | + .withUser(user).password(pwd).roles(role); |
| 146 | + } |
| 147 | + |
| 148 | + @Bean |
| 149 | + public RememberMeServices rememberMeServices() { |
| 150 | + SpringSessionRememberMeServices rememberMeServices = |
| 151 | + new SpringSessionRememberMeServices(); |
| 152 | + // optionally customize |
| 153 | + rememberMeServices.setAlwaysRemember(true); |
| 154 | + return rememberMeServices; |
| 155 | + } |
| 156 | + |
| 157 | + @Autowired |
| 158 | + private RedisOperationsSessionRepository sessionRepository; |
| 159 | + |
| 160 | + @Autowired |
| 161 | + private ApplicationProperties applicationProperties; |
| 162 | + |
| 163 | +} |
0 commit comments