Jakarta ee 使用JNDI查找访问实现类

Jakarta ee 使用JNDI查找访问实现类,jakarta-ee,jndi,lookup,Jakarta Ee,Jndi,Lookup,我已经使用REST和EJB创建了一个J2EE项目 我的最终文件是webservice.war 我无法从WebLoginModule.java的JNDI上下文查找中找到EmployeeEJBImpl 这是我的文件结构 我正在尝试实现Jboss JASS安全性,我需要从数据库中检查用户名和密码。这是我的档案 public class WebLoginModule extends UsernamePasswordLoginModule{ private SecurityPrincipal s

我已经使用REST和EJB创建了一个J2EE项目

我的最终文件是webservice.war

我无法从WebLoginModule.java的JNDI上下文查找中找到EmployeeEJBImpl

这是我的文件结构

我正在尝试实现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(

您应该能够通过Web类中的注释字段注入EJB:

public class WebLoginModule extends UsernamePasswordLoginModule {

    @EJB
    private EmployeeEJBIf employeeService;

...

因为您的实现是唯一实现接口的实现,所以注入框架会自动使用您的实现。

好的,我在登录身份验证时需要这个EJB。所以我不能作为EJB注入。 我通过为url提供正确的路径解决了这个问题

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