JAAS 和 Web 服务授权:获取登录用户

JAAS and web service authorization : get logged user

我有一个通过 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 注入 [=13] 的内置解决方案=] 进入 EJB,并从中获取 userPrincipal:

@Context
private SecurityContext securityContext;

在你的方法中:

Principal principal = securityContext.getUserPrincipal();

另请参阅:

Baeldung's post on Java EE 8 Security API