我想从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);
}
}
}
}