Sunshine
2024-11-04 177f10973fd9bc1f0930369bb086ad9f0053440c
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
package com.nova.sankuai.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nova.sankuai.domain.dto.LiandengPageJumpAutoDownloadDTO;
import com.nova.sankuai.domain.dto.LiandengPageJumpAutoLoginDTO;
import com.nova.sankuai.domain.entity.UnionloginRelay;
import com.nova.sankuai.infra.config.CommonException;
import com.nova.sankuai.infra.mapper.UnionloginRelayMapper;
import com.nova.sankuai.infra.utils.RsaSecretUtils;
import com.nova.sankuai.service.ILiandengPageJumpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
@Slf4j
@Service
public class ILiandengPageJumpServiceImpl implements ILiandengPageJumpService {
 
    @Resource
    RsaSecretUtils rsa;
 
    @Resource
    UnionloginRelayMapper unionloginRelayMapper;
 
    @Override
    public String checkAutoLogin(LiandengPageJumpAutoLoginDTO param) throws Exception {
        byte[] autoLoginBytes = rsa.base642Byte(param.getAutologin());
        JSONObject autoLogin = JSON.parseObject(new String(rsa.privateDecrypt(autoLoginBytes), StandardCharsets.UTF_8));
 
        if (param.getAppId() == null || !StringUtils.equals(param.getAppId(), autoLogin.getString("appId"))) {
            throw new CommonException("校验失败,appId 填写错误");
        }
        UnionloginRelay record = unionloginRelayMapper.selectByAppId(param.getAppId());
 
        if (checkParam(param.getAppId(), autoLogin, record)) {
            String appId = autoLogin.getString("appId");
            byte[] bytes = RsaSecretUtils.publicEncrypt(autoLogin.toJSONString().getBytes(StandardCharsets.UTF_8), record.getPublicKey());
            String enc = Base64.getEncoder().encodeToString(bytes);
            return String.format(record.getDistUrl(), appId, URLEncoder.encode(enc, "UTF-8"));
        }
        throw new CommonException("校验失败,不存在对应渠道");
    }
 
    private boolean checkParam(String appId, JSONObject json, UnionloginRelay record) {
        if (record == null) throw new CommonException("校验失败,appId 不存在");
        if (record.getEnable() != 1) throw new CommonException("跳转规则未启用");
        Pattern pattern = Pattern.compile(record.getChannelCodePattern());
        Matcher matcher = pattern.matcher(json.getString("channelCode"));
        if (matcher.matches()) {
            json.put("appId", record.getAppIdOut());
            return true;
        }
        return false;
    }
 
    @Override
    public boolean checkAutoDownload(LiandengPageJumpAutoDownloadDTO param) throws Exception {
        byte[] autodownloadBytes = rsa.base642Byte(param.getAutodownload());
        JSONObject autoDownload = JSON.parseObject(new String(rsa.privateDecrypt(autodownloadBytes), StandardCharsets.UTF_8));
 
        if (param.getAppId() == null || !StringUtils.equals(param.getAppId(), autoDownload.getString("appId"))) {
            throw new CommonException("校验失败,appId 填写错误");
        }
        UnionloginRelay record = unionloginRelayMapper.selectByAppId(param.getAppId());
 
        return checkParam(param.getAppId(), autoDownload, record);
    }
}