spring security3.x学习(8)_web的投票器和spEL配置

来源:互联网 发布:ubuntu root password 编辑:程序博客网 时间:2024/04/28 14:24
上次我们说到了"访问决策投票管理"这个概念,那如何使用呢?
看一下如下代码:
<http auto-config="true" access-decision-manager-ref="unanimousBased" >
<bean class="org.springframework.security.access.vote.UnanimousBased" id="unanimousBased">
  <property name="decisionVoters">
    <list>
      <ref bean="roleVoter"/>
      <ref bean="authenticatedVoter"/>
    </list>
  </property>
</bean>
<bean class="org.springframework.security.access.vote.RoleVoter"   id="roleVoter"/>
<bean class="org.springframework.security.access.vote.AuthenticatedVoter" id="authenticatedVoter"/> 


这里使用了一个unanimousBased这样的一个"访问决策投票管理",配置了相关的投票器(roleVoter/authenticatedVoter)


那好。我们仔细关注一下角色投票器(RoleVoter),上边使用了一种配置的方式,其实还有一种更为方便和简单的投票器方法。
那就是使用Spring表达式语言配置访问控制:
要实现这一功能的直接方式是在<http>配置元素上添加use-expressions属性。
如:
<http auto-config="true"  use-expressions="true"> 
这时候我们生命的使用就可以使用spEL表达式了。此时你配置的时候就可以像如下配置:
<http auto-config="true" use-expressions="true">
  <intercept-url pattern="/*" access="hasRole('ROLE_USER')"/>
</http>


这个就非常方便了,就不用以前在access属性中写死角色信息咯。 呵呵。如果想好好使用这个的话,最好抽出时间多去看一些spEL的文档和说明。
那好,我们现在又遇到了一个问题,我们没有配置投票器,那默认是哪种呢? 使用了spEL的话,系统会默认使用o.s.s.web.access.expression.WebExpressionVoter这样一个投票器,他是通过o.s.s.web.access.expression.WebSecurityExpressionHandler去实现处理的。

WebSecurityExpressionHandler同时负责评估表达式的执行结果以及提供在表达式中应用的安全相关的方法。这个接口的默认实现对外暴露了o.s.s.web.access.expression.WebSecurityExpressionRoot 类中定义的方法。

好的,让我们来看看她的流程图和关系:


通过这个图,我们也能看出来,当真正获取spEL方法时,其实是使用WebSecurityExpressionRoot,我们通过它的父类可以看到一些方法,这样,我贴出来了反编译后的源码,我们可以看一下:

[html] view plaincopy
  1. /*     */ package org.springframework.security.access.expression;  
  2. /*     */  
  3. /*     */ import java.io.Serializable;  
  4. /*     */ import java.util.Collection;  
  5. /*     */ import java.util.HashSet;  
  6. /*     */ import java.util.Set;  
  7. /*     */ import org.springframework.security.access.PermissionEvaluator;  
  8. /*     */ import org.springframework.security.access.hierarchicalroles.RoleHierarchy;  
  9. /*     */ import org.springframework.security.authentication.AuthenticationTrustResolver;  
  10. /*     */ import org.springframework.security.core.Authentication;  
  11. /*     */ import org.springframework.security.core.authority.AuthorityUtils;  
  12. /*     */  
  13. /*     */ public abstract class SecurityExpressionRoot  
  14. /*     */   implements SecurityExpressionOperations  
  15. /*     */ {  
  16. /*     */   protected final Authentication authentication;  
  17. /*     */   private AuthenticationTrustResolver trustResolver;  
  18. /*     */   private RoleHierarchy roleHierarchy;  
  19. /*     */   private Set<String> roles;  
  20. /*  30 */   public final boolean permitAll = 1;  
  21. /*     */  
  22. /*  33 */   public final boolean denyAll = 0;  
  23. /*     */   private PermissionEvaluator permissionEvaluator;  
  24. /*  35 */   public final String read = "read";  
  25. /*  36 */   public final String write = "write";  
  26. /*  37 */   public final String create = "create";  
  27. /*  38 */   public final String delete = "delete";  
  28. /*  39 */   public final String admin = "administration";  
  29. /*     */  
  30. /*     */   public SecurityExpressionRoot(Authentication a) {  
  31. /*  42 */     if (a == null) {  
  32. /*  43 */       throw new IllegalArgumentException("Authentication object cannot be null");  
  33. /*     */     }  
  34. /*  45 */     this.authentication = a;  
  35. /*     */   }  
  36. /*     */  
  37. /*     */   public final boolean hasAuthority(String authority) {  
  38. /*  49 */     return hasRole(authority);  
  39. /*     */   }  
  40. /*     */  
  41. /*     */   public final boolean hasAnyAuthority(String[] authorities) {  
  42. /*  53 */     return hasAnyRole(authorities);  
  43. /*     */   }  
  44. /*     */  
  45. /*     */   public final boolean hasRole(String role) {  
  46. /*  57 */     return getAuthoritySet().contains(role);  
  47. /*     */   }  
  48. /*     */  
  49. /*     */   public final boolean hasAnyRole(String[] roles) {  
  50. /*  61 */     Set roleSet = getAuthoritySet();  
  51. /*     */  
  52. /*  63 */     for (String role : roles) {  
  53. /*  64 */       if (roleSet.contains(role)) {  
  54. /*  65 */         return true;  
  55. /*     */       }  
  56. /*     */     }  
  57. /*     */  
  58. /*  69 */     return false;  
  59. /*     */   }  
  60. /*     */  
  61. /*     */   public final Authentication getAuthentication() {  
  62. /*  73 */     return this.authentication;  
  63. /*     */   }  
  64. /*     */  
  65. /*     */   public final boolean permitAll() {  
  66. /*  77 */     return true;  
  67. /*     */   }  
  68. /*     */  
  69. /*     */   public final boolean denyAll() {  
  70. /*  81 */     return false;  
  71. /*     */   }  
  72. /*     */  
  73. /*     */   public final boolean isAnonymous() {  
  74. /*  85 */     return this.trustResolver.isAnonymous(this.authentication);  
  75. /*     */   }  
  76. /*     */  
  77. /*     */   public final boolean isAuthenticated() {  
  78. /*  89 */     return (!(isAnonymous()));  
  79. /*     */   }  
  80. /*     */  
  81. /*     */   public final boolean isRememberMe() {  
  82. /*  93 */     return this.trustResolver.isRememberMe(this.authentication);  
  83. /*     */   }  
  84. /*     */  
  85. /*     */   public final boolean isFullyAuthenticated() {  
  86. /*  97 */     return ((!(this.trustResolver.isAnonymous(this.authentication))) && (!(this.trustResolver.isRememberMe(this.authentication))));  
  87. /*     */   }  
  88. /*     */  
  89. /*     */   public Object getPrincipal() {  
  90. /* 101 */     return this.authentication.getPrincipal();  
  91. /*     */   }  
  92. /*     */  
  93. /*     */   public void setTrustResolver(AuthenticationTrustResolver trustResolver) {  
  94. /* 105 */     this.trustResolver = trustResolver;  
  95. /*     */   }  
  96. /*     */  
  97. /*     */   public void setRoleHierarchy(RoleHierarchy roleHierarchy) {  
  98. /* 109 */     this.roleHierarchy = roleHierarchy;  
  99. /*     */   }  
  100. /*     */  
  101. /*     */   private Set<String> getAuthoritySet() {  
  102. /* 113 */     if (this.roles == null) {  
  103. /* 114 */       this.roles = new HashSet();  
  104. /* 115 */       Collection userAuthorities = this.authentication.getAuthorities();  
  105. /*     */  
  106. /* 117 */       if (this.roleHierarchy != null) {  
  107. /* 118 */         userAuthorities = this.roleHierarchy.getReachableGrantedAuthorities(userAuthorities);  
  108. /*     */       }  
  109. /*     */  
  110. /* 121 */       this.roles = AuthorityUtils.authorityListToSet(userAuthorities);  
  111. /*     */     }  
  112. /*     */  
  113. /* 124 */     return this.roles;  
  114. /*     */   }  
  115. /*     */  
  116. /*     */   public boolean hasPermission(Object target, Object permission)  
  117. /*     */   {  
  118. /* 129 */     return this.permissionEvaluator.hasPermission(this.authentication, target, permission);  
  119. /*     */   }  
  120. /*     */  
  121. /*     */   public boolean hasPermission(Object targetId, String targetType, Object permission) {  
  122. /* 133 */     return this.permissionEvaluator.hasPermission(this.authentication, (Serializable)targetId, targetType, permission);  
  123. /*     */   }  
  124. /*     */  
  125. /*     */   public void setPermissionEvaluator(PermissionEvaluator permissionEvaluator) {  
  126. /* 137 */     this.permissionEvaluator = permissionEvaluator;  
  127. /*     */   }  
  128. /*     */ }  

有这样一个概念:伪属性(pseudo-property)

【伪属性(pseudo-property)是指没有传入参数并符合JavaBeans的getters命名格式的方法。
这允许SpEL表达式能够省略方法的圆括号以及is 或get的前缀。如isAnonymous()方法可以
通过anonymous伪属性来访问。】

突然觉得越看越有意思了。 那好,我们继续研究一下他的表达式都是如何支持的呢?、。我查了一下spring security帮助文档,它说,表达式有一些是通用的,有一些是web专用的,我们看一下啊。


上边这些可以用于web也可以用于方法。
在看一下web上可以使用的:


看到我画上红框的部分,那是我们应该特别注意的。 


看下面的一段话(听起来是废话,不过最好了解一下):
"
你可能会认为hasRole会返回一个Boolean值,实际上正是如此。基于SpEL的访
问控制声明必须是返回Boolean类型的表达式。返回值为true意味着投票器的结果是允许访
问,false 的结果意味着投票器拒绝访问
"


那好。 到今天为止,我们就学习了认证和授权了。 还有一些简单标签的配置,。应该慢慢就可以动手做一些东西了,。good!

0 0
原创粉丝点击