【问题标题】:Why do I get a AuthenticationException when invoking the EJB remotely on TomEE?为什么在 TomEE 上远程调用 EJB 时会收到 AuthenticationException?
【发布时间】:2017-09-25 06:43:04
【问题描述】:

我有一个在 TomEE 7.0.3 服务器上运行的 ejb。顺便说一句,所有这些都在 Tomee 1.7.4 中工作。 我已经使用 tomcat-users.xml 文件设置了一组用户

<tomcat-users>
    <role rolename="admin" />
    <role rolename="admin-gui" />
    <role rolename="admin-script" />
    <role rolename="manager" />
    <role rolename="manager-gui" />
    <role rolename="manager-script" />
    <role rolename="manager-jmx" />
    <role rolename="manager-status" />
    <role rolename="tomee-admin" />
    <user
        name="admin"
        password="admin"
        roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status,tomee-admin" />
    <role rolename="tomcat" />
    <user
        name="tomcat"
        password="tomcat"
        roles="tomcat" />
    <user
        name="manager"
        password="manager"
        roles="manager" />
</tomcat-users>

我可以通过提供用户“admin”的凭据来访问 url http://127.0.0.1/tomee/ejb。 我的 server.xml 文件包含以下条目

<Resource auth="Container" description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase"
    pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />

    <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI resources 
            under the key "UserDatabase". Any edits that are performed against this UserDatabase 
            are immediately available for use by the Realm. -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase" />
    </Realm>

问题是当我尝试远程调用 ejb 时,我的 JNDI InitialContext 使用以下属性。

java.naming.factory.initial=org.apache.openejb.client.RemoteInitialContextFactory
java.naming.provider.url=http://127.0.0.1:8082/tomee/ejb
java.naming.security.principal=admin
java.naming.security.credentials=admin

以下是调用ejb的代码。

public static Object locateService(String serviceName) throws NamingException, IOException {
    InputStream in = ServiceLocator.class.getClassLoader().getResourceAsStream("servicelocator.properties");
    Properties p = new Properties();
    p.load(in);
    InitialContext ctx = new InitialContext(p);
    return ctx.lookup("PaymentManagerRemote");
}

如您所见,我提供了正确的用户名和密码,但出现以下异常

Apr 27, 2017 12:39:07 PM org.apache.openejb.client.EventLogger log
INFO: RemoteInitialContextCreated{providerUri=http://127.0.0.1:8082/tomee/ejb}
Exception in thread "main" javax.naming.AuthenticationException: Error while communicating with server: ; nested exception is: 
    javax.naming.AuthenticationException
    at org.apache.openejb.client.JNDIContext.authenticate(JNDIContext.java:381)
    at org.apache.openejb.client.JNDIContext.getInitialContext(JNDIContext.java:289)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:216)
    at co.uk.meghdoot.core.util.ServiceLocator.locateService(ServiceLocator.java:20)
    at co.uk.meghdoot.core.test.DeviceLocationTest.setUp(DeviceLocationTest.java:53)
    at co.uk.meghdoot.core.test.DeviceLocationTest.main(DeviceLocationTest.java:109)

有人能解释一下吗?

【问题讨论】:

    标签: java jakarta-ee ejb-3.0 apache-tomee tomee-7


    【解决方案1】:

    使用 tomcat-users.xml 假设您使用 UserDatabaseRealm 作为 server.xml 中的领域,这可能不是这种情况(未写在您的问题中)。这也假设身份验证是通过 servlet/tomcat 主干完成的。在您添加 tomee webapp 之前,默认情况下不会出现这种情况(您可以物理创建它并定义 ejbd servlet - 请参阅 http://tomee.apache.org/ejbd-transport.html - 执行 request.login() 的过滤器。

    使用ejbd协议tomee会自动使用tomee安全服务登录,默认依赖server.xml的第一个realm。

    【讨论】:

    • 我浏览了您发送的链接,我得到的错误是 AuthenticationException 而不是序列化错误。此外,对白名单和黑名单进行任何更改,我应该能够调用远程 ejb,默认黑名单应该可以正常工作。
    • 您是否也正确设置了 jaas(JVM 系统属性 + JAASRealm)?
    • 不,我没有设置 JAAS 领域。是新版tomee的要求吗?你能指出一些文档吗?
    • 不适用于新的 tomee,它在绕过 tomcat 层 (ejbd) 时总是依赖 JAAS。这篇文章解释了如何设置它rmannibucau.wordpress.com/2012/10/18/…
    • 浏览器可以看到localhost:8080/tomee/ejb这个页面,为什么远程客户端看不到呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2014-03-12
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多