package com.linghu.controller; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.linghu.model.entity.User; import com.linghu.utils.JwtUtils; @RestController @RequestMapping("/auth") public class AuthController { @Value("${jwt.secret}") private String secretKey; @PostMapping("/login") public ResponseEntity externalLogin( @RequestBody User user) { // 生成JWT令牌 JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); String token = jwtUtils.generateToken(user); Map response = new HashMap<>(); response.put("token", token); // 返回JWT令牌 return ResponseEntity.ok(response); } // 获取用户信息 @PostMapping("/getUserInfo") public ResponseEntity getUserInfo(@RequestParam String token) { // 解析JWT令牌,获取用户信息 JwtUtils jwtUtils = new JwtUtils(secretKey, 3600); User user = jwtUtils.parseToken(token); // 返回用户信息 return ResponseEntity.ok(user); } }