【问题标题】:Both isAnonymous() and isAuthenticated() return false on error pageisAnonymous() 和 isAuthenticated() 在错误页面上都返回 false
【发布时间】:2012-08-13 12:57:33
【问题描述】:

我有一个错误页面,当返回 404 响应状态时会显示该页面。 这个页面是通过模板机制生成的(我使用瓷砖);在这个模板中,我有一个包含类似内容的标题:

    <sec:authorize access="isAnonymous()">
        blabla
    </sec:authorize>
    <sec:authorize access="isAuthenticated()">
        blibli
    </sec:authorize>

因此,根据用户是否经过身份验证,它会显示 blibli 或 blabla。此代码适用于使用此模板的所有页面,但我的 404 页面除外!它什么都不显示!

有什么想法吗??

【问题讨论】:

    标签: java spring jsp spring-security


    【解决方案1】:

    我敢打赌,问题在于您如何在 web.xml 中定义 filter-mapping。最常见的配置是:

    <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    这会将过滤器映射到所有 URL,但仅当它们通过 REQUEST 方法访问时。此过滤器未捕获的所有其他情况(如INCLUDEFORWARDERROR)。所以要将过滤器绑定到ERROR 请求,将其定义为

    <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
    
            <!-- apply Spring Security authentication to error-pages -->
            <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    

    试试看。如果不起作用,请添加 &lt;dispatcher&gt;INCLUDE&lt;/dispatcher&gt;,因为 Tiles 可能通过这种方式包含页面。

    另见:

    【讨论】:

    • 确实! &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt; 工作!我今天学到了一些东西:D
    【解决方案2】:

    对于那些寻找 java 配置的人:

    private void registerSpringSecurityFilterChain(ServletContext servletContext) {
      FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter(
            BeanIds.SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy());
      springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
      springSecurityFilterChain.addMappingForUrlPatterns(EnumSet.of(DispatcherType.ERROR), false, "/*");
    
    }
    

    【讨论】:

      【解决方案3】:

      如果这个 404 页面超出了您的安全 URL,那么 isAnonymous() 和 isAuthenticated() 都将返回 false 并且不会呈现任何内容。

      【讨论】:

        猜你喜欢
        • 2014-04-28
        • 2015-12-30
        • 2013-10-15
        • 1970-01-01
        • 2021-01-28
        • 2021-01-24
        • 2020-10-07
        • 2021-02-21
        相关资源
        最近更新 更多