jersey - 读取 Jersey ContainerRequestFilter 中的请求属性

标签 jersey

我有一个受 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 the ServletRequest. Any modifications of the properties are also reflected in the set of properties of the associated ServletRequest.

注意:自 Jersey 2.4(2013 年 10 月发布)以来,注入(inject) HttpServletRequest 应该可以按预期工作。

关于jersey - 读取 Jersey ContainerRequestFilter 中的请求属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691753/

相关文章:

java - 我如何使用 JDK 1.7 运行 jersey 2.26?

java - 在Tomcat中运行时, Atmosphere 广播无法到达客户端

java - 不应使用 "com.sun.*"和 "sun.*"包中的类 Sonar issue for Jersey client

java - 我对这种骡子 Jersey 流程做错了什么?

java - 如何在请求过滤器中获取表单参数

java - 尝试使用 Postman to REST 进行 POST 时出现异常

java - REST Web 服务所需的 jar

Java Guice : If you inject the same dependency multiple times, 是该依赖项注入(inject)的同一个实例吗?

java - MULTIPART_FORM_DATA : No injection source found for a parameter of type public javax. ws.rs.core.Response

java - Dropwizard 注册两个类/客户端