在泽西过滤器中注入EJB [英] Inject EJB in Jersey filter

查看:130
本文介绍了在泽西过滤器中注入EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,Jersey JAX-RS作为后端,AngularJS作为前端;我需要一种身份验证,所以在每个请求中,我发送一个应该由后台验证的令牌。为此,我决定创建一个泽西过滤器,将寻找该令牌,然后调用我的AuthenticateService来检查用户是否可以进行身份​​验证。
然后由 @RolesAllowed 注释管理授权。



这是我的问题:我不能在Jersey过滤器中注入EJB,因为它与资源一起工作很好,但是使用过滤器,服务始终保持 null



任何想法怎么欺骗?
谢谢



过滤代码:

  @Provider 
@Priority(Priorities.AUTHORIZATION)
public class AuthenticationFilter实现ContainerRequestFilter {

@EJB(name = AuthenticationService.LOOKUP_NAME)
private AuthenticationService authService;

@Override
public void filter(ContainerRequestContext requestContext)throws IOException {
/ **
*获取头参数
* /
String userIdStr = requestContext.getHeaderString(SecurityConsts.HEADER_ID_PARAMETER);
int userId = 0;

if(userIdStr!= null&!userIdStr.isEmpty()){
userId = Integer.parseInt(userIdStr);
}

String securityToken = requestContext.getHeaderString(SecurityConsts.HEADER_TOKEN);

用户user = null;

/ **
*如果存在令牌,请尝试验证用户
* /
如果(securityToken!= null&!securityToken)。 isEmpty()){
// NullPointerException在这里发生
user = authService.authenticateWithToken(userId,securityToken);
}

/ **
*设置正确的安全上下文
* /
requestContext.setSecurityContext(new ConfiguratorSecurityContext(user));
}

}


解决方案


JAX-RS 2.0不支持将EJB注入JAX-RS组件
(提供者,资源)。


但是有一些选择来解决这个问题。




  • 您可以尝试切换到CDI,例如将您的服务变为 @ManagedBean 并使用 @Inject


  • 您可以尝试通过上下文查找获得您的服务,如下所示:


      InitialContext context = new InitialContext(); 
    context.lookup(java:comp / env / ejb / YourBean);



  • 您还可以尝试使用 @Stateless 所以它由容器管理。




你可以找到相关的JIRA here here



另见:




I am currently developing an application with Jersey JAX-RS as backend and AngularJS as frontend ; I need a sort of authentication, and so with every request I send a token that should be verify by the backend. For this, I decided to create a Jersey filter that will look for that token, and then call my AuthenticateService to check if the user can be authenticated. Authorization is then managed by @RolesAllowed annotation.

Here is my problem : I can't inject an EJB inside a Jersey filter, strangly because it works great with resources.. But with a filter, the service always stays null

Any idea how to trick it ? Thanks

Filter code :

@Provider
@Priority( Priorities.AUTHORIZATION )
public class AuthenticationFilter implements ContainerRequestFilter {

    @EJB( name=AuthenticationService.LOOKUP_NAME)
    private AuthenticationService authService;

    @Override
    public void filter( ContainerRequestContext requestContext ) throws IOException {
        /**
         * Get headers parameters
         */
        String userIdStr = requestContext.getHeaderString( SecurityConsts.HEADER_ID_PARAMETER );
        int userId = 0;

        if( userIdStr != null && !userIdStr.isEmpty() ) {
            userId = Integer.parseInt( userIdStr );
        }

        String securityToken = requestContext.getHeaderString( SecurityConsts.HEADER_TOKEN );

        User user = null;

        /**
         * If a token is present, try to authenticate the user
         */
        if( securityToken != null && !securityToken.isEmpty() ) {
                    // NullPointerException happens here
            user = authService.authenticateWithToken( userId, securityToken );
        }

        /**
         * Set correct security context
         */
        requestContext.setSecurityContext( new ConfiguratorSecurityContext( user ) );
    }

}

解决方案

This is a more or less know problem.

JAX-RS 2.0 does not support injection of EJBs into JAX-RS components (providers, resources).

But there are some options to solve this.

  • You can try switching to CDI, e.g. turning your service into a @ManagedBean and using @Inject.

  • You can try to get your service via context lookup, something like this:

    InitialContext context = new InitialContext();
    context.lookup("java:comp/env/ejb/YourBean");
    

  • You can also try to annotate your filter with @Stateless so it gets managed by the container.

You can find related JIRAs here and here.

See also:

这篇关于在泽西过滤器中注入EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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