package com.linghu.controller;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.linghu.model.common.ResponseResult;
|
import com.linghu.model.dto.TokenRequest;
|
import com.linghu.utils.OpenCryptUtil;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
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
|
@PostMapping("/parseToken")
|
@ApiOperation(value = "解析token")
|
public ResponseResult<?> parseToken(@RequestBody TokenRequest tokenRequest) {
|
String token = tokenRequest.getToken();
|
if (token == null || token.isEmpty()) {
|
return ResponseResult.success("token为空");
|
}
|
|
// try {
|
// 解密token
|
OpenCryptUtil openCryptUtil = new OpenCryptUtil();
|
String decrypt = openCryptUtil.decrypt(token);
|
//转换为对象
|
try {
|
|
return ResponseResult.success(decrypt);
|
} catch (Exception e) {
|
return ResponseResult.error(e.getMessage());
|
}
|
|
// // 处理可能的JSON格式问题
|
// String cleanedJson = cleanJsonString(decrypt);
|
//
|
// // 使用ObjectMapper解析JSON
|
// ObjectMapper objectMapper = new ObjectMapper();
|
// JsonNode root = objectMapper.readTree(cleanedJson);
|
|
// 提取所需字段
|
// String name = root.get("name").asText();
|
|
// } 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);
|
}
|
}
|