jiazx0107@163.com
2023-11-10 715f40da0c953ec0080ccf056f9d759c3a249ea0
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
package com.fzzy.gateway.hx2023.controller;
 
import com.alibaba.fastjson2.JSONObject;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.utils.RedisConst;
import com.fzzy.api.utils.RedisUtil;
import com.fzzy.gateway.entity.GatewayConf;
import com.fzzy.gateway.hx2023.data.GatewayAuthData;
import com.fzzy.gateway.service.GatewayConfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 *
 */
@Slf4j
@Controller
@RequestMapping("/sc2023/gateway")
public class GatewayController {
 
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private GatewayConfService confService;
 
 
    /**
     * 鉴权接口
     *
     * @return {
     * "message": "成功",
     * "result": {
     * "token":"b2919bed493eca1ee1efb8d3cfa08a"
     * },
     * "status": 0,
     * "code": 200,
     * "timestamp": 0
     * }
     */
    @PostMapping("/authorize/login")
    public @ResponseBody
    JSONObject authorize(@RequestBody GatewayAuthData data) {
 
 
        List<GatewayConf> list = confService.getCacheConfList();
 
        JSONObject json = new JSONObject();
        json.put("timestamp", System.currentTimeMillis());
        if (null == list || list.isEmpty()) {
            json.put("code", 500);
            json.put("message", "未获取网关信息");
            return json;
        }
 
        String gatewayId = null;
        for (GatewayConf conf : list) {
            if (data.getUsername().equals(conf.getGatewayUsername()) && data.getPassword().equals(conf.getGatewayPassword())) {
                gatewayId = conf.getGatewayId();
                break;
            }
        }
 
        if (null == gatewayId) {
            json.put("code", 500);
            json.put("message", "未匹配到用户名和密码");
            return json;
        }
 
 
        String token = "fzzy-" + gatewayId;
 
        log.debug("============鉴权==========={}--{}--{}", data.getUsername(), data.getPassword(), token);
 
        this.updateGatewayToken(token, data.getUsername());
 
        JSONObject result = new JSONObject();
        result.put("token", token);
        json.put("result", result);
        json.put("message", "成功");
        json.put("status", 0);
        json.put("code", 200);
        return json;
    }
 
    public void updateGatewayToken(String token, String username) {
        String key = RedisConst.buildKey(RedisConst.KYE_GATE_TOKEN, username);
        redisUtil.set(key, token);
    }
 
    public String getGatewayToken(String username) {
        String key = RedisConst.buildKey(RedisConst.KYE_GATE_TOKEN, username);
        return (String) redisUtil.get(key);
    }
}