繁体   English   中英

Spring Security不会抛出任何异常

[英]Spring Security does not throw any exceptions

我的春季安全配置:

<security:http auto-config="true"  use-expressions="true" >
<security:intercept-url pattern="/game/**" access="isAuthenticated()"/>
<security:intercept-url pattern="/**" access="permitAll"/>

<security:form-login password-parameter="password"  username-parameter="username"  login-page="/"
            default-target-url="/game/" login-processing-url="/processLogin" authentication-failure-url="/"/>
<security:logout logout-url="/game/logout" logout-success-url="/" delete-cookies="true" invalidate-session="true"/>
<security:access-denied-handler ref="accessDeniedHandler" />
</security:http>

定制处理程序

@Component
public class AccessDeniedHandler extends AccessDeniedHandlerImpl {
@Override
public void handle(HttpServletRequest request,
                   HttpServletResponse response, AccessDeniedException exception)
        throws IOException, ServletException {
    super.handle(request, response, exception);
    }
}

异常解析器:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key=".AccessDeniedException">accessDenied</prop>
        </props>
    </property>
</bean>

我遇到的问题是spring security不会抛出任何异常。 例如,当我输入受保护的URL时,它会将我重定向到登录页面。 如果我从配置中删除登录页面,它会将我重定向到spring security default login form。 因为我无法处理任何像AccessDeniedException这样的异常。 正如您在上面所看到的,我已经定义了自定义访问被拒绝处理程序,它会覆盖handle方法,但它甚至不会进入它。 我也尝试使用SimpleMappingExceptionResolver,但这也不起作用。 所以我的问题是:如何使spring安全抛出异常,以便我可以通过解析器或自定义处理程序捕获它们?

我的AccessDeniedHandler无法工作的原因是因为它仅适用于经过身份验证的用户,因此,当未经身份验证的用户尝试仅为经过身份验证的用户访问内容时,为了显示错误页面(这是尝试捕获AccessDeniedException的目的),您应该编写自定义入口点 ,并覆盖默认入口点

感谢Luke Taylor向我澄清这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM