Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot: Bypass OncePerRequestFilter filters

I have a basic SpringBoot 2.0.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.

I have created a Custom JWT based security filter JwtFilter :

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

But I want to Bypass this filter only for 1 specific request / method: "/api/v1/menus", when is a POST

But I don't know if it is possible in the WebSecurityConfigurerAdapter :

 JwtAuthorizationTokenFilter authenticationTokenFilter = new JwtAuthorizationTokenFilter(userDetailsService(), jwtTokenUtil, tokenHeader);
     httpSecurity
         .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
          .antMatcher("/api/v1/menus")
like image 733
Nunyet de Can Calçada Avatar asked Sep 17 '18 14:09

Nunyet de Can Calçada


1 Answers

You can override the shouldNotFilter method from the OncePerRequestFilter as below:

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

}
like image 51
Tom Avatar answered Sep 23 '22 20:09

Tom