wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
| 3 | import com.example.g8backend.entity.User; |
| 4 | import com.example.g8backend.service.IUserService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 6 | import org.springframework.http.ResponseEntity; |
| 7 | import org.springframework.security.core.Authentication; |
| 8 | import org.springframework.security.core.context.SecurityContextHolder; |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 9 | import org.springframework.web.bind.annotation.*; |
| 10 | |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 11 | @RestController |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 12 | @RequestMapping("/user") |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 13 | public class UserController { |
| 14 | |
| 15 | @Autowired |
| 16 | private IUserService userService; |
| 17 | |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 18 | // 获取已登录的用户信息 |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 19 | @GetMapping |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 20 | 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); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 26 | } |
| 27 | } |