2013-01-06 88 views
4

我有项目是IAM春季安全3.1.3和MVC 3.2春季安全:拦截的URL模式访问=“#ID == 1

我想得允许在路径中的用户ID的URL wehen是匹配主要用户ID

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

HTTP使用表达式它设置为true,当尝试principal.userId == 1它的工作原理,但我需要从URL中使用提取的值。

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

+0

想我发现了这把它注射到你的HTTP元素,但事实并非如此工作 http://forum.springsource.org/showthread.php?130413-Web-Security-Expressions-and-PathVariables – knoma

回答

6

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

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

要做到这一点,你需要:扩展WebSecurityExpressionRoot
2.添加getIdUrlPathParameter()方法
1.添加CustomWebSecurityExpressionRoot。它将有权访问HttpServletRequest对象。
3.定义可扩展的CustomWebSecurityExpressionHandler DefaultWebSecurityExpressionHandler。重写createSecurityExpressionRoot方法abd在这里使用您的CustomWebSecurityExpressionRoot。
4.定义自定义访问决策管理器(如下XML)
5.通过访问决策管理-ref属性

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

thx您的建议 - 它为我工作。 – knoma