|
| 1 | +package com.example.springbootshiro.config; |
| 2 | + |
| 3 | +import com.example.springbootshiro.domain.MenuInfo; |
| 4 | +import com.example.springbootshiro.domain.RoleInfo; |
| 5 | +import com.example.springbootshiro.domain.UserInfo; |
| 6 | +import com.example.springbootshiro.service.MenuService; |
| 7 | +import com.example.springbootshiro.service.RoleService; |
| 8 | +import com.example.springbootshiro.service.UserService; |
| 9 | +import org.apache.shiro.SecurityUtils; |
| 10 | +import org.apache.shiro.authc.*; |
| 11 | +import org.apache.shiro.authz.AuthorizationInfo; |
| 12 | +import org.apache.shiro.authz.SimpleAuthorizationInfo; |
| 13 | +import org.apache.shiro.realm.AuthorizingRealm; |
| 14 | +import org.apache.shiro.subject.PrincipalCollection; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | + |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +public class ShiroRealm extends AuthorizingRealm { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private UserService userService; |
| 25 | + @Autowired |
| 26 | + private RoleService roleService; |
| 27 | + @Autowired |
| 28 | + private MenuService menuService; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) { |
| 32 | + UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal(); |
| 33 | + String userName = user.getUsername(); |
| 34 | + |
| 35 | + SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); |
| 36 | + |
| 37 | + List<RoleInfo> roleList = this.roleService.findUserRole(userName); |
| 38 | + Set<String> roleSet = new HashSet<String>(); |
| 39 | + for (RoleInfo r : roleList) { |
| 40 | + roleSet.add(r.getRoleName()); |
| 41 | + } |
| 42 | + simpleAuthorizationInfo.setRoles(roleSet); |
| 43 | + |
| 44 | + List<MenuInfo> permissionList = this.menuService.findUserPermissions(userName); |
| 45 | + Set<String> permissionSet = new HashSet<String>(); |
| 46 | + for (MenuInfo m : permissionList) { |
| 47 | + permissionSet.add(m.getPerms()); |
| 48 | + } |
| 49 | + simpleAuthorizationInfo.setStringPermissions(permissionSet); |
| 50 | + return simpleAuthorizationInfo; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { |
| 55 | + String userName = (String) token.getPrincipal(); |
| 56 | + String password = new String((char[]) token.getCredentials()); |
| 57 | + |
| 58 | + UserInfo user = this.userService.findByName(userName); |
| 59 | + |
| 60 | + if (user == null) { |
| 61 | + throw new UnknownAccountException("用户名或密码错误!"); |
| 62 | + } |
| 63 | + if (!password.equals(user.getPassword())) { |
| 64 | + throw new IncorrectCredentialsException("用户名或密码错误!"); |
| 65 | + } |
| 66 | + if ("0".equals(user.getStatus())) { |
| 67 | + throw new LockedAccountException("账号已被锁定,请联系管理员!"); |
| 68 | + } |
| 69 | + SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName()); |
| 70 | + return info; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments