Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

spring security:intercept-url pattern access="#id == 1

i have project were iam spring security 3.1.3 and mvc 3.2

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...