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; |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 4 | import com.example.g8backend.dto.ApiResponse; |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 5 | import com.example.g8backend.service.IUserSecurityService; |
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 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 { |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 14 | |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 15 | @Autowired |
| 16 | private IUserSecurityService userSecurityService; |
| 17 | |
| 18 | @PutMapping("/change-password") |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 19 | public ApiResponse<String> changePassword(@RequestBody PasswordChangeDTO dto) { |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 20 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 21 | Long userId = (Long) authentication.getPrincipal(); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 22 | |
| 23 | // 调用服务层进行密码修改 |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 24 | userSecurityService.changePassword(userId, dto.getOldPassword(), dto.getNewPassword()); |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 25 | |
| 26 | // 返回统一的成功响应 |
| 27 | return ApiResponse.success("密码修改成功"); |
22301071 | 8e412ad | 2025-04-24 22:24:51 +0800 | [diff] [blame] | 28 | } |
夜雨声烦 | e73ff92 | 2025-05-13 18:49:03 +0800 | [diff] [blame] | 29 | } |