guyue
4 天以前 89f8649e8cf9bc12b9e29abb0adc4f9b77273143
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
 
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);
    }
}