3

I'm doing a token based authentication using jboss 7.1 and resteasy. I'm using a PreProcessInterceptor to intercept request, get token, retrieve user from token and then check user roles against custom annotations placed on the method. What I want to do now is to inject User into the method like folowing.

@Path("/doStuffWithUser")
@GET
@Requires("ADMIN") // custom annotation
public Response doStuffWithUser(@Context User user);

I know this is very close from this question and I have tried addapting the differents solutions proposed on the linked github example but I can't find a way to inject the user from within my PreProcessInterceptor.

Thanks

Community
  • 1
  • 1
user2641570
  • 804
  • 8
  • 20

1 Answers1

13

This is the solution I finnaly found:

PreProcessInterceptor.preProcess(..){
    ... 
    retrieve User from token
    check roles
    ...
    //add the user to the context data
    ResteasyProviderFactory.pushContext(User.class, user);

}

You can then retrieve the User with the notation I used in my question.

Hope it will help someone.

user2641570
  • 804
  • 8
  • 20
  • 1
    is there a jax-rs agnostic way? – Setheron Nov 02 '15 at 19:29
  • 2
    PreProcessInterceptor is deprecated in Resteasy 3.x. In 3.x version, this can be done using javax.ws.rs.container.ContainerRequestFilter . See http://stackoverflow.com/a/17597783/122975 for more informations – Benoît Guérout Mar 05 '16 at 12:28