如何从`javax.servlet.Filter`中排除特定的HTTP方法?

时间:2015-09-17 11:08:04

标签: spring spring-boot servlet-filters http-method

我想从OPTION中排除HTTP javax.servlet.Filter请求。我怎样才能实现它?

过滤器注册:

@Bean
public FilterRegistrationBean filterRegistrationBean() {

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(authorizationRequestFilter());
    registrationBean.addUrlPatterns("/persons/*", "/accounts/*");

    return registrationBean;
}

我想避免使用XML配置。

实际状态

现在我在过滤器中排除OPTIONS方法:

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

        HttpServletRequest request = (HttpServletRequest) req;

        if ("OPTIONS".equals(request.getMethod())) {
            chain.doFilter(req, res);
        } else {
            String accessToken = request.getHeader(AUTHORIZATION_TOKEN);
            if (StringUtils.isEmpty(accessToken)) {
                HttpResponseWriter.throwUnauthorized(res);
            } else {
                AccountLoginData account = loginService.find(accessToken);
                if (account == null) {
                    HttpResponseWriter.throwForbidden(res);
                } else {
                    chain.doFilter(req, res);
                }
            }
        }

    }

0 个答案:

没有答案