【Shiro】Springboot集成Shiro(二)

简介: 【Shiro】Springboot集成Shiro(二)

【3】核心类代码

【3.1】ShiroDbRealm



1. package com.itheima.shiro.core;
2. 
3. import org.apache.shiro.authc.AuthenticationInfo;
4. import org.apache.shiro.authc.AuthenticationToken;
5. import org.apache.shiro.authz.AuthorizationInfo;
6. import org.apache.shiro.realm.AuthorizingRealm;
7. import org.apache.shiro.subject.PrincipalCollection;
8. 
9. import javax.annotation.PostConstruct;
10. 
11. 
12. /**
13.  *
14.  * @Description shiro自定义realm
15.  */
16. public abstract class ShiroDbRealm extends AuthorizingRealm {
17. 
18.   /**
19.    * @Description 认证
20.    * @param authcToken token对象
21.    * @return 
22.    */
23.   public abstract AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) ;
24. 
25.   /**
26.    * @Description 鉴权
27.    * @param principals 令牌
28.    * @return
29.    */
30.   public abstract AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals);
31. 
32.   /**
33.    * @Description 密码匹配器
34.    */
35.   @PostConstruct
36.   public abstract void initCredentialsMatcher() ;
37. 
38. 
39. }
【3.2】ShiroDbRealmImpl
1. package com.itheima.shiro.core.impl;
2. 
3. import com.itheima.shiro.constant.SuperConstant;
4. import com.itheima.shiro.core.base.ShiroUser;
5. import com.itheima.shiro.core.base.SimpleToken;
6. import com.itheima.shiro.core.ShiroDbRealm;
7. import com.itheima.shiro.core.bridge.UserBridgeService;
8. import com.itheima.shiro.pojo.User;
9. import com.itheima.shiro.utils.BeanConv;
10. import com.itheima.shiro.utils.DigestsUtil;
11. import com.itheima.shiro.utils.EmptyUtil;
12. import org.apache.shiro.authc.AuthenticationInfo;
13. import org.apache.shiro.authc.AuthenticationToken;
14. import org.apache.shiro.authc.SimpleAuthenticationInfo;
15. import org.apache.shiro.authc.UnknownAccountException;
16. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
17. import org.apache.shiro.authz.AuthorizationInfo;
18. import org.apache.shiro.subject.PrincipalCollection;
19. import org.apache.shiro.util.ByteSource;
20. import org.springframework.beans.factory.annotation.Autowired;
21. 
22. /**
23.  * @Description:自定义shiro的实现
24.  */
25. public class ShiroDbRealmImpl extends ShiroDbRealm {
26. 
27. @Autowired
28. private UserBridgeService userBridgeService;
29. 
30. 
31. /**
32.      * @Description 认证方法
33.      * @param authcToken 校验传入令牌
34.      * @return AuthenticationInfo
35.      */
36. @Override
37. public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) {
38. SimpleToken token = (SimpleToken)authcToken;
39. User user  = userBridgeService.findUserByLoginName(token.getUsername());
40. if(EmptyUtil.isNullOrEmpty(user)){
41. throw new UnknownAccountException("账号不存在");
42.         }
43. ShiroUser shiroUser = BeanConv.toBean(user, ShiroUser.class);
44.         shiroUser.setResourceIds(userBridgeService.findResourcesIdsList(user.getId()));
45. String salt = user.getSalt();
46. String password = user.getPassWord();
47. return new SimpleAuthenticationInfo(shiroUser, password, ByteSource.Util.bytes(salt), getName());
48.     }
49. 
50. /**
51.      * @Description 授权方法
52.      * @param principals SimpleAuthenticationInfo对象第一个参数
53.      * @return
54.      */
55. @Override
56. public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
57. ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
58. return userBridgeService.getAuthorizationInfo(shiroUser);
59.     }
60. 
61. /**
62.      * @Description 加密方式
63.      */
64. @Override
65. public void initCredentialsMatcher() {
66. HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SuperConstant.HASH_ALGORITHM);
67.         matcher.setHashIterations(SuperConstant.HASH_INTERATIONS);
68.         setCredentialsMatcher(matcher);
69. 
70.     }
71. }
【3.3】SimpleToken



1. package com.itheima.shiro.core.base;
2. 
3. import org.apache.shiro.authc.UsernamePasswordToken;
4. 
5. 
6. /**
7.  * @Description 自定义tooken
8.  */
9. public class SimpleToken extends UsernamePasswordToken {
10. 
11.   /** serialVersionUID */
12.   private static final long serialVersionUID = -4849823851197352099L;
13. 
14.   private String tokenType;
15. 
16.   private String quickPassword;
17. 
18.   /**
19.    * Constructor for SimpleToken
20.    * @param tokenType
21.    */
22.   public SimpleToken(String tokenType, String username,String password) {
23.     super(username,password);
24.     this.tokenType = tokenType;
25.   }
26. 
27.   public SimpleToken(String tokenType, String username,String password,String quickPassword) {
28.     super(username,password);
29.     this.tokenType = tokenType;
30.     this.quickPassword = quickPassword;
31.   }
32. 
33.   public String getTokenType() {
34.     return tokenType;
35.   }
36. 
37.   public void setTokenType(String tokenType) {
38.     this.tokenType = tokenType;
39.   }
40. 
41.   public String getQuickPassword() {
42.     return quickPassword;
43.   }
44. 
45.   public void setQuickPassword(String quickPassword) {
46.     this.quickPassword = quickPassword;
47.   }
48. 
49. 
50. }
【3.4】ShiroUser



1. package com.itheima.shiro.core.base;
2. 
3. import com.itheima.shiro.utils.ToString;
4. import lombok.Data;
5. 
6. import java.util.List;
7. 
8. 
9. /**
10.  * @Description 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息
11.  */
12. @Data
13. public class  ShiroUser extends ToString {
14. 
15.   /** serialVersionUID */
16.   private static final long serialVersionUID = -5024855628064590607L;
17. 
18.   /**
19.    * 主键
20.    */
21.   private String id;
22. 
23.   /**
24.    * 登录名称
25.    */
26.   private String loginName;
27. 
28.   /**
29.    * 真实姓名
30.    */
31.   private String realName;
32. 
33.   /**
34.    * 昵称
35.    */
36.   private String nickName;
37. 
38.   /**
39.    * 密码
40.    */
41.   private String passWord;
42. 
43.   /**
44.    * 加密因子
45.    */
46.   private String salt;
47. 
48.   /**
49.    * 性别
50.    */
51.   private Integer sex;
52. 
53.   /**
54.    * 邮箱
55.    */
56.   private String zipcode;
57. 
58.   /**
59.    * 地址
60.    */
61.   private String address;
62. 
63.   /**
64.    * 固定电话
65.    */
66.   private String tel;
67. 
68.   /**
69.    * 电话
70.    */
71.   private String mobil;
72. 
73.   /**
74.    * 邮箱
75.    */
76.   private String email;
77. 
78.   /**
79.    * 职务
80.    */
81.   private String duties;
82. 
83.   /**
84.    * 排序
85.    */
86.   private Integer sortNo;
87. 
88.   /**
89.    * 是否有效
90.    */
91.   private String enableFlag;
92. 
93.   private List<String> resourceIds;
94. 
95.   public ShiroUser() {
96.     super();
97.   }
98. 
99.   public ShiroUser(String id, String loginName) {
100.    super();
101.    this.id = id;
102.    this.loginName = loginName;
103.  }
104. 
105. 
106.  @Override
107.  public int hashCode() {
108.    final int prime = 31;
109.    int result = 1;
110.    result = prime * result + ((email == null) ? 0 : email.hashCode());
111.    result = prime * result + ((id == null) ? 0 : id.hashCode());
112.    result = prime * result
113.        + ((loginName == null) ? 0 : loginName.hashCode());
114.    result = prime * result + ((mobil == null) ? 0 : mobil.hashCode());
115.    return result;
116.  }
117. 
118.  @Override
119.  public boolean equals(Object obj) {
120.    if (this == obj)
121.      return true;
122.    if (obj == null)
123.      return false;
124.    if (getClass() != obj.getClass())
125.      return false;
126.    ShiroUser other = (ShiroUser) obj;
127.    if (email == null) {
128.      if (other.email != null)
129.        return false;
130.    } else if (!email.equals(other.email))
131.      return false;
132.    if (id == null) {
133.      if (other.id != null)
134.        return false;
135.    } else if (!id.equals(other.id))
136.      return false;
137.    if (loginName == null) {
138.      if (other.loginName != null)
139.        return false;
140.    } else if (!loginName.equals(other.loginName))
141.      return false;
142.    if (mobil == null) {
143.      if (other.mobil != null)
144.        return false;
145.    } else if (!mobil.equals(other.mobil))
146.      return false;
147.    return true;
148.  }
149. 
150. 
151. }
【3.5】UserBridgeService
1. package com.itheima.shiro.core.bridge;
2. 
3. import com.itheima.shiro.core.base.ShiroUser;
4. import com.itheima.shiro.pojo.User;
5. import org.apache.shiro.authz.AuthorizationInfo;
6. 
7. import java.util.List;
8. 
9. /**
10.  * @Description:用户信息桥接(后期会做缓存)
11.  */
12. public interface UserBridgeService {
13. 
14. 
15. /**
16.      * @Description 查找用户信息
17.      * @param loginName 用户名称
18.      * @return user对象
19.      */
20.     User findUserByLoginName(String loginName);
21. 
22. /**
23.      * @Description 鉴权方法
24.      * @param shiroUser 令牌对象
25.      * @return 鉴权信息
26.      */
27.     AuthorizationInfo getAuthorizationInfo(ShiroUser shiroUser);
28. 
29. /**
30.      * @Description 查询用户对应角色标识list
31.      * @param userId 用户id
32.      * @return 角色标识集合
33.      */
34.     List<String> findRoleList(String userId);
35. 
36. /**
37.      * @Description 查询用户对应资源标识list
38.      * @param userId 用户id
39.      * @return 资源标识集合
40.      */
41.     List<String> findResourcesList(String userId);
42. 
43. /**
44.      * @Description 查询资源ids
45.      * @param userId 用户id
46.      * @return 资源id集合
47.      */
48.     List<String> findResourcesIds(String userId);
49. }
【3.6】UserBridgeServiceImpl
1. package com.itheima.shiro.core.bridge.impl;
2. 
3. import com.itheima.shiro.core.adapter.UserAdapter;
4. import com.itheima.shiro.core.base.ShiroUser;
5. import com.itheima.shiro.core.bridge.UserBridgeService;
6. import com.itheima.shiro.pojo.Resource;
7. import com.itheima.shiro.pojo.Role;
8. import com.itheima.shiro.pojo.User;
9. import org.apache.shiro.authz.AuthorizationInfo;
10. import org.apache.shiro.authz.SimpleAuthorizationInfo;
11. import org.springframework.beans.factory.annotation.Autowired;
12. import org.springframework.stereotype.Component;
13. 
14. import java.util.ArrayList;
15. import java.util.List;
16. 
17. /**
18.  * @Description:用户信息桥接(后期会做缓存)
19.  */
20. @Component("userBridgeService")
21. public class UserBridgeServiceImpl implements UserBridgeService {
22. 
23. @Autowired
24.     UserAdapter userAdapter;
25. 
26. @Override
27. public User findUserByLoginName(String loginName) {
28. 
29. return userAdapter.findUserByLoginName(loginName);
30.     }
31. 
32. @Override
33. public AuthorizationInfo getAuthorizationInfo(ShiroUser shiroUser) {
34. //查询用户对应的角色标识
35.         List<String> roleList = this.findRoleList(shiroUser.getId());
36. //查询用户对于的资源标识
37.         List<String> resourcesList = this.findResourcesList(shiroUser.getId());
38. //构建鉴权信息对象
39. SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
40.         simpleAuthorizationInfo.addRoles(roleList);
41.         simpleAuthorizationInfo.addStringPermissions(resourcesList);
42. return simpleAuthorizationInfo;
43.     }
44. 
45. @Override
46. public List<String> findRoleList(String userId){
47.         List<Role> roles = userAdapter.findRoleByUserId(userId);
48.         List<String> roleLabel = new ArrayList<>();
49. for (Role role : roles) {
50.             roleLabel.add(role.getLabel());
51.         }
52. return roleLabel;
53.     }
54. 
55. @Override
56. public List<String> findResourcesList(String userId){
57.         List<Resource> resources = userAdapter.findResourceByUserId(userId);
58.         List<String> resourceLabel = new ArrayList<>();
59. for (Resource resource : resources) {
60.             resourceLabel.add(resource.getLabel());
61.         }
62. return resourceLabel;
63.     }
64. 
65. @Override
66. public List<String> findResourcesIds(String userId) {
67.         List<Resource> resources = userAdapter.findResourceByUserId(userId);
68.         List<String> ids = new ArrayList<>();
69. for (Resource resource : resources) {
70.             ids.add(resource.getId());
71.         }
72. return ids;
73.     }
74. 
75. }
相关文章
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
1184 0
|
前端开发
SpringBoot2.3.1集成Knife4j接口文档
SpringBoot2.3.1集成Knife4j接口文档
1247 44
|
12月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
766 3
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
998 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
1079 2
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
582 2
|
缓存 安全 Java
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
973 7
|
缓存 Java 数据库
SpringBoot集成Ehcache缓存使用指南
以上是SpringBoot集成Ehcache缓存的基本操作指南,帮助你在实际项目中轻松实现缓存功能。当然,Ehcache还有诸多高级特性,通过学习和实践,你可以更好地发挥它的威力。
1006 20
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
334 0

热门文章

最新文章