【问题标题】:JSF2 with SpringSecurity: handle AccessDeniedException of @Secured-Annotation带有 SpringSecurity 的 JSF2:处理 @Secured-Annotation 的 AccessDeniedException
【发布时间】:2013-11-01 09:42:47
【问题描述】:

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

我用下面的

<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>

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

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

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

【问题讨论】:

    标签: spring jsf jsf-2 spring-security


    【解决方案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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2014-01-30
      • 2016-09-08
      • 2012-01-18
      • 2016-05-18
      • 2015-12-10
      • 1970-01-01
      相关资源
      最近更新 更多