Spring security 用于通配符的spring安全映射

Spring security 用于通配符的spring安全映射,spring-security,spring-boot,Spring Security,Spring Boot,使用Spring Boot 1.1.17,Spring MVC和Spring Security: 我想允许未经验证的用户(访问者)访问几个子域。例如: mysite.com/customerA mysite.com/customerB 如果尝试了无效的客户站点,那么我的控制器将抛出异常或重定向回/(mysite.com/),当然域的其他部分(mysite.com/customerA/myaccount)将需要登录 我还没有真正弄明白如何使用SpringSecurity和SpringMVC来实

使用Spring Boot 1.1.17,Spring MVCSpring Security:

我想允许未经验证的用户(访问者)访问几个子域。例如:

  • mysite.com/customerA
  • mysite.com/customerB
如果尝试了无效的客户站点,那么我的控制器将抛出异常或重定向回/(mysite.com/),当然域的其他部分(mysite.com/customerA/myaccount)将需要登录

我还没有真正弄明白如何使用SpringSecurity和SpringMVC来实现这一点。以下是我目前正在尝试的:

@配置
@启用WebMVC安全性
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
@自动连线
私有CustomUserDetails服务CustomUserDetails服务;
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.addFilterAfter(新的CSRFTokenGeneratorFilter(),CsrfFilter.class)
.授权请求()
.antMatchers(“/”).permitAll()
.antMatchers(“/**/”).permitAll()
.antMatchers(“/login”).permitAll()
.antMatchers(“/wizard”).permitAll()
.antMatchers(“/menu”).permitAll()
.antMatchers(“/error”).permitAll()
.antMatchers(“/resources/**”).permitAll()
.antMatchers(“/css/**”).permitAll()
.antMatchers(“/js/**”).permitAll()
.antMatchers(“/fonts/**”).permitAll()
.antMatchers(“/libs/**”).permitAll();
http
.formLogin()
.loginPage(“/loginPage”)
.permitAll()
.loginProcessingUrl(“/login”)
.failureUrl(“/login?错误”)
.defaultSuccessUrl(“/?选项卡=成功”)
.及()
.logout().logoutRequestMatcher(新的AntPathRequestMatcher(“/logout”)).logoutSuccessUrl(“/”)
.permitAll()
.及()
.csrf();
http
.会议管理()
.最多会议(1)
.expiredUrl(“/login?过期”)
.maxSessionsPreventsLogin(真)
.及()
.sessionCreationPolicy(sessionCreationPolicy.IF_需要)
.invalidSessionUrl(“/”);
http
.authorizeRequests().anyRequest().authorized();
}
@凌驾
受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{
PasswordEncoder编码器=新的BCryptPasswordEncoder();
auth.userDetailsService(customUserDetailsService).passwordEncoder(编码器);
}
@凌驾
公共void配置(Web安全性){
忽略();
}
}
和我的主页控制器:

@控制器
@请求映射(“/{officeName}/”)
公共类家庭控制器{
私有身份验证用户getVisitor(@PathVariable String officeName){
…对办公室做点什么,如果发现,则重定向
如果(!StringUtils.isEmpty(officeName)){
Office=officeService.findByName(officeName);
returnoffice.getUrl();
}
返回“/”;
}
当我尝试访问该url时,会出现以下错误:

 o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/customerA/]
 s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /customerA/
 s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/customerA/]
 o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/customerA/] are [/**]
 o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/customerA/] are {}
 o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/customerA/] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@2f295527] and 1 interceptor
 o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/customerA/] is: -1
 o.s.w.s.r.ResourceHttpRequestHandler     : Trying relative path [customerA] against base location: ServletContext resource [/]
 o.s.w.s.r.ResourceHttpRequestHandler     : Trying relative path [customerA] against base location: class path resource [META-INF/resources/]
 o.s.w.s.r.ResourceHttpRequestHandler     : Trying relative path [customerA] against base location: class path resource [resources/]
 o.s.w.s.r.ResourceHttpRequestHandler     : Trying relative path [customerA] against base location: class path resource [static/]
 o.s.w.s.r.ResourceHttpRequestHandler     : Trying relative path [customerA] against base location: class path resource [public/]
 o.s.w.s.r.ResourceHttpRequestHandler     : No matching resource found - returning 404 
我尝试添加此ServletRegistrationBean:

@Bean
公共服务注册Bean dispatcherRegistration(DispatcherServlet DispatcherServlet){
ServletRegistrationBean注册=新的ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings(“/”,“/testCustomer/*”);
for(Office:officeService.findAllActiveOffices()){
registration.addUrlMappings(office.getUrl()+“/*”);
}
申报登记;
}
但这似乎只有在应用程序在启动时知道客户时才起作用,而在客户注册的情况下则不是动态的


有没有办法将其配置为处理这些类型的通配符?

您可以尝试以下配置:

@配置
@启用WebMVC安全性
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
@自动连线
私有用户详细信息服务_userService;
@自动连线
专用密码编码器_PasswordEncoder;
/**
*定义Spring security在测试期间使用的密码编码器
*认证程序。
*/
@豆子
公共密码编码器PasswordEncoder(){
//默认强度=10
返回新的BCryptPasswordEncoder();
}
/**
*设置身份验证管理器的安全配置
*/
@自动连线
public void configureGlobal(AuthenticationManagerBuilder身份验证)
抛出异常{
认证
.userDetailsService(_userService)
.passwordEncoder(_passwordEncoder);
返回;
}
/**
*配置禁用Spring安全性的位置(安全性=无)。
*来自spring参考:“通常是[此处]注册的请求
*应该是静态资源。对于动态请求,
*考虑将请求映射为允许所有用户。
*/
@凌驾
public void configure(WebSecurity web)引发异常{
忽略
蚂蚁配对者(
“/css/**”,
“/js/**”,
“/fonts/**”,
“/resources/**”,
“/libs/**”号;
返回;
}
/**
*在HttpSecurity对象中设置安全配置。
*/
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
//设置安全配置
http
.授权请求()
//任何用户都可以使用以下URL(无身份验证)
蚂蚁配对者(
"/",
“/login”,
“/菜单”)
.permitAll()
//任何其他url都必须经过身份验证
.anyRequest().authenticated()
.及()