【问题标题】:How to override security access in Spring?如何在 Spring 中覆盖安全访问?
【发布时间】:2014-06-03 22:15:28
【问题描述】:

我正在使用 Spring Framework 4.0.0 GA 和 Spring Security 3.2.0 GA。我已使用如下切入点表达式将安全性应用于包中所有类的所有方法。

<global-method-security secured-annotations="enabled" pre-post-annotations="enabled" proxy-target-class="false">
    <protect-pointcut expression="execution(* admin.dao.*.*(..))" access="ROLE_ADMIN"/>
</global-method-security>

admin.dao中定义的所有类的所有方法只能由权限为ROLE_ADMIN的用户访问。

现在是否可以在此包中某些类的某些方法中覆盖此安全约束?

我需要匿名访问这个包下某个类中的某些方法(这已经是安全的了)。

在 JAAS 中,这可以通过在相关方法上方使用 javax.annotation.security.PermitAll 注释来实现,该注释将覆盖任何全局约束(例如,应用类级别的约束)。

我已经尝试过使用上述方法的@Secured(value = "permitAll")@Secured(value = "isAnonymous()"),但它们都不起作用。

【问题讨论】:

    标签: spring security spring-mvc spring-security


    【解决方案1】:

    尝试以下方法:

    <global-method-security secured-annotations="enabled" pre-post-annotations="enabled" proxy-target-class="false">
        <protect-pointcut expression="execution(* admin.your.permit.all.dao.*.*(..))" 
              access="permitAll"/>
        <protect-pointcut expression="execution(* admin.dao.*.*(..))" access="ROLE_ADMIN"/>
    </global-method-security>
    

    确保将protect-pointcutpermitAll 条目放在第一位,在这种情况下顺序很重要。

    【讨论】:

    • +1 切入点表达式按照您指定的顺序进行计算。
    • execution(* admin.dao.*.methodName(..)) 这样的切入点表达式按预期工作,但我必须使用IS_AUTHENTICATED_ANONYMOUSLY 作为access 属性的值,即使use-expressionsauto-config 设置为true &lt;http&gt;&lt;http auto-config='true' use-expressions="true" disable-url-rewriting="true"&gt;。同样,permitAll 适用于像 &lt;intercept-url pattern="/utility/Login.jsf" access="permitAll" requires-channel="any"/&gt; 这样的拦截器。我错过了关于permitAll 的内容吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2016-12-27
    • 2015-04-26
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多