在我的 Java EE 应用程序中,我声明了以下 RESTful 类
@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 粘贴到新选项卡中的网络浏览器中
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”身份登录。
那么,为什么调用者主体在第二种情况下会发生变化,是否有解决方案?