xiaoyong931011
2022-07-07 bc43681f185af1edf833cf6c94833cb1cdd44a8e
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
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.configurations.properties.SecurityProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import com.xcong.farmer.cms.modules.system.util.CaptchaUtil;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.util.UUIDUtil;
import com.xcong.farmer.cms.utils.RedisUtils;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.annotation.Resource;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.asymmetric.RSA;
 
@Service
@Slf4j
public class CommonServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements ICommonService {
 
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ApplicationProperties applicationProperties;
    @Resource
    private SecurityProperties securityProperties;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UUIDUtil uuidUtil;
    @Autowired
    private CaptchaUtil captchaUtil;
    //从SpringBoot的配置文件中取出过期时间
    @Value("${server.servlet.session.timeout}")
    private Integer timeout;
 
    @Override
    public Result login(AdminLoginDto adminLoginDto) {
 
        //根据前端传回的token在redis中找对应的value
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        String codeToken = adminLoginDto.getCodeToken();
        String codeValue = adminLoginDto.getCodeValue();
        if (redisTemplate.hasKey(codeToken)) {
            //验证通过, 删除对应的key
            if (valueOperations.get(codeToken).equals(codeValue)) {
                redisTemplate.delete(codeToken);
            } else {
                return Result.fail("验证码不正确");
            }
        } else {
            return Result.fail("验证码已过期");
        }
        String username = adminLoginDto.getUsername();
        String password = adminLoginDto.getPassword();
        UserEntity userEntity = userMapper.selectByUserNameAndPassword(username, SecureUtil.md5(password));
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("请输入正确的账号和密码");
        }
        Integer status = userEntity.getStatus();
        if(UserEntity.STATUS_DISABLED.equals(status)){
            return Result.fail("账号禁止登陆,请联系管理员");
        }
        //生成UUID作为token
        String token = IdUtil.simpleUUID();
        String redisToken = AppContants.APP_LOGIN_PREFIX + token;
        String redisMember = AppContants.APP_LOGIN_PREFIX + userEntity.getId();
 
        if (StrUtil.isNotBlank(redisUtils.getString(redisMember))) {
            redisUtils.del(AppContants.APP_LOGIN_PREFIX + redisUtils.getString(redisMember));
        }
        redisUtils.set(redisToken, JSONObject.toJSONString(userEntity), 3000L);
        redisUtils.set(redisMember, token);
 
        Map<String, Object> authInfo = new HashMap<>();
        // 开启debug模式,则将加密后的token返回
        if (applicationProperties.isDebug()) {
            authInfo.put("token", token);
            authInfo.put("rsaToken", AppContants.TOKEN_START_WITH + generateAsaToken(token));
            authInfo.put("user", userEntity);
        } else {
            authInfo.put("token", token);
            authInfo.put("user", userEntity);
        }
        return Result.ok("登录成功", authInfo);
    }
 
    @Override
    public Result memberLogout() {
        Long id = LoginUserUtil.getLoginUser().getId();
        //获取用户ID
        UserEntity userEntity = userMapper.selectById(id);
        if (ObjectUtil.isEmpty(userEntity)) {
            return Result.fail("用户不存在");
        }
 
        String redisMember = AppContants.APP_LOGIN_PREFIX + userEntity.getId();
        String token = redisUtils.getString(redisMember);
        redisUtils.del(AppContants.APP_LOGIN_PREFIX + token);
        SecurityContextHolder.clearContext();
        return Result.ok("退出成功");
    }
 
    @Override
    public Map<String, Object> createToken(String captcha) {
        //生成一个token
        String key = uuidUtil.getUUID32();
        //生成验证码对应的token  以token为key  验证码为value存在redis中
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, captcha);
        //设置验证码过期时间
        redisTemplate.expire(key, timeout, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<>();
        map.put("token", key);
        map.put("expire", timeout);
        return map;
    }
 
    @Override
    public Result captchaCreator() throws IOException {
        return captchaUtil.catchaImgCreator();
    }
 
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, securityProperties.getPublicKey());
        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
    }
}