AuthenticationRealm.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package platform.modules.sys.shiro;
  2. import com.xiaoleilu.hutool.log.Log;
  3. import com.xiaoleilu.hutool.log.LogFactory;
  4. import org.apache.shiro.authc.*;
  5. import org.apache.shiro.authz.AuthorizationInfo;
  6. import org.apache.shiro.authz.SimpleAuthorizationInfo;
  7. import org.apache.shiro.realm.AuthorizingRealm;
  8. import org.apache.shiro.subject.PrincipalCollection;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.core.env.Environment;
  11. import platform.common.Constant;
  12. import platform.common.util.EhCacheUtils;
  13. import platform.modules.build.entity.BuildInfo;
  14. import platform.modules.build.entity.Company;
  15. import platform.modules.build.service.BuildInfoService;
  16. import platform.modules.build.service.CompanyService;
  17. import platform.modules.government.dao.UserDao;
  18. import platform.modules.government.entity.Street;
  19. import platform.modules.government.entity.User;
  20. import platform.modules.government.service.StreetService;
  21. import platform.modules.government.service.UserService;
  22. import platform.modules.sys.entity.Menu;
  23. import platform.modules.sys.service.MenuService;
  24. import javax.annotation.Resource;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Objects;
  28. /**
  29. * 身份校验核心类,包括认证和授权
  30. *
  31. * @author lhf
  32. */
  33. public class AuthenticationRealm extends AuthorizingRealm {
  34. protected final static Log log = LogFactory.get(AuthenticationRealm.class);
  35. @Resource
  36. private UserDao userDao;
  37. @Resource
  38. private StreetService streetService;
  39. @Resource
  40. private BuildInfoService buildInfoService;
  41. @Resource
  42. private CompanyService companyService;
  43. @Resource
  44. private MenuService menuService;
  45. @Autowired
  46. private Environment environment;
  47. @Autowired
  48. private UserService userService;
  49. /**
  50. * 认证 校验用户身份是否合法
  51. */
  52. @Override
  53. public AuthenticationInfo doGetAuthenticationInfo(
  54. AuthenticationToken authenticationToken) throws AuthenticationException {
  55. log.info("##################执行Shiro权限认证##################");
  56. UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
  57. User record = null;
  58. try {
  59. record = userDao.findByNickNameOnly(token.getUsername());
  60. //record = userDao.findByLogin(token.getUsername(),Constant.UserType.COMPANY);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. if (record == null || record.getDel_flag()) {
  65. throw new UnknownAccountException();// 没找到帐号
  66. }
  67. //内网(super,政府,街道可内网登录,园区,企业不能)
  68. if (environment.getProperty("spring.profiles").equals(Constant.Environment.PROD_IN)) {
  69. if (!(record.getUser_type().equals(Constant.UserType.STREET) || record.getUser_type().equals(Constant.UserType.GOVERNMENT) || record.getUser_type().equals(Constant.UserType.SUPER))) {
  70. throw new UnknownAccountException();
  71. }
  72. }
  73. ///content/list外网(super,政府,不能外网登录)
  74. else if (environment.getProperty("spring.profiles").equals(Constant.Environment.PROD_OUT)) {
  75. /*if (record.getUser_type().equals(Constant.UserType.SUPER)) {
  76. throw new UnknownAccountException();
  77. }else */
  78. if (record.getUser_type().equals(Constant.UserType.GOVERNMENT)) {
  79. //是否可以外网登录 在部门表中配置
  80. if (!userService.isOutLogin(record)) {
  81. throw new UnknownAccountException("");
  82. }
  83. }
  84. }
  85. //政府测试机 内外网都能登录
  86. else if (environment.getProperty("spring.profiles").equals(Constant.Environment.PROD_TEST)) {
  87. /*if (record.getUser_type().equals(Constant.UserType.SUPER)) {
  88. throw new UnknownAccountException();
  89. }else */
  90. /*if(record.getUser_type().equals(Constant.UserType.GOVERNMENT)) {
  91. if(!userService.isOutLogin(record)) {
  92. throw new UnknownAccountException();
  93. }
  94. }*/
  95. }
  96. if (Objects.equals(record.getIs_register(), 0)) {
  97. throw new LockedAccountException(Constant.USER_REVIEWING); // 帐号审核中
  98. }
  99. if (Boolean.TRUE.equals(!record.getIs_start())) {
  100. throw new LockedAccountException(Constant.USER_HAS_LOCK); // 帐号锁定
  101. }
  102. if (null != record.getUser_type()) {
  103. if (record.getUser_type().equals(Constant.UserType.BUILD)) {
  104. BuildInfo buildInfo = buildInfoService.findById(record.getBuild_id());
  105. if (null != buildInfo) {
  106. if (null == buildInfo.getIs_start() || !buildInfo.getIs_start()) {
  107. throw new LockedAccountException("园区被锁定,不能登录"); // 园区被禁用
  108. }
  109. } else {
  110. throw new LockedAccountException("园区不存在,不能登录"); // 园区被禁用
  111. }
  112. } else if (record.getUser_type().equals(Constant.UserType.STREET)) {
  113. Street street = streetService.findById(record.getStreet_id());
  114. if (null != street) {
  115. if (null == street.getIs_start() || !street.getIs_start()) {
  116. throw new LockedAccountException("街道被锁定,不能登录"); // 街道被禁用
  117. }
  118. } else {
  119. throw new LockedAccountException("街道不存在,不能登录"); // 园区被禁用
  120. }
  121. } else if (record.getUser_type().equals(Constant.UserType.COMPANY)) {
  122. Company company = companyService.findById(record.getCompany_id());
  123. if (null != company) {
  124. if (null == company.getIs_start() || !company.getIs_start()) {
  125. throw new LockedAccountException("公司被锁定,不能登录"); // 公司被禁用
  126. }
  127. } else {
  128. throw new LockedAccountException("公司不存在,不能登录"); // 园区被禁用
  129. }
  130. }
  131. }
  132. //将此用户存放到登录认证info中,无需自己做密码对比,Shiro使用CredentialsMatcher会为我们进行密码对比校验
  133. SimpleAccount authenticationInfo = new SimpleAccount(
  134. record, record.getPassword(), getName());
  135. // return authenticationInfo;
  136. return new SimpleAuthenticationInfo(record, record.getPassword(), getName());
  137. }
  138. @Override
  139. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  140. log.info("=========执行授权===========");
  141. //获取主身份信息
  142. User user = (User) principalCollection.getPrimaryPrincipal();
  143. List<Menu> menus;
  144. if (user.getIs_admin()) {
  145. Menu menu = new Menu();
  146. menu.setUser_type(user.getUser_type());
  147. menus = menuService.findListByWhere(menu);
  148. } else {
  149. menus = menuService.findListMenuByUserId(user.getId(), user.getUser_type());
  150. }
  151. //单独定一个集合对象
  152. List<String> permissions = new ArrayList<String>();
  153. if (menus != null) {
  154. for (Menu permission : menus) {
  155. //将数据库中的权限标签 符放入集合
  156. permissions.add(permission.getPermission());
  157. }
  158. }
  159. //查到权限数据,返回授权信息(要包括 上边的permissions)
  160. SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
  161. //将上边查询到授权信息填充到simpleAuthorizationInfo对象中
  162. simpleAuthorizationInfo.addStringPermissions(permissions);
  163. return simpleAuthorizationInfo;
  164. }
  165. /**
  166. * 清除所有用户授权缓存信息
  167. */
  168. public void clearCachedAuthorizationInfoAll() {
  169. String cacheName = super.getAuthorizationCacheName();
  170. EhCacheUtils.removeAll(cacheName);
  171. }
  172. }