jboss Resteasy parameter injection with @Context

前提是你 提交于 2019-12-02 06:02:21

问题


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.



来源:https://stackoverflow.com/questions/26275771/jboss-resteasy-parameter-injection-with-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!