blob: 2665b4c6ff194ac38be3a5131a07fd78e75de299 [file] [log] [blame]
package com.example.g8backend.controller;
import com.example.g8backend.entity.User;
import com.example.g8backend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
// 获取已登录的用户信息
@GetMapping
public ResponseEntity<?> getUserInfo(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
long userId = (long) authentication.getPrincipal();
User user = userService.getById(userId);
user.setPassword(null);
return ResponseEntity.ok(user);
}
}