在我看来,只要您不尝试使用此用户对象构建类似会话的内容,此方法就是有效的.
作为answered here你可以使用@Context和@Provider,但这不完全是你想要的.
使用Resteasy Dispatcher可以直接为每个@Context注入一个类.
但是在这里你必须注册应该注入的Object.我认为这对请求范围的参数没有意义.
您可以做的是注入这样的提供者:
// Constructor of your JAX-RS Application
public RestApplication(@Context Dispatcher dispatcher) {
dispatcher.getDefaultContextObjects().put(UserProvider.class,new UserProvider());
}
// a resource
public Response getById(@Context UserProvider userProvider) {
User user = userProvider.get();
}
解决问题的其他方法:
>注册WebFilter,验证用户,包装ServletRequest并覆盖getUserPrincipal.然后,您可以从注入的HttpServletRequest访问UserPrincipal.
>实现一个实现ContainerRequestFilter的JAX-RS拦截器.将ContainerRequestContext.html#setSecurityContext与UserPrincipal一起使用,并将SecurityContext注入ResourceMethod-Parameter.
>实现更新Method-Parameters的CDI-Interceptor.
>为您的用户实现一个类which produces并通过CDI注入它.
我把例子推到了github.