2

単純なクライアントアプリケーションからGlassfishにデプロイされているセキュリティで保護されたリモートEJBを呼び出そうとしていますが、自分自身を認証できません。

クライアント:

    public void go() {
    try {
        Context ctx = getInitialContext();
        TestBeanRemote remote = (TestBeanRemote) ctx.lookup("java:global/ejbTest2/TestBean");
        String result = remote.testMe();
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private Context getInitialContext() throws NamingException, FileNotFoundException, IOException {
    Properties p = new Properties();
    InputStream stream = Runner.class.getResourceAsStream("jndi.properties");
    assert stream != null : "jndi.properties file not found";
    p.load(stream);
    return new InitialContext(p);
}

クライアント上のjndi.properties

java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
#optional. Defaults to localhost. Only needed if web server is running
#on a different host than the appserver
org.omg.CORBA.ORBInitialHost = localhost
#optional. Defaults to 3700. Only needed if target orb port is not 3700.
org.omg.CORBA.ORBInitialPort = 3700
java.naming.security.principal = test
java.naming.security.credentials = test

EJB

@Stateless(description = "A simple bean to learn the ins and outs of ejbs")
@DeclareRoles({ Roles.ADMIN, Roles.USER })
@RolesAllowed({})
public class TestBean implements TestBeanRemote {

    @Resource
    private SessionContext ejbContext;

    @Override
    @RolesAllowed(Roles.ADMIN)
    // @PermitAll
    public String testMe() throws Exception {
        return ejbContext.getCallerPrincipal().getName();
    }
}

sun-ejb-jar

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
    <security-role-mapping>
        <role-name>Admin</role-name>
        <group-name>Admin</group-name> <!-- As defined in container -->
    </security-role-mapping>
    <security-role-mapping>
        <role-name>User</role-name>
        <group-name>User</group-name> <!-- As defined in container -->
    </security-role-mapping>
    <enterprise-beans />
</sun-ejb-jar>

そして、ユーザーID「test」資格情報「test」グループリスト「Admin」を管理コンソールの「構成」>「default-config」>「セキュリティ」>「レルム」>「ファイル」>「ユーザーの管理」に追加しました。

これにより、org.omg.CORBA.NO_PERMISSIONを含むスタックトレースが生成され、PermitAllを使用すると、ejbContext.getCallerPrincipal()。getName()の結果が「匿名」になります。

ここで何が欠けていますか?

4

1 に答える 1

2

このスレッドは古いですが、答えは誰かを助けるかもしれません。コンテキストのプリンシパルとクレデンシャルは無視されます。ejbルックアップの前に、ProgrammaticLoginを使用してEJBにアクセスする必要があります。

System.setProperty("java.security.auth.login.config", "auth.conf");

ProgrammaticLogin pl = new ProgrammaticLogin();
pl.login("user", "password");

//Now lookup the EJB and then logout

pl.logout();

ご覧のとおり、ファイルを指す「java.security.auth.login.config」を設定する必要があります。

default {
com.sun.enterprise.security.auth.login.ClientPasswordLoginModule required debug=false; };

これは、サーバーで構成されているデフォルトのレルムを使用するか、「default」を「file」などのレルム名に置き換えてレルムを明示的に指定することもできることを示しています。

于 2012-10-09T11:50:56.913 に答える