1

我将 Spring-Security 3.1.3 与 Spring 3.2.2 和 Majorra 2.1.25 一起使用。我不使用托管 bean,而是使用 SpringBeanFacesELResolver。所以基本上,我什么都用弹簧。

我使用以下

<http auto-config="true">
    <form-login login-page="/components/public/login.jsf" authentication-failure-handler-ref="customAuthenticationFailureHandler" />
    <intercept-url pattern="/components/admin/**" access="ROLE_ADMIN" />
    <intercept-url pattern="/components/secured/**" access="ROLE_USER,ROLE_ADMIN" />
    <intercept-url pattern="/**" />
    <session-management>
        <concurrency-control max-sessions="1" expired-url="/components/public/sessionExpired.jsf" />
    </session-management>
    <access-denied-handler ref="customAccessDeniedHandler" />
</http>

它按预定方式工作,例如在访问受保护的页面时,用户会被引导到登录处,登录后他会被带到所请求的页面。如果他尝试访问管理页面,但只有 ROLE_USER,他会被我的 customAccessDeniedHandler 引导到拒绝访问页面

到目前为止,一切都很好。现在的问题如下:我使用@Secured({ "ROLE_ADMIN" })一种方法。如果权限不足的用户访问此方法,则会抛出 AccessDeniedException,这正是我想要的。但是:我的 customAccessDeniedHandler 没有被调用!这是为什么?

更多信息:该方法作为 AJAX 调用的一部分被调用,我想使用我的处理程序将 FacesMessage 设置为反馈。我如何集中执行此操作?我很确定我可以围绕这个包装另一种方法并使用 try-catch 自己捕获 AccessDeniedException。但是对每个必须保护的方法都这样做只会用大量不必要的 try-catch-methods 使我的代码膨胀。如何集中处理异常?

4

1 回答 1

1

我现在找到了解决方案。我使用 Spring-AOP 并将一个环绕方面“绑定”到所有使用 @Secured 注释的方法

<!-- aspect configuration -->
<aop:config>
    <aop:aspect id="securedAspect" ref="securityFeedbackAspect">
        <aop:around pointcut="@annotation(org.springframework.security.access.annotation.Secured)" method="handleSecuredAnnotations" />
    </aop:aspect>
</aop:config>

方面看起来像这样

@Service
public class SecurityFeedbackAspect {
    public Object handleSecuredAnnotations(final ProceedingJoinPoint pjp) throws Throwable {
        try {
            return pjp.proceed();
         } catch (AccessDeniedException e) {
            // log + set feedback for user here
         }
    }
}

希望有一天这对任何人都有帮助。一个附加信息:不知何故,我无法让它与仅注释配置一起使用,因为总是首先调用@Secured-check,并且我的方面只有在 Spring-Security-Logic 没有引发异常时才会运行。我最终使用了 XML 配置,这似乎总是先行,因为我没有找到其他方法(即使使用@Order)

于 2013-10-24T13:11:41.280 回答