WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.server.core.service.impl;
 
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tech.powerjob.common.enums.EncryptType;
import tech.powerjob.common.enums.ErrorCodes;
import tech.powerjob.common.exception.PowerJobException;
import tech.powerjob.common.utils.DigestUtils;
import tech.powerjob.server.common.utils.AESUtil;
import tech.powerjob.server.core.service.AppInfoService;
import tech.powerjob.server.persistence.remote.model.AppInfoDO;
import tech.powerjob.server.persistence.remote.repository.AppInfoRepository;
 
import java.util.Optional;
import java.util.concurrent.TimeUnit;
 
/**
 * AppInfoServiceImpl
 *
 * @author tjq
 * @since 2023/3/4
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppInfoServiceImpl implements AppInfoService {
 
    private final Cache<Long, AppInfoDO> appId2AppInfoDO = CacheBuilder.newBuilder()
            .softValues()
            .expireAfterWrite(3, TimeUnit.MINUTES)
            .maximumSize(1024)
            .build();
 
    private final AppInfoRepository appInfoRepository;
 
    private static final String ENCRYPT_KEY = "ChinaNo.1_ChinaNo.1_ChinaNo.1AAA";
 
    private static final String ENCRYPT_PWD_PREFIX = "sys_encrypt_aes:";
 
    @Override
    public Optional<AppInfoDO> findByAppName(String appName) {
        return appInfoRepository.findByAppName(appName);
    }
 
    @Override
    public Optional<AppInfoDO> findById(Long appId, boolean useCache) {
        if (!useCache) {
            Optional<AppInfoDO> appInfoOpt = appInfoRepository.findById(appId);
            appInfoOpt.ifPresent(appInfo -> appId2AppInfoDO.put(appId, appInfo));
            return appInfoOpt;
        }
        try {
            AppInfoDO appInfoDO = appId2AppInfoDO.get(appId, () -> {
                Optional<AppInfoDO> appInfoOpt = appInfoRepository.findById(appId);
                if (appInfoOpt.isPresent()) {
                    return appInfoOpt.get();
                }
                throw new IllegalArgumentException("can't find appInfo by appId:" + appId);
            });
            return Optional.of(appInfoDO);
        } catch (Exception e) {
            log.warn("[AppInfoService] findByIdWithCache failed,appId={}", appId, e);
        }
        return Optional.empty();
    }
 
    @Override
    public void deleteById(Long appId) {
        appInfoRepository.deleteById(appId);
    }
 
    @Override
    public AppInfoDO save(AppInfoDO appInfo) {
 
        String originPassword = appInfo.getPassword();
        String encryptPassword = AESUtil.encrypt(originPassword, ENCRYPT_KEY);
        String finalPassword = ENCRYPT_PWD_PREFIX.concat(encryptPassword);
        appInfo.setPassword(finalPassword);
 
        return appInfoRepository.saveAndFlush(appInfo);
    }
 
    public static void main(String[] args) {
        String encrypt = AESUtil.encrypt("1232432423423423", ENCRYPT_KEY);
//        String encryptPassword = AESUtil.encrypt(originPassword, ENCRYPT_KEY);
//        System.out.println(encryptPassword);
//        String finalPassword = ENCRYPT_PWD_PREFIX.concat(encryptPassword);
//        System.out.println(finalPassword);
        System.out.println(encrypt);
    }
 
    @Override
    public Long assertApp(String appName, String password, String encryptType) {
        AppInfoDO appInfo = appInfoRepository.findByAppName(appName).orElseThrow(() -> new PowerJobException(ErrorCodes.INVALID_APP, appName));
        return assertApp(appInfo, password, encryptType);
    }
 
    @Override
    public Long assertApp(AppInfoDO appInfo, String password, String encryptType) {
        boolean checkPass = checkPassword(appInfo, password, encryptType);
        if (!checkPass) {
            throw new PowerJobException(ErrorCodes.INCORRECT_PASSWORD, null);
        }
        return appInfo.getId();
    }
 
    private boolean checkPassword(AppInfoDO appInfo, String password, String encryptType) {
        String originPwd = fetchOriginAppPassword(appInfo);
        if (StringUtils.isEmpty(encryptType) || EncryptType.NONE.getCode().equalsIgnoreCase(encryptType)) {
            return password.equals(originPwd);
        }
        if (EncryptType.MD5.getCode().equalsIgnoreCase(encryptType)) {
            return password.equalsIgnoreCase(DigestUtils.md5(originPwd));
        }
        throw new PowerJobException(ErrorCodes.INVALID_REQUEST, "unknown_encryptType:" + encryptType);
    }
 
    @Override
    public String fetchOriginAppPassword(AppInfoDO appInfo) {
        String dbPwd = appInfo.getPassword();
        if (StringUtils.isEmpty(dbPwd)) {
            return dbPwd;
        }
 
        if (dbPwd.startsWith(ENCRYPT_PWD_PREFIX)) {
            String encryptPassword = dbPwd.replaceFirst(ENCRYPT_PWD_PREFIX, StringUtils.EMPTY);
            return AESUtil.decrypt(encryptPassword, ENCRYPT_KEY);
        }
 
        return dbPwd;
    }
 
}