22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
| 3 | import com.example.g8backend.dto.PasswordChangeDTO; |
| 4 | import com.example.g8backend.service.IUserSecurityService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.http.ResponseEntity; |
| 7 | import org.springframework.security.core.Authentication; |
| 8 | import org.springframework.security.core.context.SecurityContextHolder; |
| 9 | import org.springframework.web.bind.annotation.*; |
| 10 | |
| 11 | @RestController |
| 12 | @RequestMapping("/user/security") |
| 13 | public class UserSecurityController { |
| 14 | @Autowired |
| 15 | private IUserSecurityService userSecurityService; |
| 16 | |
| 17 | @PutMapping("/change-password") |
| 18 | public ResponseEntity<?> changePassword(@RequestBody PasswordChangeDTO dto) { |
| 19 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 20 | Long userId = (Long) authentication.getPrincipal(); |
| 21 | userSecurityService.changePassword(userId, dto.getOldPassword(), dto.getNewPassword()); |
| 22 | return ResponseEntity.ok("密码修改成功"); |
| 23 | } |
| 24 | } |