guyue
2025-09-05 e4451cbe7eea81c397353e8d5649e52dcbd3b7d1
src/main/java/com/linghu/controller/AuthController.java
@@ -1,46 +1,42 @@
package com.linghu.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.dto.TokenRequest;
import com.linghu.service.AuthService;
import com.linghu.utils.OpenCryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
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 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;
import javax.validation.Valid;
@Api(value = "认证接口", tags = "认证管理")
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Value("${jwt.secret}")
    private String secretKey;
    @PostMapping("/login")
    public ResponseEntity<?> externalLogin(
            @RequestBody User user) {
    @Autowired
    private AuthService authService;
        // 生成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);
    //open-crypt解析token
    @PostMapping("/parseToken")
    @ApiOperation(value = "解析token")
    public ResponseResult<?> parseToken(@Valid @RequestBody TokenRequest tokenRequest) {
        return authService.parseTokenAuth(tokenRequest);
    }
    // 获取用户信息
    @PostMapping("/getUserInfo")
    public ResponseEntity<?> getUserInfo(@RequestParam String token) {
        // 解析JWT令牌,获取用户信息
        JwtUtils jwtUtils = new JwtUtils(secretKey, 3600);
        User user = jwtUtils.parseToken(token);
        // 返回用户信息
        return ResponseEntity.ok(user);
    @GetMapping("/getToken")
    @ApiOperation(value = "获取token")
    public ResponseEntity<?> getToken(@RequestParam(value = "user" ) String user) {
        // 创建JwtUtils对象,并生成JWT令牌
        OpenCryptUtil openCryptUtil = new OpenCryptUtil();
        String decrypt = openCryptUtil.encrypt(user);
        // 返回JWT令牌
        return ResponseEntity.ok(decrypt);
    }
}