0

在我的 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”身份登录。

那么,为什么调用者主体在第二种情况下会发生变化,是否有解决方案?

4

1 回答 1

1

嗯,完成了……

这个想法是

   sessionContext.getCallerPrincipal.getName()

取决于调用者(客户端)的上下文,实际上取决于它正在运行哪个应用程序服务器。

我的 ExtJS 应用程序(客户端)首先在 Jitty 服务器上运行,而 Java EE 应用程序(服务器端)在 GlassFish 上运行。

两个不同的应用程序服务器和两个不同的上下文是问题所在。

显然......在我在 glassfish 上部署了这两个应用程序之后,一切都很顺利。

于 2014-08-27T16:05:19.743 回答