我有一个受 Shibboleth(SSO 实现)保护的 Jersey API。 Shibboleth 将登录用户的 ID 放入请求属性中。在后端,我使用 Shiro 进行授权。 Shiro 希望了解登录用户,以便加载权限。
从请求属性中获取 userId 并将其放入 Shiro 的正确方法是什么?现在,我正在尝试的是:
@Provider
public final class ShiroLoginFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest request;
@Override
public void filter(final ContainerRequestContext requestContext)
throws IOException {
final String userId = (String) this.request.getAttribute("nameid");
final Subject subject = SecurityUtils.getSubject();
subject.login(new LocusAuthenticationToken(userId));
}
}
不幸的是,由于JERSEY-1960 ,我无法将请求上下文注入(inject)过滤器。每个用户都需要“登录”才能加载权限。我不想在 API 的每个方法中重复登录代码。我也不允许使用 web.xml 过滤器(我的老板)。我这里有什么好的选择吗?
最佳答案
您还应该能够通过 ContainerRequestContext#getProperty 直接从 ContainerRequestContext
获取 ServletRequest
属性如该方法的 JavaDoc 中所述:
In a Servlet container, the properties are synchronized with the
ServletRequest
and expose all the attributes available in theServletRequest
. Any modifications of the properties are also reflected in the set of properties of the associatedServletRequest
.
注意:自 Jersey 2.4(2013 年 10 月发布)以来,注入(inject) HttpServletRequest
应该可以按预期工作。
关于jersey - 读取 Jersey ContainerRequestFilter 中的请求属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691753/