blob: 2665b4c6ff194ac38be3a5131a07fd78e75de299 [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
3import com.example.g8backend.entity.User;
4import com.example.g8backend.service.IUserService;
5import org.springframework.beans.factory.annotation.Autowired;
wuchimedes223bfab2025-04-04 17:16:05 +08006import org.springframework.http.ResponseEntity;
7import org.springframework.security.core.Authentication;
8import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa1bf2782025-03-27 15:08:54 +08009import org.springframework.web.bind.annotation.*;
10
wuchimedesa1bf2782025-03-27 15:08:54 +080011@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080012@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080013public class UserController {
14
15 @Autowired
16 private IUserService userService;
17
wuchimedes223bfab2025-04-04 17:16:05 +080018 // 获取已登录的用户信息
wuchimedesa1bf2782025-03-27 15:08:54 +080019 @GetMapping
wuchimedes223bfab2025-04-04 17:16:05 +080020 public ResponseEntity<?> getUserInfo(){
21 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
22 long userId = (long) authentication.getPrincipal();
23 User user = userService.getById(userId);
24 user.setPassword(null);
25 return ResponseEntity.ok(user);
wuchimedesa1bf2782025-03-27 15:08:54 +080026 }
27}