@PreAuthorize弹簧控制器在授权失败时发送重定向

时间:2010-11-11 00:04:05

标签: java spring-mvc spring-security

我已经在我的控制器上成功评估了@PreAuthorize的Spring安全性。如果我使用“permitAll”然后我可以查看页面,如果我使用“isAuthenticated()”,那么我得到一个丑陋的访问被拒绝的堆栈跟踪。如果我将配置放在我的安全上下文配置xml文件中的http节点内的intercept-url中,那么我很好地重定向到登录页面,而不是在我的页面中找到令人讨厌的堆栈跟踪。

有没有办法让我只使用注释机制进行重定向?

2 个答案:

答案 0 :(得分:5)

我让这个工作。我必须处理几件事。

首先,我的Spring MVC配置有一个SimpleMappingExceptionResolver,配置了defaultErrorView。那是在他们到达我在安全配置中的http元素中配置的访问被拒绝处理程序之前拦截了身份验证和授权错误。最终的代码看起来像这样。

securitycontext.xml

<global-method-security pre-post-annotations="enabled"/>

<!-- HTTP security configurations -->
<http auto-config="false" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
    <access-denied-handler ref="myAccessDeniedHandler" />
    ... other configuration here ...
</http>

<!-- handler for authorization failure.  Will redirect to the login page. -->
<beans:bean id="myAccessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
    <beans:property name="errorPage" value="/index" />
</beans:bean>

请注意,loginUrlAuthenticationEntryPoint实际上不是解决方案的一部分,它是access-denied-handler。

我的mvc-config.xml仍然具有SimpleMappingExceptionResolver,但没有配置defaultErrorView。如果我继续这个路径,我可能会实现我自己的SimpleMappingExceptionResolver,它将允许身份验证和授权激活,或者在SimpleMappingExceptionResolver中配置它。

这笔交易中的杀手是我没有找到通过注释从intercept-url配置requires-channel =“https”的方法,所以我现在将把它放在xml配置文件中反正。

答案 1 :(得分:3)

您可以通过覆盖安全入口点来自定义错误处理。筛选链中发生的所有异常(由您在web.xml文件中定义的映射定义)都会被捕获并可在此处理。如果没有处理它们,那么ExceptionTranslationFilter将接管。

您可以像这样定义自己的入口点:

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, org.springframework.security.core.AuthenticationException authException) throws IOException, ServletException {

        if (authException != null) {
            // you can check for the spefic exception here and redirect like this
            response.sendRedirect("403.html");
        }
    }
}

您可以通过将其设置为xml配置文件中的入口点来指定此作为您的入口点:

<http entry-point-ref="customAuthenticationEntryPoint">

  ...

</http>

在您的特定情况下,PreInvocationAuthorizationAdviceVoter将由您在配置中指定的任何AccessDecisionManager调用(通常是以下之一:AffirmativeBased,ConsensusBased或UnanimousBased)。您将看到这些选民抛出了AccessDeniedException,您可以在入口点专门捕获和处理它。

捐赠

相关问题