JBoss Resteasy @Context参数注入

3

我正在使用jboss 7.1和resteasy进行基于令牌的身份验证。 我正在使用PreProcessInterceptor拦截请求,获取令牌,从令牌中检索用户,然后根据放置在方法上的自定义注释检查用户角色。现在我想像以下方式将用户注入到方法中。

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

我知道这与此问题非常接近,我已经尝试了在链接的 GitHub 示例中提出的不同解决方案,但是我无法找到一种方法在我的PreProcessInterceptor中注入用户。
谢谢。

1
有没有一种与JAX-RS无关的方式? - Setheron
1个回答

13

这是我最终找到的解决方案:

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

}
你可以通过我在问题中使用的记号来检索用户。
希望这能帮助到某些人。

1
有没有一种与JAX-RS无关的方式? - Setheron
2
PreProcessInterceptor在Resteasy 3.x中已被弃用。在3.x版本中,可以使用javax.ws.rs.container.ContainerRequestFilter来完成此操作。有关更多信息,请参见https://dev59.com/LmMm5IYBdhLWcg3wdu1V#17597783。 - Benoît Guérout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接