FilterRegistrationBean捕获所有请求

时间:2019-01-06 12:34:14

标签: java spring-boot filter

我想使用FilterRegistrationBean传递所有请求,并抛出一个过滤器。

@Bean
public FilterRegistrationBean<JwtApplicationFilter> JWTApiFilter() {
    FilterRegistrationBean<JwtApplicationFilter> registration = new FilterRegistrationBean<JwtApplicationFilter>();
    registration.setFilter(new JwtApplicationFilter());
    //In case you want the filter to apply to specific URL patterns only
    registration.addUrlPatterns("/xxx");
    registration.addUrlPatterns("/xxx/**");
    registration.addUrlPatterns("/base/");
    registration.addUrlPatterns("/xxx/*");
    registration.addUrlPatterns("/");
    registration.addUrlPatterns("/**");
    return registration;
}

我已经尝试了所有这些模式,还有更多,我只有一个使用/base-

的控制器
@RestController
@RequestMapping("/base/")
public class BaseController {
   /***
}

在我的application.properties中-

 server.servlet.context-path =/xxx

JwtApplicationFilter-

public class JwtFilterLogin extends GenericFilterBean {

    private FilterHandlers loginHandler;

    private static final Logger LOGGER = LoggerFactory.getLogger(JwtFilterLogin.class);
    public static final String HEADER_AUTH = "Authorization";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httprequest = (HttpServletRequest) request;
        HttpServletResponse httpresponse = (HttpServletResponse) response;

        LOGGER.info("JwtFilterLogin for URI=" + httprequest.getRequestURI()); //not logging or stops at debug

    }

}

1 个答案:

答案 0 :(得分:0)

我缺少registration.addUrlPatterns("/*");来匹配所有请求。