guyue
2025-07-17 d381770738b4862608620faa6fa78c7cea60b920
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
package com.linghu.controller;
 
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linghu.utils.OpenCryptUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import com.linghu.model.entity.User;
import com.linghu.utils.JwtUtils;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
 
@Api(value = "认证接口", tags = "认证管理")
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Value("${jwt.secret}")
    private String secretKey;
 
//    @PostMapping("/login")
//    @ApiOperation(value = "外部登录")
//    public ResponseEntity<?> externalLogin(
//            @RequestBody User user) {
//
//        // 生成JWT令牌
//        JwtUtils jwtUtils = new JwtUtils(secretKey, 3600);
//        String token = jwtUtils.generateToken(user);
//
//        Map<String, String> response = new HashMap<>();
//        response.put("token", token);
//        // 返回JWT令牌
//        return ResponseEntity.ok(response);
//    }
 
    // 获取用户信息
//    @PostMapping("/getUserInfo")
//    @ApiOperation(value = "获取用户信息")
//    public ResponseEntity<?> getUserInfo(@RequestParam String token) {
//        // 解析JWT令牌,获取用户信息
//        JwtUtils jwtUtils = new JwtUtils(secretKey, 3600);
//        User user = jwtUtils.parseToken(token);
//        // 返回用户信息
//        return ResponseEntity.ok(user);
//    }
    //open-crypt解析token
    @GetMapping("/parseToken")
    @ApiOperation(value = "解析token")
    public ResponseEntity<?> parseToken(@RequestParam String token) {
        if (token == null || token.isEmpty()) {
            return ResponseEntity.ok("token为空");
        }
 
        try {
            // 解密token
            OpenCryptUtil openCryptUtil = new OpenCryptUtil();
            String decrypt = openCryptUtil.decrypt(token);
 
            // 处理可能的JSON格式问题
            String cleanedJson = cleanJsonString(decrypt);
 
            // 使用ObjectMapper解析JSON
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode root = objectMapper.readTree(cleanedJson);
 
            // 提取所需字段
            String name = root.get("name").asText();
 
            return ResponseEntity.ok(name);
        } catch (JsonProcessingException e) {
            // 处理JSON解析异常
            return ResponseEntity.badRequest().body("JSON解析失败: " + e.getMessage());
        } catch (Exception e) {
            // 处理其他异常
            return ResponseEntity.badRequest().body("解析失败: " + e.getMessage());
        }
    }
 
    /**
     * 清理JSON字符串,处理可能的格式问题
     * @param jsonString 原始JSON字符串
     * @return 清理后的JSON字符串
     */
    private String cleanJsonString(String jsonString) {
        if (jsonString == null) {
            return null;
        }
 
        // 移除字符串开头和结尾可能存在的引号
        String result = jsonString.trim();
        if (result.startsWith("\"") && result.endsWith("\"") && result.length() > 1) {
            result = result.substring(1, result.length() - 1);
        }
 
        // 处理转义的引号
        result = result.replace("\\\"", "\"");
 
        // 处理换行符和其他转义字符
        result = result.replace("\\n", "\n")
                .replace("\\r", "\r")
                .replace("\\t", "\t");
 
        return result;
    }
 
    @GetMapping("/getToken")
    @ApiOperation(value = "获取token")
    public ResponseEntity<?> getToken( String user) {
        // 创建用户对
 
        // 创建JwtUtils对象,并生成JWT令牌
        OpenCryptUtil openCryptUtil = new OpenCryptUtil();
        String decrypt = openCryptUtil.encrypt(user);
        // 返回JWT令牌
        return ResponseEntity.ok(decrypt);
    }
}