分享

我的shiro之旅: 十一 shiro的权限设计

 WindySky 2017-12-27

在这里,介绍一个简单,基本的权限设计。其中包括3个类,有User,Role,Auth。下面是类信息。

  1. import java.io.Serializable;  
  2. import java.util.HashMap;  
  3. import java.util.HashSet;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. import com.concom.lang.helper.PasswordEncoder;  
  8.   
  9. /** 
  10.  * @author Dylan 
  11.  * @time 2013-8-5 
  12.  */  
  13. public class User implements Serializable {  
  14.     /** 
  15.      *  
  16.      */  
  17.     private static final long serialVersionUID = -5394552769206983498L;  
  18.   
  19.     private Long id;  
  20.     /** 
  21.      * 用户名 
  22.      */  
  23.     private String username;  
  24.     /** 
  25.      * 密码 
  26.      */  
  27.     private String password;  
  28.   
  29.     /** 
  30.      * 角色 
  31.      */  
  32.     private Set<Role> roles = new HashSet<Role>();  
  33.     /** 
  34.      * 权限 
  35.      */  
  36.     private Set<Auth> auths = new HashSet<Auth>();  
  37.   
  38.     /** 
  39.      * 用于转换role 
  40.      */  
  41.     private Map<String, Integer> roleTrans = new HashMap<String, Integer>();  
  42.     /** 
  43.      * 用于转换auth 
  44.      */  
  45.     private Map<String, Integer> authTrans = new HashMap<String, Integer>();  
  46.   
  47.     public User() {  
  48.     }  
  49.   
  50.     public Long getId() {  
  51.         return id;  
  52.     }  
  53.   
  54.     public void setId(Long id) {  
  55.         this.id = id;  
  56.     }  
  57.   
  58.     /** 
  59.      * Returns the username associated with this user account; 
  60.      *  
  61.      * @return the username associated with this user account; 
  62.      */  
  63.     public String getUsername() {  
  64.         return username;  
  65.     }  
  66.   
  67.     public void setUsername(String username) {  
  68.         this.username = username;  
  69.     }  
  70.   
  71.     /** 
  72.      * Returns the password for this user. 
  73.      *  
  74.      * @return this user's password 
  75.      */  
  76.     public String getPassword() {  
  77.         return password;  
  78.     }  
  79.   
  80.     public void setPassword(String password) {  
  81.         this.password = password;  
  82.     }  
  83.   
  84.     public Set<Role> getRoles() {  
  85.         return roles;  
  86.     }  
  87.   
  88.     public void setRoles(Set<Role> roles) {  
  89.         this.roles = roles;  
  90.     }  
  91.   
  92.     public Map<String, Integer> getRoleTrans() {  
  93.         return roleTrans;  
  94.     }  
  95.   
  96.     public void setRoleTrans(Map<String, Integer> roleTrans) {  
  97.         this.roleTrans = roleTrans;  
  98.     }  
  99.   
  100.     public Set<Auth> getAuths() {  
  101.         return auths;  
  102.     }  
  103.   
  104.     public void setAuths(Set<Auth> auths) {  
  105.         this.auths = auths;  
  106.     }  
  107.   
  108.     public Map<String, Integer> getAuthTrans() {  
  109.         return authTrans;  
  110.     }  
  111.   
  112.     public void setAuthTrans(Map<String, Integer> authTrans) {  
  113.         this.authTrans = authTrans;  
  114.     }  
  115.   
  116.     public User beforeCreate() {  
  117.         translateRole();  
  118.         translateAuth();  
  119.         return this;  
  120.     }  
  121.   
  122.     /** 
  123.      * 将装有role id的Map的值注入到role中 
  124.      */  
  125.     private void translateRole() {  
  126.         Map<String, Integer> roleIds = getRoleTrans();  
  127.         if (roleIds.isEmpty()) {  
  128.             return;  
  129.         }  
  130.         getRoles().clear();  
  131.         for (String key : roleIds.keySet()) {  
  132.             Role role = new Role();  
  133.             role.setId(roleIds.get(key));  
  134.             getRoles().add(role);  
  135.         }  
  136.     }  
  137.   
  138.     /** 
  139.      * 将装有auth id的Map的值注入到auth中 
  140.      */  
  141.     private void translateAuth() {  
  142.         Map<String, Integer> authIds = getAuthTrans();  
  143.         if (authIds.isEmpty()) {  
  144.             return;  
  145.         }  
  146.         getAuths().clear();  
  147.         for (String key : authIds.keySet()) {  
  148.             Auth auth = new Auth();  
  149.             auth.setId(authIds.get(key));  
  150.             getAuths().add(auth);  
  151.         }  
  152.     }  
  153.   
  154.     /** 
  155.      * @return 
  156.      */  
  157.     public Set<String> getRolesAsString() {  
  158.         Set<String> roles = new HashSet<String>();  
  159.         for (Role role : getRoles()) {  
  160.             roles.add(role.getCode());  
  161.         }  
  162.         return roles;  
  163.     }  
  164.   
  165.     /** 
  166.      *  
  167.      * @return 
  168.      */  
  169.     public Set<String> getAuthAsString() {  
  170.         Set<String> auths = new HashSet<String>();  
  171.         for (Auth auth : getAuths()) {  
  172.             auths.add(auth.getCode());  
  173.         }  
  174.         return auths;  
  175.     }  
  176.   
  177.     public boolean hasRoles() {  
  178.         return !getRoles().isEmpty();  
  179.     }  
  180.   
  181.     public boolean hasAuths() {  
  182.         return !getAuths().isEmpty();  
  183.     }  
  184.   
  185.     public User encodePassword() {  
  186.         setPassword(PasswordEncoder.encode(getPassword()));  
  187.         return this;  
  188.     }  
  189.   
  190. }  


  1. import java.io.Serializable;  
  2. import java.util.HashMap;  
  3. import java.util.HashSet;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. /** 
  8.  * @author Dylan 
  9.  * @time 2013-8-5 
  10.  */  
  11. public class Role implements Serializable {  
  12.     /** 
  13.      *  
  14.      */  
  15.     private static final long serialVersionUID = 6769720272431073142L;  
  16.   
  17.     private Integer id;  
  18.   
  19.     private String code;  
  20.   
  21.     private String name;  
  22.   
  23.     private Set<Auth> auths = new HashSet<Auth>();  
  24.   
  25.     private Map<String, Integer> authTrans = new HashMap<String, Integer>();  
  26.   
  27.     public Role() {  
  28.     }  
  29.   
  30.     public Role(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     public Integer getId() {  
  35.         return id;  
  36.     }  
  37.   
  38.     public void setId(Integer id) {  
  39.         this.id = id;  
  40.     }  
  41.   
  42.     public String getName() {  
  43.         return name;  
  44.     }  
  45.   
  46.     public void setName(String name) {  
  47.         this.name = name;  
  48.     }  
  49.   
  50.     public String getCode() {  
  51.         return code;  
  52.     }  
  53.   
  54.     public void setCode(String code) {  
  55.         this.code = code;  
  56.     }  
  57.   
  58.     public Set<Auth> getAuths() {  
  59.         return auths;  
  60.     }  
  61.   
  62.     public void setAuths(Set<Auth> auths) {  
  63.         this.auths = auths;  
  64.     }  
  65.   
  66.     public Map<String, Integer> getAuthTrans() {  
  67.         return authTrans;  
  68.     }  
  69.   
  70.     public void setAuthTrans(Map<String, Integer> authTrans) {  
  71.         this.authTrans = authTrans;  
  72.     }  
  73.   
  74.     public Role beforeCreate() {  
  75.         translateAuths();  
  76.         return this;  
  77.     }  
  78.   
  79.     private void translateAuths() {  
  80.         Map<String, Integer> authIds = getAuthTrans();  
  81.         if (authIds.isEmpty()) {  
  82.             return;  
  83.         }  
  84.         getAuths().clear();  
  85.         for (String key : authIds.keySet()) {  
  86.             Auth auth = new Auth();  
  87.             auth.setId(authIds.get(key));  
  88.             getAuths().add(auth);  
  89.         }  
  90.   
  91.     }  
  92.   
  93.     /** 
  94.      * @return 
  95.      */  
  96.     public Set<String> getAuthsAsString() {  
  97.         Set<String> auths = new HashSet<String>();  
  98.         for (Auth auth : getAuths()) {  
  99.             auths.add(auth.getCode());  
  100.         }  
  101.         return auths;  
  102.     }  
  103.   
  104.     public boolean hasAuth() {  
  105.         return !getAuths().isEmpty();  
  106.     }  
  107.   
  108. }  

  1. import java.io.Serializable;  
  2.   
  3. /** 
  4.  * @author Dylan 
  5.  * @time 2013-8-5 
  6.  */  
  7. public class Auth implements Serializable{  
  8.   
  9.     /** 
  10.      *  
  11.      */  
  12.     private static final long serialVersionUID = 1554690122999348374L;  
  13.       
  14.     private Integer id;  
  15.     /** 
  16.      * 权限代码 
  17.      */  
  18.     private String code;  
  19.     /** 
  20.      * 权限名称 
  21.      */  
  22.     private String name;  
  23.       
  24.     public Auth(){}  
  25.       
  26.     public Integer getId() {  
  27.         return id;  
  28.     }  
  29.   
  30.     public void setId(Integer id) {  
  31.         this.id = id;  
  32.     }  
  33.   
  34.     public String getCode() {  
  35.         return code;  
  36.     }  
  37.   
  38.     public void setCode(String code) {  
  39.         this.code = code;  
  40.     }  
  41.   
  42.     public String getName() {  
  43.         return name;  
  44.     }  
  45.   
  46.     public void setName(String name) {  
  47.         this.name = name;  
  48.     }  
  49.       
  50. }  

     因为在User里面设置的roles,auths为Set类型,Role里面设置的auths为Set类型,在jsp页面编辑注入到实体里的时候会有问题,所以均添加了Trans字段和对应的方法,目的是用于转换。

     前面的文章已经介绍过realm的编写。如果对realm不了解可以看看前面的文章。在realm中我们会覆写doGetAuthorizationInfo方法,shiro在第一次需要认证权限的时候会调用这个方法,并把权限信息放到缓存中。在这个方法里,我们调用了SimpleAuthorizationInfo的addRoles方法把用户的角色添加到了info中,调用addStringPermissions方法把用户的权限信息添加到info中。这样,shiro就可以从中取出角色码和权限码进行配对。下面再简单给出doGetAuthorizationInfo方法的实现。

  1. @Override    
  2.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {    
  3.             
  4.         String username = (String)getAvailablePrincipal(principals);    
  5.         User user = userService.getByUsername(username);    
  6.             
  7.         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();    
  8.         Set<String> rolesAsString = user.getRolesAsString();    
  9.         info.addRoles(rolesAsString);    
  10.         if(user.hasAuths()){    
  11.             info.addStringPermissions(user.getAuthAsString());    
  12.         }    
  13.         for(Role role : user.getRoles()){    
  14.             info.addStringPermissions(role.getAuthsAsString());    
  15.         }    
  16.         return info;    
  17.     }    



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多