jboss Resteasy parameter injection with @Context

2019-02-26 22:12发布

问题:

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

回答1:

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.