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信息");
|
// }
|
// }
|
}
|