首页 > 解决方案 > Spring Security - 蚂蚁匹配器允许所有不工作

问题描述

我对 antMatchers 有疑问。这不像我预期的那样工作。我尝试允许所有端点 /tokens/**,但是当调用此端点时,我的过滤器也会被调用(JwtTokenVerifier)

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/tokens/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .cors()
            .and()
            .addFilter(new JwtUsernameAndPasswordAuthenticationFilter(authenticationManager(), this.jwtProperties, objectMapper))
            .addFilterAfter(new JwtTokenVerifier(this.jwtProperties), JwtUsernameAndPasswordAuthenticationFilter.class);
}

每次我尝试访问例如 /tokens/refresh-access-token 时都会调用 JwtTokenVerifier。

@PostMapping("/tokens/refresh-access-token")
@ResponseStatus(HttpStatus.OK)
public Map<String,String> refreshAccessToken(@RequestBody String refreshToken)

令牌验证器:

public class JwtTokenVerifier extends OncePerRequestFilter {

private final JwtProperties jwtProperties;

public JwtTokenVerifier(JwtProperties jwtProperties) {
    this.jwtProperties = jwtProperties;
}

@Override
protected void doFilterInternal(HttpServletRequest request,
                                HttpServletResponse response,
                                FilterChain filterChain) throws ServletException, IOException {

标签: springspring-bootspring-security

解决方案


如果可能,您可以尝试通过覆盖 WebSecurity 方法将 URL 从 Spring Security 处理中完全排除。不过,我不知道您是否想完全忽略该路径的 FilterChain。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/tokens/**");
}

推荐阅读