jboss Resteasy 参数注入@Context [英] jboss Resteasy parameter injection with @Context

查看:27
本文介绍了jboss Resteasy 参数注入@Context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

谢谢

推荐答案

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

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.

希望对某人有所帮助.

这篇关于jboss Resteasy 参数注入@Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆