【问题标题】:Accessing SecurityContextHolderAwareRequestWrapper instance?访问 SecurityContextHolderAwareRequestWrapper 实例?
【发布时间】:2012-07-04 19:43:16
【问题描述】:

我在 Vaadin 应用程序中使用 Spring 和 Spring-Security。

我想使用SecurityContextHolderAwareRequestWrapper.isUserInRole(...) 检查用户是否具有特定角色,但无法弄清楚如何获取对包装器的引用(我尝试使用@Autowired 注入它,注意:我没有手动配置它的实例因为我相信 DelegatingFilterProxy 已经在幕后这样做了)。

This 堆栈溢出问题为我提供了解决方案,但我无法发现如何正确访问或实例化包装器。

我的另一种选择是直接访问 SecurityContext 并根据链接的 SO 问题中的其他建议遍历 GrantedAuthorities。

我应该如何访问/实例化包装器?

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    它应该开箱即用,至少在 Spring Security 3.1 中(安全链中的每个 HttpServletRequest 都应该是 SecurityContextHolderAwareRequestWrapper 的实例)。

    servlet-api-provision attribute of <http> element 添加到堆栈:

    提供 HttpServletRequest 安全方法的版本,例如 isUserInRole() 和 getPrincipal() 是通过添加一个 SecurityContextHolderAwareRequestFilter bean 到堆栈。默认为 真的。

    过滤器SecurityContextHolderAwareRequestFilter很简单,只将HttpServletRequest包裹在SecurityContextHolderAwareRequestWrapper中:

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        chain.doFilter(new SecurityContextHolderAwareRequestWrapper(
                (HttpServletRequest) req, rolePrefix), res);
    }
    

    (注意bug #SEC-1943 - 过滤器分配给别名SERVLET_API_SUPPORT_FILTER 的名称错误。)

    【讨论】:

    • 嗯,我使用的是 3.0.2,无法升级。
    • 刚刚检查了 3.0.X 文档和代码 - should be the same。您可以将<http servlet-api-provision="true" ... />` 显式添加到您的安全 bean 配置中吗?
    • 酷,所以我的应用程序是一个 HttpServletRequestListener 并且我在 onRequestStart 上收到对 HttpServletRequest 的引用;通过它我可以检查/转换到 SecurityContextHolderAwareRequestWrapper 并进行适当的调用。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2014-01-09
    • 2019-12-14
    相关资源
    最近更新 更多