| | |
| | | package com.linghu.controller; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | 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 com.linghu.model.entity.User; |
| | | import com.linghu.utils.JwtUtils; |
| | | |
| | | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | |
| | | @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("/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); |
| | | } |
| | | // @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") |
| | | @PostMapping("/parseToken") |
| | | @ApiOperation(value = "解析token") |
| | | public ResponseEntity<?> parseToken(@RequestParam String token) { |
| | | if (token == null || "".equals(token)){ |
| | | return ResponseEntity.ok("token为空"); |
| | | public ResponseResult<?> parseToken(@RequestBody TokenRequest tokenRequest) { |
| | | String token = tokenRequest.getToken(); |
| | | if (token == null || token.isEmpty()) { |
| | | return ResponseResult.success("token为空"); |
| | | } |
| | | // 解析token,获取用户信息 |
| | | OpenCryptUtil openCryptUtil = new OpenCryptUtil(); |
| | | String decrypt = openCryptUtil.decrypt(token); |
| | | System.out.println(decrypt); |
| | | System.out.println("----------------"); |
| | | |
| | | // 返回用户信息 |
| | | return ResponseEntity.ok(decrypt); |
| | | // 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(@RequestBody User user) { |
| | | public ResponseEntity<?> getToken( String user) { |
| | | // 创建用户对 |
| | | |
| | | // 创建JwtUtils对象,并生成JWT令牌 |