【问题标题】:spring security - writing a custom SPEL access expression. Is my approach correct?spring security - 编写自定义 SPEL 访问表达式。我的方法正确吗?
【发布时间】:2012-04-22 16:58:32
【问题描述】:

我希望指定一个拦截 URL 模式,例如 pattern = hasCollege('college1',college2')。为此,我正在考虑以下方法:

a) 配置 WebExpressionVoter 以使用自定义表达式处理程序

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
    <beans:property name="decisionVoters">
        <beans:list>
            <beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter">
                <beans:property name="expressionHandler" ref="myWebSecurityExpressionHandler"/>
            </beans:bean>
        </beans:list>
    </beans:property>
</beans:bean>
<beans:bean id="myWebSecurityExpressionHandler" class="com.daud.security.EEWebSecurityExpressionHandler"/>

b) 使EEWebSecurityExpressionHandlerDefaultWebSecurityExpressionHandler 的方式实现WebSecurityExpressionHandler 并使用createEvaluationContext 设置自定义根对象。

@Override
    public EvaluationContext createEvaluationContext(Authentication authentication, FilterInvocation fi) {
        StandardEvaluationContext ctx = new StandardEvaluationContext();
        SecurityExpressionRoot root = new MyWebSecurityExpressionRoot(authentication, fi);
        root.setTrustResolver(trustResolver);
        root.setRoleHierarchy(roleHierarchy);
        ctx.setRootObject(root);
        return ctx;
    }

c) 使MyWebSecurityExpressionRoot 扩展WebSecurityExpressionRoot 并声明对应于新SPEL 表达式的新方法:

public final boolean hasCollege(String... colleges){
       // logic goes here
    }

这是解决问题的正确方法吗?

【问题讨论】:

标签: java spring spring-security


【解决方案1】:

在您的 spring 安全配置中,您可以执行以下操作。

<http use-expressions="true">
    <intercept-url pattern="/**" access="isFullyAuthenticated() and principal.college matches 'Duke|USC'"/>

这是假设您的主体上有一个 getCollege() 方法。

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2013-08-23
    • 2013-10-19
    • 1970-01-01
    • 2012-02-22
    • 2020-06-03
    相关资源
    最近更新 更多