puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.common.constant.JwtConstants;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.DispatchMapper;
import com.stylefeng.guns.modular.system.dao.VersionManagementMapper;
import com.stylefeng.guns.modular.system.model.Dispatch;
import com.stylefeng.guns.modular.system.service.IDispatchService;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.UUIDUtil;
import com.stylefeng.guns.modular.system.warpper.LoginWarpper;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
 
 
@Service
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements IDispatchService {
 
    @Resource
    private DispatchMapper dispatchMapper;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Resource
    private VersionManagementMapper versionManagementMapper;
 
    private final String SALT = "WL:x9#";
 
 
    /**
     * 账号密码登录
     * @param account
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil dispatchLogin(String account, String password) throws Exception {
        Dispatch dispatch = this.queryByAccount(account);
        if(null == dispatch){
            return ResultUtil.error("账号无效");
        }
        if(dispatch.getState() == 2){
            return ResultUtil.error("账号被冻结");
        }
        if(!ShiroKit.md5(password, SALT).equals(dispatch.getPassword())){
            return ResultUtil.error("密码错误");
        }
 
        //调用单点登录的逻辑
        this.singlePointLogin(dispatch.getId());
 
        String token = this.getToken(dispatch, password);
        LoginWarpper loginWarpper = new LoginWarpper();
        loginWarpper.setId(dispatch.getId());
        loginWarpper.setToken(token);
        loginWarpper.setAppid(UUIDUtil.getRandomCode());
        loginWarpper.setPushOrder(dispatch.getPushOrder());
        return ResultUtil.success(loginWarpper);
    }
 
 
    /**
     * 从redis中获取用户id
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Integer getUserIdFormRedis(HttpServletRequest request) throws Exception {
        String requestHeader = request.getHeader(JwtConstants.AUTH_HEADER);
        if (requestHeader != null && requestHeader.startsWith("Bearer ")) {
            requestHeader = requestHeader.substring(requestHeader.indexOf(" ") + 1);
            String key = null;
            int length = requestHeader.length();
            if(length > 32){
                key = requestHeader.substring(length - 32);
            }else{
                key = requestHeader;
            }
            String value = redisUtil.getValue(key);
            return null != value ? Integer.valueOf(value) : null;
        }else{
            return null;
        }
    }
 
 
    /**
     * 编辑推单配置
     * @param pushOrder
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil editPushOrder(Integer pushOrder, Integer uid) throws Exception {
        Dispatch dispatch = this.selectById(uid);
        if(pushOrder == dispatch.getPushOrder()){
            return ResultUtil.error("不允许重复配置");
        }
        dispatch.setPushOrder(pushOrder);
        this.updateById(dispatch);
        return ResultUtil.success();
    }
 
    public Dispatch queryByAccount(String account){
        return dispatchMapper.queryByAccount(account);
    }
 
 
 
    /**
     * 获取token
     * @param dispatch
     * @param password
     * @return
     */
    private String getToken(Dispatch dispatch, String password) throws Exception{
        //封装请求账号密码为shiro可验证的token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(dispatch.getAccount(), password.toCharArray());
        String credentials = dispatch.getPassword();
        ByteSource credentialsSalt = new Md5Hash(SALT);
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                new ShiroUser(), credentials, credentialsSalt, "");
 
        //校验用户账号密码
        HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
        md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
        md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
        boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
                usernamePasswordToken, simpleAuthenticationInfo);
        String s = JwtTokenUtil.generateToken(String.valueOf(dispatch.getId()));
        redisUtil.setStrValue(s.substring(s.length() - 32), String.valueOf(dispatch.getId()), 7 * 24 * 60 * 60);
        redisUtil.setStrValue(dispatch.getPhone(), s.substring(s.length() - 32));
        redisUtil.setStrValue("DISPATCH_" + dispatch.getId(), s);
        return s;
    }
 
 
 
    /**
     * 单点登录
     * @param id
     */
    private void singlePointLogin(Integer id) throws Exception{
        //开始验证当前账号是否在别处登录
        String value = redisUtil.getValue("DISPATCH_" + id);
        if(ToolUtil.isNotEmpty(value)){//将另外设备上的强迫下线
            //开始清除redis中无效的数据
            Dispatch dispatch = dispatchMapper.selectById(id);
            String key = redisUtil.getValue(dispatch.getPhone());
            redisUtil.remove(key);//删除个人信息数据
            redisUtil.remove(dispatch.getPhone());//删除后台冻结相关缓存
            redisUtil.remove("DISPATCH_" + id);//清除存储的token
        }
    }
}