4

I would like to exclude HTTP OPTION requests from my javax.servlet.Filter. How can I achieve it?

Filter registration:

@Bean
public FilterRegistrationBean filterRegistrationBean() {

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

    return registrationBean;
}

I would like to avoid XML configuration.

Actual state

Now I am excluding OPTIONS method in filter:

 @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);
                }
            }
        }

    }
Artegon
  • 2,734
  • 6
  • 32
  • 62
  • 1
    This already can't be acheived by XML configuration in first place and thus programmatic configuration can do very little. This can only be achieved by editing filter's `doFilter()` implementation. – BalusC Sep 17 '15 at 11:16
  • @BalusC - I added my code, but I would like to avoid do it manually. – Artegon Sep 17 '15 at 11:19
  • You don't need to show filter code for that, it only adds distracting noise to question. I understand that, but you can't avoid doing that manually. Unless Spring Boot (which I'm totally not familiar with) has some reflection/AOP trickery under its covers in this regard, but I wouldn't expect that. – BalusC Sep 17 '15 at 11:19
  • I see, thanks for clarification. I thought there will be an options to avoid doing it manually. – Artegon Sep 17 '15 at 11:21
  • It already can't be achieved by XML configuration in first place. It if were, programmatic configuration would be a breeze. – BalusC Sep 17 '15 at 11:21

0 Answers0