简体   繁体   English

从同一EAR内部的WAR调用EJB

[英]Calling EJB from a WAR inside the same EAR

I am using Glassfish 4. And I have an EAR which has a WAR and a JAR (with the EJBs). 我正在使用Glassfish4。并且我有一个EAR,它具有WAR和JAR(以及EJB)。

I want to call the EJBs from my WAR but am not really sure if I need to use Local or Remote interfaces. 我想从WAR中调用EJB,但不确定是否需要使用本地或远程接口。

Inside my JAR , my Bean looks like this : 在我的JAR文件中 ,我的Bean看起来像这样:

@Stateless
public class Test implements TestLocal {

    @Override
    public void testing() {
    }
}

And my local : 而我的本地人:

@Local
public interface TestLocal {
    void testing();
}

Inside my WAR I have a web service and it looks like this : 在我的WAR内部,我有一个Web服务,它看起来像这样:

@WebService(serviceName = "TestWS")
public class TestWS {

    private @EJB TestLocal testBean;

    @WebMethod(operationName = "test")
    public String test() {
    testBean.test();
    }
}

Both of these are packaged into an EAR . 两者都包装在EAR中

When I call my WebService method I get an AccessLocalException : 当我调用WebService方法时,我得到一个AccessLocalException

Warnung: javax.ejb.AccessLocalException: Client not authorized for this invocation at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:210) 警告:javax.ejb.AccessLocalException:在com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895)处未对此调用授权的客户端210)

Firstly : 首先 :

  • Is this the correct way to call the EJB. 这是调用EJB的正确方法吗? Can a WAR inside an EAR use Local interfaces from an included JAR? EAR内的WAR可以使用随附的JAR中的本地接口吗?
  • If so then does anyone know what I am doing wrong? 如果是这样,那么有人知道我在做什么错吗? Do I need to setup some kind of security configuration? 我需要设置某种安全配置吗?

To look up a remote EJB, it must have a remote interface exposed. 要查找远程EJB,它必须公开一个远程接口。 Include that remote interface into your war. 在您的战争中加入该远程接口。

The GlassFish documentation has an entry for this error: GlassFish文档中有一个针对此错误的条目:

javax.ejb.AccessLocalException: Client Not Authorized Error

Description 描述

Role-mapping information is available in Sun-specific XML (for example, sun-ejb-jar.xml), and authentication is okay, but the following error message is displayed: 角色映射信息在特定于Sun的XML中(例如,sun-ejb-jar.xml)可用,并且可以进行身份​​验证,但是会显示以下错误消息:

[...INFO|sun-appserver-pe8.0|javax.enterprise.system.container.ejb|...|
javax.ejb.AccessLocalException: Client not authorized for this invocation.
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:...
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(...)

Solution

Check whether the EJB module (.jar) or web module (.war) is packaged in an application (.ear) and does not have role-mapping information in application level, Sun-specific, sun-application.xml. 检查EJB模块(.jar)或Web模块(.war)是否打包在应用程序(.ear)中,并且在特定于Sun的应用程序级别sun-application.xml中没有角色映射信息。 For any application (.ear), security role-mapping information must be specified in sun-application.xml. 对于任何应用程序(.ear),必须在sun-application.xml中指定安全角色映射信息。 It is acceptable to have both module-level XML and application-level XML. 同时具有模块级XML和应用程序级XML是可以接受的。

@Remote is usefull if you deploy separately your jar which contain your EJBs on a different server, for example. 例如,如果单独部署包含EJB的jar到其他服务器上,则@Remote很有用。

There, war and jar are in the same ear so you just have to use the Local annotation. 在那里,战争和震撼在同一耳朵,因此您只需要使用Local注释即可。

Tips : since EJB 3.1 interfaces are not required, you can use @LocalBean directly on your "Test" class and remove the TestLocal interface. 提示:由于不需要EJB 3.1接口,因此可以直接在“ Test”类上使用@LocalBean并删除TestLocal接口。

To call a ejb method into a class which is your war, you firstly have to create a link between war and jar. 要将ejb方法调用为您的战争类,您首先必须在war和jar之间创建链接。 Being in the same ear is not enough. 同一个耳朵是不够的。 If you use Maven, you can simply add the jar reference into your dependencies in the pom of your war. 如果使用Maven,则只需将jar引用添加到战争pom中的依赖项中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM