2014-06-27 115 views
1

我試圖從獨立應用程序(在Eclipse中運行)訪問安全EJB(位於Glassfish)。 我有一個JAAS登錄模塊工作正常使用任何web項目(如果配置在web.xml中與<realm-name>MyRealm</realm-name>等),現在我要保護我的EJB具有相同的登錄模塊(如我的自定義登錄模塊的認證用戶,並添加到認證背景,所以我必須通過鍵入@RolesAllowed({"User"})從獨立Java應用程序訪問Glassfish上的安全EJB

這裏用於爲EJB同樣是只包含1 EJB我的EJB項目。

@Stateless(name="HiEjb", mappedName = "ejb/HiEjb") 
@RolesAllowed({"User"}) 
@Remote(HiEjbRemote.class) 
@Local(HiEjbLocal.class) 
public class HiEjb implements HiEjbRemote, HiEjbLocal { 

    @Override 
    public String getHello() { 
     return "3D Studio Max"; 
    } 
} 

請注意,我能夠從獨立的客戶端訪問該EJB如果我刪除@RolesAllowed({"User"})

這裏是我的獨立客戶端代碼(一個簡單的Java類從Eclipse中運行)和auth.conf內容

default { com.maz.test.MyCustomeLoginModule required; }; 

,這裏是主要功能

public static void main(String[] args) throws Exception { 

     String authFile = "D:/auth.conf"; 
     System.setProperty("java.security.auth.login.config", authFile); 
     ProgrammaticLogin programmaticLogin = new ProgrammaticLogin(); 

     programmaticLogin.login("zahoor", "abc123".toCharArray()); //here on this line exception occurs. 


     Properties p = new Properties(); 
     //p.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); 
     p.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); 
     p.setProperty("java.naming.provider.url", "hostname:jnpport"); 
     p.setProperty(Context.SECURITY_PRINCIPAL, "zahoor"); 
     p.setProperty(Context.SECURITY_CREDENTIALS, "abc123"); 
     InitialContext ic = new InitialContext(p); 

     final String jndiName = "ejb/HiEjb"; 
     HiEjbRemote testEjb = (HiEjbRemote) ic.lookup(jndiName); 
     System.out.println("Got the reference of Remote Interface"); 
     System.out.println("Resulte from EJB::->"+ testEjb.getHello()); 

     programmaticLogin.logout(); 
    } 

當我運行上面的代碼中,我看到以下例外。

programmaticLogin.login("zahoor", "abc123".toCharArray()); 

上面的行發生異常。

Jun 27, 2014 6:59:20 PM com.sun.appserv.security.AppservPasswordLoginModule extractCredentials 
SEVERE: SEC1105: A PasswordCredential was required but not provided. 
Jun 27, 2014 6:59:20 PM com.sun.appserv.security.ProgrammaticLogin login 
SEVERE: SEC9050: Programmatic login failed 
com.sun.enterprise.security.auth.login.common.LoginException: javax.security.auth.login.LoginException: No credentials. 
    at com.sun.enterprise.security.auth.login.LoginContextDriver$9.run(LoginContextDriver.java:889) 
    at com.sun.enterprise.security.common.AppservAccessController.doPrivileged(AppservAccessController.java:61) 
    at com.sun.enterprise.security.auth.login.LoginContextDriver.doClientLogin(LoginContextDriver.java:881) 
    at com.sun.appserv.security.ProgrammaticLogin$1.run(ProgrammaticLogin.java:184) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.appserv.security.ProgrammaticLogin.login(ProgrammaticLogin.java:168) 
    at com.sun.appserv.security.ProgrammaticLogin.login(ProgrammaticLogin.java:239) 
    at ClientTest.main(ClientTest.java:51) 
Caused by: javax.security.auth.login.LoginException: No credentials. 
    at com.sun.appserv.security.AppservPasswordLoginModule.extractCredentials(AppservPasswordLoginModule.java:331) 
    at com.sun.appserv.security.AppservPasswordLoginModule.login(AppservPasswordLoginModule.java:140) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at javax.security.auth.login.LoginContext.invoke(LoginContext.java:769) 
    at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186) 
    at javax.security.auth.login.LoginContext$4.run(LoginContext.java:683) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680) 
    at javax.security.auth.login.LoginContext.login(LoginContext.java:579) 
    at com.sun.enterprise.security.auth.login.LoginContextDriver$9.run(LoginContextDriver.java:887) 
    ... 7 more 

問題:

  1. 是否有可能訪問來自獨立的客戶端安全EJB(部署在GlassFish)?
  2. 在EJB項目(@RolesAllowed({"User"})除外)中是否需要任何配置來確定要使用哪個登錄模塊?如何配置。就像我知道web項目可以通過提供認證配置通過web.xml進行保護。
  3. 通過指定default { com.maz.test.MyCustomeLoginModule required; };auth.conf做任何事情或不做,我假設它告訴ProgrammaticLogin使用MyCustomeLoginModule進行身份驗證。

回答

相關問題