1

I am currently checking all the incoming Authorization headers for JWT token values so that I can check if those tokens are blacklisted. I have implemented a custom Filter for this.

The problem is that this filter is handling all the requests that are coming into the server. I'd like to handle only the requests that are coming to the /oauth/token endpoint.

Is it possible to implement that in Spring OAuth2 Auth Server?

4
  • Are you a resource or an authorization server? Commented Apr 24, 2018 at 12:42
  • @JoseMartinez for auth server. Commented Apr 24, 2018 at 12:42
  • And when you say "filter out" does that mean you want your Filter to ignore requests to '/oauth/token' or those are the ones you trying to block? Commented Apr 24, 2018 at 12:44
  • @JoseMartinez sorry, I phrased it incorrectly. I want to handle only the requests that are incoming to oauth/token endpoint. Commented Apr 24, 2018 at 12:46

3 Answers 3

6

There are two aspects to your question, one is Filter order and two is only filtering a specific URL path. Both can be tackled by using a FilterRegistrationBean to inject your filter. See example below.

@Bean
public FilterRegistrationBean securityFilter() {
    Filter f = new MySecurityFilter();
    FilterRegistrationBean frb = new FilterRegistrationBean(f);
    frb.setOrder(SecurityProperties.ACCESS_OVERRIDE_ORDER);
    frb.setUrlPatterns(Arrays.asList("/oauth/token"));
    return frb;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's a question of priority of your filter. Add :

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 

To your WebSecurityConfiguration should help you.

Comments

1

In the case your custom filter extends the OncePerRequestFilter you can override the shouldNotFilter method.

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
    return !new AntPathMatcher().match("/oauth/token", request.getServletPath());
}

From the Doc

Can be overridden in subclasses for custom filtering control, returning true to avoid filtering of the given request.

1 Comment

The custom filter itself is implementing the Filter interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.