2

I'm looking for a way to validate whether the custom header 'X-Client-Id' is set to a value defined in a Repository within an HTTP request sent to a REST-controller in SpringBoot.

I see tutorials (like this) which includes the header in the method like this: @RequestHeader(value="User-Agent"). I assume I would have to write that line to every method and inject a common validator-bean to verify the value.

Another stackoverflow answer seems to suggest using an HandlerInterceptor. I'm not sure though if that's applicable to header values and REST endpoints.

So what is the recommended way to validate all methods of a class/REST-controller whether a specific header is set or not?

Community
  • 1
  • 1
user3105453
  • 1,881
  • 5
  • 32
  • 55

1 Answers1

2

Basically the easiest (and most logical) way is to catch the Request before it gets to your Controller. That can be achieved either with a HandlerInterceptor as the other answer states or with a simple Filter like OncePerRequestFilter.

Extend that class, override the doFilterInternal() method as doFilter() is final, extract the proper header value, check it against whatever you need and depending on the value, either throw an Exception or continue with the chain.

rorschach
  • 2,871
  • 1
  • 17
  • 20
  • I think I found the perfect explanation with an example here: http://stackoverflow.com/a/35458670/3105453. Thanks for your help and pointing the right direction – user3105453 Sep 19 '16 at 14:37