Session getCallerPrincipal.getName()返回;“匿名”;
在我的JavaEE应用程序中,我声明了以下restful类Session getCallerPrincipal.getName()返回;“匿名”;,session,jakarta-ee,extjs,sessioncontext,Session,Jakarta Ee,Extjs,Sessioncontext,在我的JavaEE应用程序中,我声明了以下restful类 @Path("userProfile") @Stateless public class UserProfileRestful { @Resource private SessionContext sessionContext; @Path("userName") @GET @Produces(MediaType.Text_Palin) public String findCurrentUserN
@Path("userProfile")
@Stateless
public class UserProfileRestful {
@Resource
private SessionContext sessionContext;
@Path("userName")
@GET
@Produces(MediaType.Text_Palin)
public String findCurrentUserName(){
return sessionContext.getCallerPrincipal.getName();
}
}
旨在通过任何客户端获取当前登录用户的用户名
使用用户名“myuser”登录后,我将以下url粘贴到新选项卡中的web浏览器
http://localhost:8080/MyApp/rest/userProfile/userName
显示的响应与预期一样:“myuser”
现在,我尝试从另一个执行Ajax请求的ExtJS应用程序(不需要身份验证)中获取该用户名,如下所示:
Ext.Ajax.Request({
url : "http://localhost:8080/MyApp/rest/userProfile/userName",
success : function(response,options){
alert('username : ' + response.responseText);
},
failure : function(){
alert('request failed');
}
});
警报结果为“匿名”,即使在同一会话中我仍然以“myuser”身份登录
那么,为什么在第二种情况下调用方主体会发生变化,有解决方案吗 好了,完成了
这个想法是
sessionContext.getCallerPrincipal.getName()
取决于调用方(客户机)的上下文,实际上它运行在哪个应用程序服务器上
我的ExtJS应用程序(客户端)首先在Jitty服务器上运行,而javaee应用程序(服务器端)在GlassFish上运行
问题在于两个不同的应用程序服务器和两个不同的上下文
显然……在我将这两个应用程序部署到glassfish上之后,一切都正常了