上一篇说明了security 是如何加载资源和权限列表的,今天这篇差不多该结束了,开始说明当用户请求一个url的时候,security是如何根据你的登陆用户 和资源权限列表去决策你是否有权限访问你的url的。
首先你登陆完成之后,security会记录你的用户名以及其他一些信息,其中包括你所具有的权限,当你访问一个url的时候,security会根据你的url地址找到访问它所需要的权限列表,然后进行决策。
如何构建你自定义的决策类?
新建一个类实现AccessDecisionManager接口,应该是这个样子:
package cn.com.xinma.frame.service; import java.util.Collection; import org.springframework.security.access.AccessDecisionManager; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.authentication.InsufficientAuthenticationException; import org.springframework.security.core.Authentication; import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource; public class aaa implements AccessDecisionManager { @Override public void decide(Authentication arg0, Object arg1, Collection<ConfigAttribute> arg2) throws AccessDeniedException, InsufficientAuthenticationException { // TODO Auto-generated method stub } @Override public boolean supports(ConfigAttribute arg0) { // TODO Auto-generated method stub return false; } @Override public boolean supports(Class<?> arg0) { // TODO Auto-generated method stub return false; } }
其中两个重写的suppots方法都把返回值改为true
最重要的就是decide方法 ,它负责去决策你是否有权限去访问你访问的资源。
这个方法的第一个参数Authentication 是你登陆的角色所具有的权限列表。
第二个参数是你访问的url。
第三个参数是访问这个url所需要的权限列表。
至于security是怎样获取这些信息的先不管,需要通过一些简单的配置就可以了。是框架自己实现的,我们不需要详细了解。
知道了登陆用户所具有的权限,和访问资源的权限,决策就非常好做了。我给出项目中的源码,仅供参考.
package cn.com.xinma.frame.service; import java.util.Collection; import java.util.Iterator; import org.springframework.security.access.AccessDecisionManager; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.access.SecurityConfig; import org.springframework.security.authentication.InsufficientAuthenticationException; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.stereotype.Component; /** * AccessdecisionManager在Spring security中是很重要的。 * * 在验证部分简略提过了,所有的Authentication实现需要保存在一个GrantedAuthority对象数组中。 这就是赋予给主体的权限。 * GrantedAuthority对象通过AuthenticationManager 保存到 * Authentication对象里,然后从AccessDecisionManager读出来,进行授权判断。 * * Spring Security提供了一些拦截器,来控制对安全对象的访问权限,例如方法调用或web请求。 * 一个是否允许执行调用的预调用决定,是由AccessDecisionManager实现的。 这个 AccessDecisionManager * 被AbstractSecurityInterceptor调用, 它用来作最终访问控制的决定。 * 这个AccessDecisionManager接口包含三个方法: * * void decide(Authentication authentication, Object secureObject, * List<ConfigAttributeDefinition> config) throws AccessDeniedException; boolean * supports(ConfigAttribute attribute); boolean supports(Class clazz); * * 从第一个方法可以看出来,AccessDecisionManager使用方法参数传递所有信息,这好像在认证评估时进行决定。 * 特别是,在真实的安全方法期望调用的时候,传递安全Object启用那些参数。 比如,让我们假设安全对象是一个MethodInvocation。 * 很容易为任何Customer参数查询MethodInvocation, * 然后在AccessDecisionManager里实现一些有序的安全逻辑,来确认主体是否允许在那个客户上操作。 * 如果访问被拒绝,实现将抛出一个AccessDeniedException异常。 * * 这个 supports(ConfigAttribute) 方法在启动的时候被 * AbstractSecurityInterceptor调用,来决定AccessDecisionManager * 是否可以执行传递ConfigAttribute。 supports(Class)方法被安全拦截器实现调用, * 包含安全拦截器将显示的AccessDecisionManager支持安全对象的类型。 */ @Component("DecisionManager") public class CustomAccessDecisionManager implements AccessDecisionManager { public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException { if (configAttributes == null) { return; } Collection<GrantedAuthority> as= authentication.getAuthorities(); Iterator<ConfigAttribute> ite = configAttributes.iterator(); while (ite.hasNext()) { ConfigAttribute ca = ite.next(); String needRole = ((SecurityConfig) ca).getAttribute(); System.out.print("URL:"+object+"----needrole:"+needRole); // ga 为用户所被赋予的权限。 needRole 为访问相应的资源应该具有的权限。 for (GrantedAuthority ga : as) { if (needRole.trim().equals(ga.getAuthority().trim())) { return; } } } throw new AccessDeniedException("访问拒绝"); } public boolean supports(ConfigAttribute attribute) { return true; } public boolean supports(Class<?> clazz) { return true; } }
好了三个部分都说完了,写完了。那要配置到security 的配置文件里面,才会起到作用。
这个配置文件也不多,看了应该很容易明白,不解释了。我贴出来。
<http auto-config="true" lowercase-comparisons="false"> <intercept-url pattern="/login.jsp" filters="none" /> <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" login-processing-url="/j_security_check" /> <remember-me user-service-ref="UserDetailsService" key="e37f4b31-0c45-11dd-bd0b-0800200c9a66" /> <!-- 单用户登陆,还需要在web.xml中加入 listener --> <session-management> <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management> <!-- 增加一个自定义的filter,放在FILTER_SECURITY_INTERCEPTOR之前, 实现用户、角色、权限、资源的数据库管理。 --> <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR" /> </http> <!-- 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性。 --> <beans:bean id="myFilter" class="cn.com.xinma.frame.service.CustomFilterSecurityInterceptor"> <beans:property name="authenticationManager" ref="authenticationManager" /><!-- 登陆的认证 --> <beans:property name="accessDecisionManager" ref="customAccessDecisionManager" /><!-- 资源访问决策 --> <beans:property name="securityMetadataSource" ref="customSecurityMetadataSource" /><!-- 资源和权限列表 --> </beans:bean> <!-- 资源源数据定义,将所有的资源和权限对应关系建立起来,即定义某一资源可以被哪些角色去访问。11/3/23 --> <beans:bean id="customSecurityMetadataSource" class="cn.com.xinma.frame.service.CustomInvocationSecurityMetadataSourceService"> </beans:bean> <authentication-manager alias="authenticationManager"> <authentication-provider user-service-ref="UserDetailsService"> <!-- 用户密码加密 just like 899312{gaobing} to md5 --> <password-encoder ref="MyPasswordEncode"> <salt-source user-property="username" /> </password-encoder> </authentication-provider> </authentication-manager> <!-- 访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源。11/3/23 --> <beans:bean id="customAccessDecisionManager" class="cn.com.xinma.frame.service.CustomAccessDecisionManager"> </beans:bean>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>