【问题标题】:How to handle 404 with Spring Security?如何使用 Spring Security 处理 404?
【发布时间】:2011-11-16 05:32:49
【问题描述】:

我只找到了与 c# 相关的主题,如果我错过了资源,请不要怪我。

看起来有点像

/project/blablaentered 并满足于 404。

实际上我只想在抛出 404 页面时指定我自己的页面。

我的安全 xml:

<security:http auto-config="true" use-expressions="true" >
    <security:form-login login-processing-url="/static/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t"  default-target-url="/home"/>
    <security:intercept-url pattern="/home" access="isAuthenticated()" />
    <security:intercept-url pattern="/home/try" access="hasRole('ROLE_EDITOR')"/>
    <security:access-denied-handler error-page="/accessDenied"/> 
       </security:http>

更新:请关注解决方案:Custom 404 using Spring DispatcherServlet

【问题讨论】:

    标签: spring-security http-status-code-404


    【解决方案1】:

    最简单的方法可能是在web.xml 中启用error-page 元素,只要您不介意它是一个普通的JSP(即没有控制器)。这样,DispatcherServlet 之外的 URL 将从您的 servlet 容器生成 404 将遵循与 Spring 无法根据您的配置映射到控制器的任何 URL 相同的路径。

    如果这还不够好,您可以为特定控制器定义 @Exception 方法,或使用 HandlerExceptionResolver

    【讨论】:

    • 能否提供示例代码?更新:这样做了,按照我没有重点的链接,显然我提供的链接有效,对不起。
    【解决方案2】:

    你需要添加类:

    import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
    
    
    public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
    
        // Do nothing
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-16
      • 2020-01-30
      • 2011-05-03
      • 2016-11-28
      • 2011-05-10
      • 1970-01-01
      • 2014-07-20
      • 2011-05-10
      • 2014-03-24
      相关资源
      最近更新 更多