您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

spring security:拦截URL模式access =“#id == 1

spring security:拦截URL模式access =“#id == 1

这是不可能的。但是还有另一种方式。你可以定义自己的网络表达式,该表达式将负责从URL中提取id参数。可能看起来像这样:

<security:intercept-url pattern="/user/{id}/edit" access="getIdUrlPathParameter() == principal.userId"/>

为此,你需要: 1.添加扩展WebSecurityExpressionRoot的 CustomWebSecurityExpressionRoot 2.添加getIdUrlPathParameter()方法。它将有权访问HttpServletRequest对象。 3.定义CustomWebSecurityExpressionHandler,以扩展DefaultWebSecurityExpressionHandler。覆盖createSecurityExpressionRoot方法,然后在此处使用CustomWebSecurityExpressionRoot。 4.定义自定义访问决策管理器(下面的xml) 5. 通过access-decision-manager-ref属性将其注入到http元素中

<security:http access-decision-manager-ref="customAccessDecisionManagerBean" >
    <security:intercept-url pattern="/user/{id}/edit" access="getIdUrlPathParameter() == principal.userId"/>
</security:http>
<bean id="customWebSecurityExpressionHandler" class="com.domain.security.CustomWebSecurityExpressionHandler"/>
<bean id="customAccessDecisionManagerBean" class="org.springframework.security.access.Vote.AffirmativeBased">
    <property name="decisionVoters">
        <list>
            <bean class="org.springframework.security.web.access.expression.WebExpressionVoter">
                <property name="expressionHandler" ref="customWebSecurityExpressionHandler" />
            </bean>
        </list>
    </property>
</bean>
Access 2022/1/1 18:19:44 有418人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶