jiangping
2023-09-21 d5bdf4cd56be6bd0e7afa81d75c35a9f15ae2a40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.doumee.config.shiro;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.system.model.SystemPermission;
import com.doumee.dao.system.model.SystemRole;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.CompanyService;
import com.doumee.service.system.SystemDataPermissionService;
import com.doumee.service.system.SystemPermissionService;
import com.doumee.service.system.SystemRoleService;
import com.doumee.service.system.SystemUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
/**
 * 自定义Realm,处理认证和权限
 * @author Eva.Caesar Liu
 * @date 2022/03/15 09:54
 */
@Component
public class ShiroRealm extends AuthorizingRealm {
 
    @Lazy
    @Autowired
    private SystemDataPermissionService systemDataPermissionService;
    @Lazy
    @Autowired
    private SystemUserService systemUserService;
 
    @Lazy
    @Autowired
    private SystemRoleService systemRoleService;
 
    @Lazy
    @Autowired
    private SystemPermissionService systemPermissionService;
 
    @Lazy
    @Autowired
    private CompanyService companyService;
 
    /**
     * 权限处理
     * @author Eva.Caesar Liu
     * @date 2022/03/15 09:54
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        LoginUserInfo loginUserInfo = (LoginUserInfo)principalCollection.getPrimaryPrincipal();
        // 设置用户角色和权限
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRoles(loginUserInfo.getRoles());
        authorizationInfo.addStringPermissions(loginUserInfo.getPermissions());
        return authorizationInfo;
    }
 
    /**
     * 认证处理
     * @author Eva.Caesar Liu
     * @date 2022/03/15 09:54
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException{
        // 获取用户名
        String username = authenticationToken.getPrincipal().toString();
        // 根据用户名查询用户对象
        SystemUser queryDto = new SystemUser();
        queryDto.setUsername(username);
        queryDto.setDeleted(Boolean.FALSE);
        SystemUser user = systemUserService.findOne(queryDto);
        if(user == null){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,账号或密码不正确!");
        }
        if( !Constants.equalsInteger(user.getType(),Constants.UserType.ZHUBO.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非主播账户身份,如有疑问请联系系统管理员!!");
        }
        if(!Constants.equalsInteger(user.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,账号信息已被禁用,如有疑问请联系系统管理员!");
        }
        if(!user.getType().equals(Constants.UserType.SYSTEM)){
            Company company = companyService.findById(user.getCompanyId());
            if(company.getStatus().equals(Constants.ONE)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,企业信息已被禁用,如有疑问请联系系统管理员!");
            }
            if(DateUtil.compareDate(new Date(),DateUtil.addDaysToDate(company.getOepnValidDate(),1))<=Constants.ZERO){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业账号已过期,如需继续使用,请联系您的业务经理");
            }
            user.setCompany(company);
        }
        // 获取登录用户信息
        List<SystemRole> roles = systemRoleService.findByUserId(user.getId());
        List<SystemPermission> permissions = systemPermissionService.findByUserId(user.getId());
        LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions);
        // 验证用户
        return new SimpleAuthenticationInfo(userInfo, user.getPassword(), this.getName());
    }
 
}