我的安全 Glassfish 3.1.2 有一个非常烦人的问题。当我从我的独立客户端连接 2 个(或更多)用户时,当我尝试从我的 EJB 获取调用者主体时,我总是拥有最后一个连接的用户:
sessionContext.getCallerPrincipal().getName();
你能帮我解决这个问题吗?
这是我的客户:
<pre>
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
connectUser("userA", "pwdB");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
connectUser("userB", "pwdB");
}
}).start();
}
public static void connectUser(String userName, String password) {
try {
ProgrammaticLogin login = new ProgrammaticLogin();
login.login(userName, password.toCharArray());
Context context = new InitialContext();
MyServiceRemote myService = (MyServiceRemote) context.lookup("MyService");
String remoteCaller = myService.getUserName();
System.out.println("Local is (" + userName + "), Remote is (" + remoteCaller + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
</pre>
这是我的 EJB:
<pre>
@Stateless(mappedName = "MyService", name="MyServiceBean")
public class MyServiceBean implements MyServiceRemote{
@Resource
private SessionContext sessionContext;
public MyServiceBean () {
super();
}
public String getUserName() {
return sessionContext.getCallerPrincipal().getName();
}
}
</pre>