记录一下Spring Security的HTTP元素

竟然距离上一次博客有五个月之久了。。。

今天简单说下spring security配置http遇到的两个问题吧:

	<security:http pattern="/v2.0/tokens/**" use-expressions="true" create-session="stateless" auto-config="true"
                   entry-point-ref="unauthorizedEntryPoint" authentication-manager-ref="preAuthAuthenticationManager"
                   access-decision-manager-ref="accessDecisionManager" >
        <security:csrf disabled="true"/>
		<!-- 这行刚开始没有 -->
        <security:intercept-url pattern="/v2.0/tokens/**" access="isAuthenticated()"/>
        <security:custom-filter position="PRE_AUTH_FILTER" ref="onePreFilter"/>
    </security:http>
	
	<bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
        <constructor-arg>
            <list>
                <ref bean="roleVoter" />
                <ref bean="authenticatedVoter" />
            </list>
        </constructor-arg>
    </bean>
    <bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter ">
        <property name="rolePrefix" value="" />
    </bean>
    <bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter" />



以上配置主要自定义了一个accessDecisionManager用于自定义用户是否有权限访问某个pattern的URL。

问题1:刚开始访问的时候没有注释下面的"intercept-url"这行,因为copy的其他配置也没有,结果发现,其他的API都能被保护住,只有这个不可以。查了半天,才发现其他的API方法上都加了Spring security的@Secure注解。

然后兴趣使然,就往下看了看,结果发现这个http element仅仅是用来声明一个Filter Chain, 在调用到最终目标方法之前,会通过filter chain中的FilterSecurityInterceptor/MethodSecurityInterceptor调用到AbstractSecurityInterceptor.beforeInvocation()方法,并且把配置/注解中的attribute拿出来,这个Attribute实际上是SecurityMetadataSource接口实现类的一部分,对于XML配置来说,会在Spring初始化时候就根据配置生成好);对于注解来说,没细看,不过内容是和Secure注解相关的。

如果既没有方法上的注解保护,也没有配置intercept-url,那这个filter chain应该就算白配了,过了一堆过滤器应该都不会起作用。

于是添加了Intercept URL, 加上了access="isAuthenticated()"

问题二:加上access后,应用启动就报错了,说这个isAuthenticated不认识。报错是在FilterSecurityInterceptor初始化之后的afterPropertiesSet方法里。 于是就开始看Spring Security的AccessDecision机制。

Spring Security就三个AccessDecision Manager,都继承自AbstractAccessDecisionManager.基本就是一帮人做决定的时候的这三种情况:一个人说通过就是通过;多数人说通过才是通过;所有人说通过就是通过。

这个manager里面会有若干个Voter(投票者),每种投票者可能会支持不同的用户输入,isAuthenticate只有WebExpressionVoter才支持,这个Voter我又正好没有配,才会报错。

一些表达式可以见
http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

如果想跑过的话,把http元素上自定义的access-decision-manager-ref="accessDecisionManager"去掉就好。

猜你喜欢

转载自wwwcomy.iteye.com/blog/2309829