自定义登录模块无法传播安全性

时间:2014-07-06 01:16:51

标签: java-ee spring-security glassfish jaas

我在 Glassfish 服务器上运行2 EJB ,第一个从独立客户端(Eclipse)调用,调用成功到第一个ejb的功能,它将一条消息打印到控制台,然后尝试使用custom JAAS login module验证用户,验证是成功,但是如果在登录后我调用第二个受{{ 1}},第一个EJB无法使用权​​限错误调用第二个EJB的方法。

第一个EJB方法

@RolesAllowed

第二个EJB

public void testFunction() throws LoginException {
        System.out.println("With in test function"); // successfully seen on console output
        LoginContext loginContext = null;
        try {
            MazCallBack lc = new MazCallBack("testUser", "testPassword");
            loginContext = new LoginContext("SampleLoginModule", lc);
            loginContext.login(); //success
            for (Principal p :loginContext.getSubject().getPrincipals()) 
                System.out.println(" Subject Principal "+p.getName()); //successfully seen on console output

            SecondEjbRemote remote = (SecondEjbRemote) new InitialContext().lookup("ejb/SecondEjb");
            remote.hello();//exception here
        } catch (NamingException e) {
            e.printStackTrace();
        }
        loginContext.logout();
    }

自定义登录模块 以下是Login模块的一些部分。如果需要,我会添加更多。

@Stateless(name="SecondEjb ", mappedName = "ejb/SecondEjb")
@Remote(SecondEjbRemote.class)
@Local(SecondEjbLocal.class)
@RolesAllowed({"User"})
public class SecondEjb implements SecondEjbRemote , SecondEjbLocal {
@Override
    public void hello() {
        System.out.println("Solute: Hi man how are you");
    }
}

用户100%通过上面的示例(部分显示)登录模块获得认证。由于我public class SampleLoginModule implements LoginModule { public boolean commit() throws LoginException { if (succeeded == false) { return false; } else { //user come here and add them and can be seen on console. //in EJB 1, I have checked the subject right after the login and these //principal can be seen over there. System.out.println("Setting username in principal : " + username); subject.getPrincipals().add(new PrincipalImpl(username)); subject.getPrincipals().add(new PrincipalImpl("User")); Group group = new Group("User"); subject.getPrincipals().add(group); .............. commitSucceeded = true; return true; } } } 之后的第一个EJB see主题能够principals LoginContext

异常

login

1 个答案:

答案 0 :(得分:0)

尝试将用户凭据作为属性传递给InitialContext,这是为查找远程EJB而创建的:

public void testFunction() throws LoginException {
  System.out.println("With in test function"); // successfully seen on console output
  LoginContext loginContext = null;
  try {
    MazCallBack lc = new MazCallBack("testUser", "testPassword");
    loginContext = new LoginContext("SampleLoginModule", lc);
    loginContext.login(); //success
    for (Principal p : loginContext.getSubject().getPrincipals()) 
      System.out.println(" Subject Principal "+p.getName()); //successfully seen on console output
      Hashtable env = new Hashtable(); 
      env.put(Context.SECURITY_PRINCIPAL, loginContext.getSubject().getPrincipals().getName()); 
      Context ic = new InitialContext (env);
      SecondEjbRemote remote = (SecondEjbRemote) ic.lookup("ejb/SecondEjb");
      remote.hello();
  } catch (NamingException e) {
      e.printStackTrace();
  }
  loginContext.logout();
}