我有一个通过 JAAS 登录的 JSF 应用程序。 效果很好。
现在,我的页面调用 REST Web 服务。我需要知道是谁发出了请求。
在请求的 header 中,我有:
Cookie = JSESSIONID=XBHZuYnzgkGyQSR8kBLNSks_s7nuXAMli7Gp-9Mn.dlicitra; _ga=GA1.1.1590792307.1560863707
Web 服务是在无状态 EJB 中实现的。方法是:
@Path(value = "myservice/{id}")
@GET
@Produces(value = "application/json")
public List<Records> getServices(
@HeaderParam(value = "Cookie") String cookie,
@PathParam(value = "id") Long id){
return ... ;
}
如何从 cookie 字符串中获取已登录的用户?
最佳答案
正如评论中所解释的,我不会使用解析或解码 cookie 的 SessionId 来进行破坏,而是使用 Java EE 的安全 API 内置解决方案注入(inject) SecurityContext
进入 EJB,并从中获取 userPrincipal:
@Context
private SecurityContext securityContext;
在你的方法中:
Principal principal = securityContext.getUserPrincipal();
另请参阅:
关于java - JAAS和Web服务授权: get logged user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521426/