如何根据 URL 创建 JWT 过滤器的例外

how to create exceptions to JWT filter based on URLs

我正在研究 JWT authentication.I 想要绕过(不允许通过 jwt 过滤器)提供的 URL。下面是代码片段。

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable().authorizeRequests().antMatchers(bypassURLs)
                    .permitAll().anyRequest().authenticated().and().addFilter(new JWTAuthenticationFilter(authenticationManager(), jwtConfigProperties))
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}

在上面的代码中,我希望系统不过滤 bypassURLs。 如果我传递“/sayHello”,则不应将 JWTAuthenticationFilter 应用于此,而“/sayHello”以外的 URL 必须通过 JWTAuthenticationFilter。

我试过 http.csrf().ignoringAntMatchers("/sayHello"); 和一些正则表达式但没有成功。请帮忙

当使用 permitAll 时,它意味着每个经过身份验证的用户,但是您禁用了匿名访问,因此无法使用。

你想要的是忽略某些 URLs,因为它会覆盖采用 WebSecurity 对象和 ignore 模式的 configure 方法。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/v1/signup");
}

并从 HttpSecurity 部分删除该行。这将告诉 Spring 安全忽略此 URL 并且不对其应用任何过滤器。