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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.vincent.rsf.openApi.controller;
 
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.openApi.entity.constant.Constants;
import com.vincent.rsf.openApi.entity.dto.CommonResponse;
import com.vincent.rsf.openApi.entity.AppAuthParam;
import com.vincent.rsf.openApi.security.service.AppAuthService;
import com.vincent.rsf.openApi.security.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
 
/**
 * App认证控制器
 * 
 * 提供AppId和AppSecret的登录认证功能
 * 
 * @author vincent
 * @since 2026-01-05
 */
@RestController
//@RequestMapping("/auth")
@Api(tags = "应用认证管理")
@Slf4j
public class AuthController {
 
    // 开启模拟数据
    @Value("${foreign.api.data.simulated}")
    public static String SIMULATED_DATA_ENABLE = "1";
 
    @Resource
    private AppAuthService appAuthService;
 
 
    /**
     * 获取App认证Token
     *
     * @param param 应用ID和应用密钥
     * @return 认证Token
     */
    @ApiOperation("获取App认证Token")
    @PostMapping("/getToken")
    public CommonResponse getToken(@RequestBody AppAuthParam param) {
        String appId = param.getAppId();
        String appSecret = param.getAppSecret();
 
        if (Cools.isEmpty(appId, appSecret)) {
            return CommonResponse.error("AppId和AppSecret不能为空");
        }
 
        boolean isValid = appAuthService.validateApp(appId, appSecret);
        if (isValid) {
            String token = Constants.TOKEN_PREFIX + TokenUtils.generateToken(appId, appSecret);  //appAuthService.generateAppToken(appId, appSecret);
            return CommonResponse.ok()
                    .setMsg("获取Token成功")
                    .setData(token);
        } else {
            return CommonResponse.error("AppId或AppSecret无效");
        }
    }
 
//    /**
//     * 验证Token的接口
//     *
//     * @param token 要验证的Token
//     * @return Token验证结果
//     */
//    @PostMapping("/validateToken")
//    public Map<String, Object> validateToken(@RequestHeader(name = "authorization") String token) {
//        log.info("验证Token: {}", token.substring(0, Math.min(10, token.length())) + "...");
//
//        boolean isValid = TokenUtils.validateToken(token);
//
//        Map<String, Object> response = new HashMap<>();
//        response.put("code", "200");
//        response.put("message", isValid ? "Token有效" : "Token无效");
//        response.put("data", Map.of(
//                "valid", isValid,
//                "appId", isValid ? TokenUtils.getAppIdFromToken(token) : null,
//                "userId", isValid ? TokenUtils.getUserIdFromToken(token) : null
//        ));
//        response.put("success", isValid);
//
//        return response;
//    }
 
//    /**
//     * AppId和AppSecret登录认证
//     *
//     * @param param 认证参数
//     * @return 认证结果
//     */
//    @ApiOperation("AppId和AppSecret登录认证")
//    @PostMapping("/login")
//    public CommonResponse login(@RequestBody AppAuthParam param) {
//        String appId = param.getAppId();
//        String appSecret = param.getAppSecret();
//
//        if (Cools.isEmpty(appId, appSecret)) {
//            return CommonResponse.error("AppId和AppSecret不能为空");
//        }
//
//        boolean isValid = appAuthService.validateApp(appId, appSecret);
//        if (isValid) {
//            // 生成Token
//            String token = appAuthService.generateAppToken(appId, appSecret);
//            return CommonResponse.ok()
//                    .setMsg("登录成功")
//                    .setData(token);
//        } else {
//            return CommonResponse.error("AppId或AppSecret无效");
//        }
//    }
//
//
//
//    /**
//     * 验证App认证
//     *
//     * @param request HTTP请求
//     * @return 验证结果
//     */
//    @ApiOperation("验证App认证")
//    @PostMapping("/validate")
//    public CommonResponse validate(HttpServletRequest request) {
//        String appId = request.getHeader(Constants.HEADER_APP_ID);
//        String appSecret = request.getHeader(Constants.HEADER_APP_SECRET);
//
//        if (Cools.isEmpty(appId, appSecret)) {
//            return CommonResponse.error("缺少AppId或AppSecret");
//        }
//
//        boolean isValid = appAuthService.validateApp(appId, appSecret);
//        if (isValid) {
//            return CommonResponse.ok()
//                    .setMsg("验证成功")
//                    .setData(appAuthService.getAppInfo(appId));
//        } else {
//            return CommonResponse.error("验证失败");
//        }
//    }
//
//    /**
//     * 获取当前认证的App信息
//     *
//     * @param request HTTP请求
//     * @return App信息
//     */
//    @ApiOperation("获取当前认证的App信息")
//    @GetMapping("/info")
//    public CommonResponse getAppInfo(HttpServletRequest request) {
//        String appId = (String) request.getAttribute("APP_ID");
//        if (appId == null) {
//            appId = request.getHeader(Constants.HEADER_APP_ID);
//        }
//
//        if (appId == null) {
//            return CommonResponse.error("未找到AppId");
//        }
//
//        var appInfo = appAuthService.getAppInfo(appId);
//        if (appInfo != null) {
//            return CommonResponse.ok()
//                    .setMsg("获取App信息成功")
//                    .setData(appInfo);
//        } else {
//            return CommonResponse.error("未找到App信息");
//        }
//    }
}