1. import java.util.HashSet;
    2. import java.util.List;
    3. import java.util.Set;
    4. import org.apache.shiro.authc.AuthenticationException;
    5. import org.apache.shiro.authc.AuthenticationInfo;
    6. import org.apache.shiro.authc.AuthenticationToken;
    7. import org.apache.shiro.authc.LockedAccountException;
    8. import org.apache.shiro.authc.SimpleAuthenticationInfo;
    9. import org.apache.shiro.authc.UnknownAccountException;
    10. import org.apache.shiro.authc.UsernamePasswordToken;
    11. import org.apache.shiro.authc.credential.CredentialsMatcher;
    12. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    13. import org.apache.shiro.authz.AuthorizationException;
    14. import org.apache.shiro.authz.AuthorizationInfo;
    15. import org.apache.shiro.authz.SimpleAuthorizationInfo;
    16. import org.apache.shiro.realm.AuthorizingRealm;
    17. import org.apache.shiro.subject.PrincipalCollection;
    18. import org.apache.shiro.util.ByteSource;
    19. import org.springframework.beans.factory.annotation.Autowired;
    20. import org.springframework.stereotype.Service;
    21. import org.springframework.util.StringUtils;
    22. import com.db.sys.dao.SysMenuDao;
    23. import com.db.sys.dao.SysRoleMenuDao;
    24. import com.db.sys.dao.SysUserDao;
    25. import com.db.sys.dao.SysUserRoleDao;
    26. import com.db.sys.entity.SysUser;
    27. @Service
    28. public class ShiroUserRealm extends AuthorizingRealm {
    29. @Autowired
    30. private SysUserDao sysUserDao;
    31. @Autowired
    32. private SysUserRoleDao sysUserRoleDao;
    33. @Autowired
    34. private SysRoleMenuDao sysRoleMenuDao;
    35. @Autowired
    36. private SysMenuDao sysMenuDao;
    37. /**
    38. * 设置凭证匹配器
    39. */
    40. @Override
    41. public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
    42. //构建凭证匹配对象
    43. HashedCredentialsMatcher cMatcher=new HashedCredentialsMatcher();
    44. //设置加密算法
    45. cMatcher.setHashAlgorithmName("MD5");
    46. //设置加密次数
    47. cMatcher.setHashIterations(1);
    48. super.setCredentialsMatcher(cMatcher);
    49. }
    50. /**
    51. * 通过此方法完成认证数据的获取及封装,系统
    52. * 底层会将认证数据传递认证管理器,由认证
    53. * 管理器完成认证操作。
    54. */
    55. @Override
    56. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
    57. throws AuthenticationException {
    58. //1.获取用户名(用户页面输入)
    59. UsernamePasswordToken upToken=
    60. (UsernamePasswordToken)token;
    61. String username=upToken.getUsername();
    62. //2.基于用户名查询用户信息
    63. SysUser user=
    64. sysUserDao.findUserByUserName(username);
    65. //3.判定用户是否存在
    66. if(user==null)
    67. throw new UnknownAccountException();
    68. //4.判定用户是否已被禁用。
    69. if(user.getValid()==0)
    70. throw new LockedAccountException();
    71. //5.封装用户信息
    72. ByteSource credentialsSalt=
    73. ByteSource.Util.bytes(user.getSalt());
    74. //记住:构建什么对象要看方法的返回值
    75. SimpleAuthenticationInfo info=
    76. new SimpleAuthenticationInfo(
    77. user,//principal (身份)
    78. user.getPassword(),//hashedCredentials
    79. credentialsSalt, //credentialsSalt
    80. getName());//realName
    81. //6.返回封装结果
    82. return info;
    83. }
    84. /**通过此方法完成授权信息的获取及封装*/
    85. @Override
    86. protected AuthorizationInfo doGetAuthorizationInfo(
    87. PrincipalCollection principals) {
    88. //1.获取登录用户信息,例如用户id
    89. SysUser user=(SysUser)principals.getPrimaryPrincipal();
    90. Integer userId=user.getId();
    91. //2.基于用户id获取用户拥有的角色(sys_user_roles)
    92. List<Integer> roleIds=
    93. sysUserRoleDao.findRoleIdsByUserId(userId);
    94. if(roleIds==null||roleIds.size()==0)
    95. throw new AuthorizationException();
    96. //3.基于角色id获取菜单id(sys_role_menus)
    97. Integer[] array={};
    98. List<Integer> menuIds=
    99. sysRoleMenuDao.findMenuIdsByRoleIds(
    100. roleIds.toArray(array));
    101. if(menuIds==null||menuIds.size()==0)
    102. throw new AuthorizationException();
    103. //4.基于菜单id获取权限标识(sys_menus)
    104. List<String> permissions=
    105. sysMenuDao.findPermissions(
    106. menuIds.toArray(array));
    107. //5.对权限标识信息进行封装并返回
    108. Set<String> set=new HashSet<>();
    109. for(String per:permissions){
    110. if(!StringUtils.isEmpty(per)){
    111. set.add(per);
    112. }
    113. }
    114. SimpleAuthorizationInfo info=
    115. new SimpleAuthorizationInfo();
    116. info.setStringPermissions(set);
    117. return info;//返回给授权管理器
    118. }
    119. }