使用转发的Spring Security错误页面

我想显示有关身份验证拒绝的自定义错误页面(我正在使用预先身份验证的secnario),并且还希望从Spring Security 3.0.X访问拒绝

了解我们可以使用以下方法执行此操作:

        
        
    

<beans:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <beans:property name="loginFormUrl" value="/error.jsp"/>
</beans:bean>

<beans:bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
    <beans:property name="errorPage" value="/error.jsp"/>
</beans:bean>

但是这样做会导致重定向,而不是前进到错误页面.无论如何都要执行到错误页面(以便我们可以在请求中设置一些属性)

谢谢

最佳答案
在我的security-context.xml中,对于身份验证失败,我确实这样做(请注意authentication-failure-url属性):

    <security:form-login login-page="/auth/login"
        authentication-failure-url="/auth/login?error=true"
        default-target-url="/mvc/home"
        always-use-default-target="true" />

对于拒绝访问,我使用以下命令:

    <security:access-denied-handler error-page="/auth/access-denied"/>

< security:http use-expressions =“ true”>中的两个标签.对我来说,工作就像一个咒语,我不知道为什么当Spring提供易于使用的出色标签时,您为什么尝试以这种方式进行配置.

我不知道它是否回答您的问题,希望对您有所帮助.

编辑:

使用上面提供的配置,意味着您在后台使用默认的身份验证失败处理程序(SimpleUrlAuthenticationFailureHandler).您可以通过更改属性forwardToDestination值来更改默认行为(默认的行为是在身份验证失败时执行重定向).这是SimpleUrlAuthenticationFailureHandler的作用:

/**
 * Performs the redirect or forward to the {@code defaultFailureUrl} if set,otherwise returns a 401 error code.
 * <p>
 * If redirecting or forwarding,{@code saveException} will be called to cache the exception for use in
 * the target view.
 */
public void onAuthenticationFailure(HttpServletRequest request,HttpServletResponse response,AuthenticationException exception) throws IOException,ServletException {

    if (defaultFailureUrl == null) {
        logger.debug("No failure URL set,sending 401 Unauthorized error");

        response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"Authentication Failed: " + exception.getMessage());
    } else {
        saveException(request,exception);

        if (forwardToDestination) {
            logger.debug("Forwarding to " + defaultFailureUrl);

            request.getRequestDispatcher(defaultFailureUrl).forward(request,response);
        } else {
            logger.debug("Redirecting to " + defaultFailureUrl);
            redirectStrategy.sendRedirect(request,response,defaultFailureUrl);
        }
    }
}

因此,我想我是在security-context.xml中声明了SimpleUrlAuthenticationFailureHandler,并使用应该起作用的setUseForward(boolean forwardToDestination)方法设置了提到的属性值.可能是这样的:

<bean id="simpleUrlAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="useForward" value="true">
</bean>

接着:

    <security:form-login login-page="/auth/login"
        authentication-failure-handler-ref="simpleUrlAuthenticationFailureHandler"
        default-target-url="/mvc/home"
        always-use-default-target="true" />

祝好运.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...