jboss用@Context注入Resteasy参数

use*_*570 2 java rest jboss jax-rs resteasy

我正在使用jboss 7.1和resteasy进行基于令牌的身份验证.我正在使用PreProcessInterceptor来拦截请求,获取令牌,从令牌中检索用户,然后根据放置在方法上的自定义注释检查用户角色.我现在要做的是将User注入方法,如下所示.

@Path("/doStuffWithUser")
@GET
@Requires("ADMIN") // custom annotation
public Response doStuffWithUser(@Context User user);
Run Code Online (Sandbox Code Playgroud)

我知道这与这个问题非常接近,我尝试在链接的github示例中添加了不同的解决方案,但是我找不到从PreProcessInterceptor中注入用户的方法.

谢谢

use*_*570 12

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);

}
Run Code Online (Sandbox Code Playgroud)

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

Hope it will help someone.

  • Resteasy 3.x中不推荐使用PreProcessInterceptor.在3.x版本中,可以使用javax.ws.rs.container.ContainerRequestFilter完成此操作.有关更多信息,请参见http://stackoverflow.com/a/17597783/122975 (2认同)