From fd19536cbf9e5acec9bf7270f3f46037e822827d Mon Sep 17 00:00:00 2001
From: wuyouming666 <2265557248@qq.com>
Date: 星期二, 29 八月 2023 16:44:45 +0800
Subject: [PATCH] 添加菜单折叠自适应宽度 修改密码 修改用户角色 中英文切换全局配置
---
CanadaMes-back/src/main/java/com/canadames/controller/UserController.java | 73 +++++++++++++++++++++++++++++++++---
1 files changed, 66 insertions(+), 7 deletions(-)
diff --git a/CanadaMes-back/src/main/java/com/canadames/controller/UserController.java b/CanadaMes-back/src/main/java/com/canadames/controller/UserController.java
index e4f66c6..35caaa8 100644
--- a/CanadaMes-back/src/main/java/com/canadames/controller/UserController.java
+++ b/CanadaMes-back/src/main/java/com/canadames/controller/UserController.java
@@ -11,6 +11,7 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.SimpleHash;
@@ -22,6 +23,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import java.util.Map;
+
@RestController
@Slf4j
@RequestMapping("/api/user")
@@ -29,6 +32,46 @@
public class UserController {
@Autowired
private UserService userService;
+
+
+ @ApiOperation(value = "淇敼瀵嗙爜")
+ @PostMapping("/changePassword")
+ @RequiresAuthentication
+ public Result changePassword(@RequestBody Map<String, String> request) {
+ User currentUser = SecurityUtil.getCurrentUser();
+ String oldPassword = request.get("oldPassword");
+ String newPassword = request.get("newPassword");
+
+ // 鏍¢獙鏃у瘑鐮佹槸鍚︽纭�
+ if (!verifyPassword(currentUser, oldPassword)) {
+ return Result.fail("鏃у瘑鐮佷笉姝g‘");
+ }
+
+ // 鏇存柊瀵嗙爜
+ updatePassword(currentUser, newPassword);
+
+ return Result.success("瀵嗙爜淇敼鎴愬姛");
+ }
+ /**
+ * 楠岃瘉瀵嗙爜鏄惁姝g‘
+ */
+ private boolean verifyPassword(User user, String password) {
+ Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+ String md5 = new SimpleHash("MD5", password, salt, 1024).toHex();
+ return md5.equals(user.getPassword());
+ }
+
+ /**
+ * 鏇存柊瀵嗙爜
+ */
+ private void updatePassword(User user, String newPassword) {
+ Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+ String md5 = new SimpleHash("MD5", newPassword, salt, 1024).toHex();
+ user.setPassword(md5);
+ userService.saveOrUpdate(user);
+ }
+
+
@ApiOperation(value = "鍒嗛〉鏌ヨ鐢ㄦ埛")
@GetMapping("/selectPage")
@@ -38,7 +81,7 @@
return Result.success(userService.selectPage(userVO));
}
- @ApiOperation(value = "娣诲姞鎴栨垨鑰呬慨鏀圭敤鎴�")
+ @ApiOperation(value = "娣诲姞鎴栦慨鏀圭敤鎴�")
@PostMapping("/saveOrUpdate")
@RequiresRoles({"admin"})
@RequiresPermissions({"user:update", "user:add"})
@@ -46,14 +89,27 @@
if ("admin".equals(user.getUsername())) {
return Result.fail("绠$悊鍛樹笉鍙互琚鐢�");
}
- Integer count = userService.lambdaQuery().eq(User::getUsername, user.getUsername())
+ Integer count = userService.lambdaQuery()
+ .eq(User::getUsername, user.getUsername())
.ne(user.getId() != null, User::getId, user.getId())
.count();
- if (count > 0) return Result.fail("鐢ㄦ埛鍚嶅凡瀛樺湪");
- // 閫氳繃shiro榛樿鐨勫姞瀵嗗伐鍏风被涓烘敞鍐岀敤鎴风殑瀵嗙爜杩涜鍔犲瘑
- Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
- String md5 = new SimpleHash("MD5", user.getPassword(), salt, 1024).toHex();
- user.setPassword(md5);
+ if (count > 0) {
+ return Result.fail("鐢ㄦ埛鍚嶅凡瀛樺湪");
+ }
+
+ // 濡傛灉瀵嗙爜鏈慨鏀癸紝鍒欎笉杩涜鍔犲瘑鎿嶄綔
+ if (user.getId() != null) {
+ User existingUser = userService.getById(user.getId());
+ if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
+ user.setPassword(existingUser.getPassword());
+ } else {
+ // 瀵嗙爜鍙戠敓浜嗗彉鍖栵紝杩涜鍔犲瘑鎿嶄綔
+ Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+ String md5 = new SimpleHash("MD5", user.getPassword(), salt, 1024).toHex();
+ user.setPassword(md5);
+ }
+ }
+
userService.saveOrUpdate(user);
return Result.success();
}
@@ -86,4 +142,7 @@
SecurityUtils.getSubject().logout();
return Result.success("娉ㄩ攢鎴愬姛");
}
+
+ private class UpdatePasswordRequest {
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0