如何整洁地配置Spring WebSecurity

时间:2019-01-18 08:35:13

标签: spring spring-boot spring-security

我目前正在使用Spring Security设置反向代理安全性域,其想法是默认情况下对所有请求都要求不记名令牌,除了一些例外,例如注册等。当前我的配置功能如下:

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))
            // this disables session creation on Spring Security
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

蚂蚁匹配器非常有用,但是您必须单独传递所有URL。有没有办法让我传递一个字符串数组,以便我可以使配置分开?

1 个答案:

答案 0 :(得分:0)

SETDATE VICRRP  QLD1RRP
0   01-06-2013  1.0 2.0
1   01-08-2013  8.0 NaN
2   01-08-2014  NaN 4.0

在上面的代码http.cors().and().csrf().disable().authorizeRequests() .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll() 中也将接受字符串数组。以下是Spring Security antMatchers中anyMatcher方法的实现。根据方法签名,您应该能够传递包含所需路径的字符串数组。

4.2.3.RELEASE

如果您深入研究实现,spring会将这个args转换为所有路径的ArrayList。

此外,还有另一种方法。如果要扩展spring的 /** * Maps a {@link List} of * {@link org.springframework.security.web.util.matcher.AntPathRequestMatcher} * instances that do not care which {@link HttpMethod} is used. * * @param antPatterns the ant patterns to create * {@link org.springframework.security.web.util.matcher.AntPathRequestMatcher} from * * @return the object that is chained after creating the {@link RequestMatcher} */ public C antMatchers(String... antPatterns) { return chainRequestMatchers(RequestMatchers.antMatchers(antPatterns)); } 类,要忽略Spring安全性不能保护的路径,请再次重写相同的方法。

WebSecurityConfigurerAdapter

我想这样很好。