JwtAuthorizationTokenFilter

JwtAuthorizationTokenFilter

我有一个基本的SpringBoot 2.0.5.RELEASE应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎并将其打包为可执行JAR文件。

我创建了一个基于自定义JWT的安全过滤器JwtFilter:

@Provider
public class JwtAuthorizationTokenFilter extends OncePerRequestFilter {
...
}

但我只想针对1个特定请求/方法绕过此过滤器:"/api/v1/menus",何时是POST
但是我不知道WebSecurityConfigurerAdapter是否可行:
 JwtAuthorizationTokenFilter authenticationTokenFilter = new JwtAuthorizationTokenFilter(userDetailsService(), jwtTokenUtil, tokenHeader);
     httpSecurity
         .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
          .antMatcher("/api/v1/menus")

最佳答案

您可以从OncePerRequestFilter重写shouldNotFilter方法,如下所示:

@Provider
public class JwtAuthorizationTokenFilter extends OncePerRequestFilter {
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return new AntPathMatcher().match("/api/v1/menus", request.getServletPath());
    }

}

08-27 15:20