【问题标题】:Servlet 3.0 AsyncContext and EJB @RolesAllowed in JBossJBoss 中的 Servlet 3.0 AsyncContext 和 EJB @RolesAllowed
【发布时间】:2023-04-07 05:44:01
【问题描述】:

阅读新的 Servlet 3.0 规范,我发现了 HttpServletRequest 的 startAsynch 方法,它声称以异步方式将正确的上下文信息传播到传递的可运行对象。

我在我的 servlet 的 doGet 方法中编写了这段代码:

@EJB  
private EJBManagerLocal manager;  

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {  
     if(request.getUserPrincipal() != null && request.isUserInRole("admin"))  
          //Method protected by @RolesAllowes("admin") annotation EJB-side  
          manager.verify();  

     final AsyncContext ctx = request.startAsync(request,response);  
     ctx.start(new Runnable(){  
          HttpServletRequest = (HttpServletRequest)ctx.getRequest();  
          if(request.getUserPrincipal() != null && request.isUserInRole("admin"))  
               //Method protected by @RolesAllowes("admin") annotation EJB-side  
               manager.verify();  
     });  
}  

当第一次调用 manager.verify() 时,在 AsyncContext 之外一切正常,但是当在调试中进入 Runnable 时,我可以看到,即使“if”被成功传递(所以主体已经正确传播到AsyncContext中的Runnable),当调用@RolesAllowed注解保护的EJB方法时,JBoss抛出一个异常说“方法验证的调用”是不允许的。

谁能帮帮我?

平台:JBoss EAP 6.2.0

编辑:JBoss EAP 6.3.0 中的相同行为

【问题讨论】:

    标签: java security servlets asynchronous jboss


    【解决方案1】:

    声称以异步方式传播正确的东西 传递给可运行文件的上下文信息。

    传播到 runnable 满足,您可以访问主体及其角色。

    if(request.getUserPrincipal() != null && request.isUserInRole("admin"))
    

    我认为 ejb 异步调用的最佳方法是使用 @Asynchronous 注释。

    另见:Asynchronous Method Invocation

    编辑:

    根据Java™ Servlet Specification Version 3.0

    Java 企业版功能,例如第 15.2.2 节,“Web 应用程序环境”第 15-174 页和第 15.3.1 节, “在 EJB™ 调用中传播安全身份”(第 15-176 页)是 仅适用于执行初始请求的线程或当 请求通过AsyncContext.dispatch 发送到容器 方法。 Java Enterprise Edition 功能可能可供其他用户使用 线程通过 AsyncContext.start(Runnable) 方法。

    在 jboss 论坛看到这个帖子,是一个类似的问题:Anonymous principal when invoking EJB from a thread inside a servlet

    【讨论】:

    • 嗨 Federico,感谢您的回答,是的,上下文已正确传播到可运行对象……但我问为什么从异步获取的 servlet 上下文具有主体和 EJB上下文没有......我也期待使用这种方法在上下文之间进行主要传播。不幸的是我不能使用异步 EJB,因为我使用线程来提高 UI 性能,而不是数据加载
    • @Antlia 我已经用更多信息更新了答案。我认为当从 AsyncContext.start(Runnable) 执行时,jboss 不会在可运行中实现传播。
    • 非常感谢您发布的文档,我搜索了整个网络并找到了任何东西。对于您在 JBoss 论坛上发布的链接,它是从我的帐户发布的:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2020-08-12
    相关资源
    最近更新 更多