Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication in multiple methods with JAX-RS/Jersey - How to keep it clean?

I'm working on a web service written in Java using JAX-RS/Jersey and Spring.

The WS authenticates a user when he or she logs in and returns an access key which has a limited lifespan. Most of the available methods require a valid "access key + userId" combination.

For all of the available methods I am using @FormParam to retrieve POST parameters, and the response is in JSON. What I am trying to figure out is how to avoid having to type out

@FormParam("userId") final Integer userId,
@FormParam("accessKey") final String accessKey

and manually invoke my authentication method for each of the probably 20 methods I will have that require authentication. This is especially cumbersome because I will then also have to add code to handle e.g. expired access keys.

Is there another way to go about retrieving the two parameters above and performing authentication so I can keep my methods clean?

like image 443
John B Avatar asked Jan 27 '26 18:01

John B


1 Answers

Since you're using Jersey, you can use servlet-filter-like APIs to DRY up your code. Check out:

  • Equivalent of Servlet Filter for Jersey / JAX-RS / REST resources?
  • How does one intercept a request during the Jersey lifecycle?
  • ContainerRequestFilter
like image 119
Matt Ball Avatar answered Jan 30 '26 08:01

Matt Ball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!