当前位置: 移动技术网 > IT编程>开发语言>Java > spring security自定义决策管理器

spring security自定义决策管理器

2019年07月19日  | 移动技术网IT编程  | 我要评论

首先介绍下spring的决策管理器,其接口为accessdecisionmanager,抽象类为abstractaccessdecisionmanager。而我们要自定义决策管理器的话一般是继承抽象类而不去直接实现接口。

在spring中引入了投票器(accessdecisionvoter)的概念,有无权限访问的最终觉得权是由投票器来决定的,最常见的投票器为rolevoter,在rolevoter中定义了权限的前缀,先看下spring在rolevoter中是怎么处理授权的。

public int vote(authentication authentication, object object, collection<configattribute> attributes) { 
  int result = access_abstain; 
  collection<? extends grantedauthority> authorities = extractauthorities(authentication); 
  for (configattribute attribute : attributes) { 
    if (this.supports(attribute)) { 
      result = access_denied; 
      // attempt to find a matching granted authority 
      for (grantedauthority authority : authorities) { 
        if (attribute.getattribute().equals(authority.getauthority())) { 
          return access_granted; 
        } 
      } 
    } 
  } 
  return result; 
} 
collection<? extends grantedauthority> extractauthorities(authentication authentication) { 
  return authentication.getauthorities(); 
} 

authentication中是用户及用户权限信息,attributes是访问资源需要的权限,然后循环判断用户是否有访问资源需要的权限,如果有就返回access_granted,通俗的说就是有权限。

spring提供了3个决策管理器,至于这三个管理器是如何工作的请查看springsecurity源码

affirmativebased 一票通过,只要有一个投票器通过就允许访问

consensusbased 有一半以上投票器通过才允许访问资源

unanimousbased 所有投票器都通过才允许访问

下面来实现一个简单的自定义决策管理器,这个决策管理器并没有使用投票器

public class defaultaccessdecisionmanager extends abstractaccessdecisionmanager { 
  public void decide( authentication authentication, object object,  
      collection<configattribute> configattributes)  
    throws accessdeniedexception, insufficientauthenticationexception{ 
    sysuser user = (sysuser)authentication.getprincipal(); 
    logger.info("访问资源的用户为"+user.getusername()); 
    //如果访问资源不需要任何权限则直接通过 
    if( configattributes == null ) { 
      return ; 
    } 
    iterator<configattribute> ite = configattributes.iterator(); 
    //遍历configattributes看用户是否有访问资源的权限 
    while( ite.hasnext()){ 
      configattribute ca = ite.next(); 
      string needrole = ((securityconfig)ca).getattribute(); 
      //ga 为用户所被赋予的权限。 needrole 为访问相应的资源应该具有的权限。 
      for( grantedauthority ga: authentication.getauthorities()){ 
        if(needrole.trim().equals(ga.getauthority().trim())){ 
          return; 
        } 
      } 
    } 
    throw new accessdeniedexception(""); 
  } 
} 

 decide这个方法没有任何的返回值,需要在没有通过授权时抛出accessdeniedexception。

如果有访问某个资源需要同时拥有两个或两个以上权限的情况,这时候就要通过自定义accessdecisionvoter来实现了,这个也很简单在这里就不赘述了。如果要在页面中使用hasrole()这样的表达式就需要注入webexpressionvoter了。
在springsecurity中自定义权限前缀

权限的前缀默认是role_,网上的很多例子是说,直接在配置文件中加上下面的配置就可以了。

<bean id="rolevoter" class="org.springframework.security.access.vote.rolevoter"> 
  <property name="roleprefix" value="auth_"></property> 
</bean> 

亲测不管用的,我想应该不是我配置的问题,而是在我们配置了http auto-config="true"spring就已经将accessdecisionmanager初始化好了,即便配置到之前也不行,因为这个初始化是spring自己来完成的,它并没有把你配置的rolevoter注入到accessdecisionmanager中。那我们就来手动的注入accessdecisionmanager吧。

在http配置中有个access-decision-manager-ref属性,可以使我们手动注入accessdecisionmanager,下面是详细配置

<sec:http auto-config="true" access-decision-manager-ref="accessdecisionmanager"> 
  <sec:access-denied-handler ref="accessdeniedhandler"/> 
  <sec:session-management invalid-session-url="/login.jsp" /> 
  <sec:intercept-url pattern="/app.jsp" access="auth_gg_fbgbgg"/> 
  <sec:intercept-url pattern="/**" access="is_authenticated_fully" /> 
  <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp" 
    default-target-url="/index.jsp"/> 
</sec:http> 
<bean id="accessdecisionmanager" class="org.springframework.security.access.vote.affirmativebased"> 
  <constructor-arg name="decisionvoters"> 
    <list> 
      <ref bean="rolevoter"/> 
      <ref bean="authenticatedvoter"/> 
    </list> 
  </constructor-arg> 
  <property name="messagesource" ref="messagesource"></property> 
</bean> 
<bean id="rolevoter" class="org.springframework.security.access.vote.rolevoter"> 
  <property name="roleprefix" value=""></property> 
</bean> 
<bean id="authenticatedvoter" class="org.springframework.security.access.vote.authenticatedvoter" /> 

在这里我们就不用自定义的accessdecisionmanager了,直接用spring的affirmativebased,因为spring本身提供的这些决策管理器就已经很强大了。

配置很简单,要想修改权限的前缀只需要修改rolevoter中的roleprefix就可以了,如果不要前缀就让它为“”。

authenticatedvoter是为了支持is_authenticated这种认证,authenticatedvoter提供的3种认证,分别是

is_authenticated_anonymously 允许匿名用户进入

is_authenticated_fully 允许登录用户进入

is_authenticated_remembered 允许登录用户和rememberme用户进入

总结

以上所述是小编给大家介绍的spring security自定义决策管理器,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网