2015-09-24 89 views
0

我已經使用REST和EJB創建了一個J2EE項目。使用JNDI查找訪問實現類

我的最終文件是webservice.war

我無法從JNDI上下文查找從WebLoginModule.java

定位EmployeeEJBImpl這是我的文件結構

enter image description here

我試圖實現Jboss JASS安全性,我需要從數據庫中檢查用戶名密碼。這是我的文件

public class WebLoginModule extends UsernamePasswordLoginModule{ 

    private SecurityPrincipal sp = null; 
    Subject sub ; 

    public boolean logout() throws LoginException 
    { 
     sub.getPrincipals().remove(sp); 
     System.out.println("Logging out"); 
     return super.logout(); 
    } 


    @Override 
    protected boolean validatePassword(String username, String password) 
    { 
     Principal p = this.getIdentity(); 
     sub = new Subject(); 

     System.out.println("password(as param) : "+password); 
     if(password==null){ 
      System.out.println("password empty"); 
      password = username; 
      username = p.getName(); 
     }else{ 
      //Do nothing 
     } 

      if(p instanceof SecurityPrincipal) {      

       try { 
        sp = (SecurityPrincipal)p; 
        sp.setUsername(p.getName()); 
        sp.setPassword(password);    
        sp.setSubj(sub); 
        sp.setColRole(null);; // TODO: fix this. 
        System.out.println("username: "+username); 
        return isValidUser(username, password); 
       } 
       catch(Exception e) { 
        e.printStackTrace(); 

       } 
      } 

     return false; 
    } 

    @Override 
    protected String getUsersPassword() throws LoginException { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public boolean isValidUser(String username, String password) { 
     boolean result=false; 
     try{ 
      final Hashtable jndiProperties = new Hashtable(); 
      jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); 
      final Context context = new InitialContext(jndiProperties); 
      System.out.println("getting value"); 
      EmployeeEJBIf lif = (EmployeeEJBIf) context.lookup("java:global/!EmployeeEJBIf"); 
      System.out.println("loading data"); 
      password=hashPassword(password); 
     }catch(Exception e){ 
      e.printStackTrace(); 
      return false; 
     } 
     return result; 
    } 


    @Override 
    protected Group[] getRoleSets() throws LoginException { 
     try {   
      Group callerPrincipal = new SimpleGroup("CallerPrincipal"); 
      Group roles = new SimpleGroup("Roles"); 
      Group[] groups = {roles,callerPrincipal};   
      roles.addMember(new SecurityPrincipal("SecurityAdmin")); 
      callerPrincipal.addMember(sp); 
      return groups; 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
      throw new LoginException(e.getMessage()); 
     } 

    } 
    private String hashPassword(String password) { 
      String hashword = null; 
      try { 
       MessageDigest md5 = MessageDigest.getInstance("MD5");    
       md5.update(password.getBytes()); 
       BigInteger hash = new BigInteger(1, md5.digest()); 
       hashword = hash.toString(16); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      }   
      return hashword; 
    } 

例外:

獲得價值

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) javax.naming.NameNotFoundException: !EmployeeEJBIf -- service jboss.naming.context.java.global.!EmployeeEJBIf 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.naming.InitialContext.lookup(Unknown Source) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.isValidUser(WebLoginModule.java:88) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at com.webservice.security.WebLoginModule.validatePassword(WebLoginModule.java:64) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at org.jboss.security.auth.spi.UsernamePasswordLoginModule.login(UsernamePasswordLoginModule.java:267) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at java.lang.reflect.Method.invoke(Unknown Source) 

11:15:13,295 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invoke(Unknown Source) 

11:15:13,311 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.access$000(Unknown Source) 

11:15:13,312 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext$4.run(Unknown Source) 

11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext$4.run(Unknown Source) 

11:15:13,313 ERROR [stderr] (http--192.168.1.153-8080-5) at java.security.AccessController.doPrivileged(Native Method) 

11:15:13,314 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.invokePriv(Unknown Source) 

11:15:13,315 ERROR [stderr] (http--192.168.1.153-8080-5) at javax.security.auth.login.LoginContext.login(

回答

0

你應該能夠通過註釋字段在Web類注入EJB:

public class WebLoginModule extends UsernamePasswordLoginModule { 

    @EJB 
    private EmployeeEJBIf employeeService; 

... 

因爲你的實現是唯一一個實現接口的自動注入框架ly使用你的實現。

0

那麼我需要這個EJB,而登錄身份驗證。所以我不能注入EJB。 我解決了問題,爲網址提供了正確的路徑,例如

context.lookup("java:global/webservice/EmployeeEJBImpl!com.webservice.service.EmployeeEJBIf");