春季安全性:拦截URL模式访问=“#id == 1

编程入门 行业动态 更新时间:2024-10-24 14:15:39
本文介绍了春季安全性:拦截URL模式访问=“#id == 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的项目分别是iam spring security 3.1.3和mvc 3.2

i have project were iam spring security 3.1.3 and mvc 3.2

我也希望允许路径中的用户ID与主体用户ID匹配的网址

i want too allow a url wehen in the userid in the path is matching the principal userid

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

http use-expressions设置为true,并且当tryPrincipal.userId == 1时可以工作,但是我需要使用从URL中提取的值.

http use-expressions it set to true and when a try principal.userId == 1 it works but i need to use the extracted value from the url.

我已经尝试了所有可能的组合.

i already tried all possible combinations.

推荐答案

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

It's not possible. But there is another way. You can define you own web expression that will be responsible for extracting of id parameter from URL. It may looks like that:

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

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

To do so you need: 1. Add CustomWebSecurityExpressionRoot that extends WebSecurityExpressionRoot 2. Add getIdUrlPathParameter() method. It will have access to HttpServletRequest object. 3. Define CustomWebSecurityExpressionHandler that extends DefaultWebSecurityExpressionHandler. Override createSecurityExpressionRoot method abd use your CustomWebSecurityExpressionRoot here. 4. Define custom access decision manager (xml below) 5. Inject it into your http element via access-decision-manager-ref attribute

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

更多推荐

春季安全性:拦截URL模式访问=“#id == 1

本文发布于:2023-10-29 09:50:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:安全性   春季   模式   URL   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!