From 1d9317aaf0364cb10d365166cb7696811e14d1d2 Mon Sep 17 00:00:00 2001
From: wuyouming666 <2265557248@qq.com>
Date: 星期五, 08 九月 2023 17:22:57 +0800
Subject: [PATCH] 补充删除的文件
---
springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java | 178 ++++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 146 insertions(+), 32 deletions(-)
diff --git a/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java b/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
index 2200978..69ffbaa 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
@@ -1,56 +1,170 @@
package com.example.springboot.controller;
-import com.example.springboot.common.Result;
+import com.example.springboot.security.constant.SystemConstant;
+import com.example.springboot.security.util.SecurityUtil;
import com.example.springboot.entity.User;
-import com.example.springboot.mapper.UserMapper;
+import com.example.springboot.entity.vo.Result;
+import com.example.springboot.entity.vo.UserVo;
import com.example.springboot.service.UserService;
-
+import io.swagger.annotations.Api;
+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;
+import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
-import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-//@CrossOrigin
@RestController
-@RequestMapping("/user")
+@Slf4j
+@RequestMapping("/api/user")
+@Api(tags = "鐢ㄦ埛")
public class UserController {
-
@Autowired
- UserMapper userMapper;
+ private UserService userService;
- @Autowired
- UserService userservice;
- @GetMapping("/page") // /user/all?name=xxxx
- public Result selectAll(@RequestParam String name, @RequestParam Integer start, @RequestParam Integer pageSize) {
- // return userMapper.selectAll("%" + name + "%");
- List<User> userList = userMapper.selectPage(name, start, pageSize);
- Integer total = userMapper.selectTotal(name);
+ @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");
- Map<String, Object> map = new HashMap<>();
- map.put("list", userList);
- map.put("total", total);
- return Result.success(map);
+ // 鏍¢獙鏃у瘑鐮佹槸鍚︽纭�
+ 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());
}
- @PostMapping("/save")
- public Result save(@RequestBody User user) {
- userservice.Save(user);
+ /**
+ * 鏇存柊瀵嗙爜
+ */
+ 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 = "閲嶇疆瀵嗙爜")
+ @PostMapping("/resetPass")
+ @RequiresRoles({"admin"})
+
+ public Result resetPassword(@RequestBody UserVo userVO) {
+ User user = userService.getById(userVO.getId());
+ if (user == null) {
+ return Result.fail("鐢ㄦ埛涓嶅瓨鍦�");
+ }
+
+ // 鐢熸垚榛樿瀵嗙爜
+ String newPassword = "123456";
+
+ // 鏇存柊瀵嗙爜
+ updatePassword(user, newPassword);
+
+ return Result.success("瀵嗙爜閲嶇疆鎴愬姛锛屾柊瀵嗙爜涓猴細" + newPassword);
+ }
+
+ /**
+ * 鏇存柊瀵嗙爜
+ */
+
+
+
+ @ApiOperation(value = "鍒嗛〉鏌ヨ鐢ㄦ埛")
+ @GetMapping("/selectPage")
+// @RequiresRoles({"admin"})
+ @RequiresPermissions({"user:select"})
+ public Result selectPage(UserVo userVO) {
+ return Result.success(userService.selectPage(userVO));
+ }
+
+ @ApiOperation(value = "娣诲姞鎴栦慨鏀圭敤鎴�")
+ @PostMapping("/saveOrUpdate")
+ @RequiresRoles({"admin"})
+ @RequiresPermissions({"user:update", "user:add"})
+ public Result saveOrUpdate(@RequestBody User user) {
+ if ("admin".equals(user.getUsername())) {
+ return Result.fail("绠$悊鍛樹笉鍙互琚鐢�");
+ }
+ Integer count = userService.lambdaQuery()
+ .eq(User::getUsername, user.getUsername())
+ .ne(user.getId() != null, User::getId, user.getId())
+ .count();
+ 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();
}
- @PutMapping("/update")
- public Result update(@RequestBody User user) {
- userservice.Save(user);
+ @ApiOperation(value = "閫氳繃id鍒犻櫎鐢ㄦ埛")
+ @PostMapping("/removeById")
+ @RequiresRoles({"admin"})
+ @RequiresPermissions({"user:delete"})
+ public Result removeById(@RequestBody UserVo userVO) {
+ userService.removeById(userVO.getId());
return Result.success();
}
- @DeleteMapping("/del")
- public Result delete(@RequestParam Integer id) {
- userMapper.delete(id);
- return Result.success();
+ @ApiOperation(value = "閫氳繃id鏌ヨ鐢ㄦ埛")
+ @GetMapping("/getById")
+ @RequiresPermissions({"user:select"})
+ public Result selectById(UserVo userVO) {
+ return Result.success(userService.getById(userVO.getId()));
}
-}
+
+
+ @ApiOperation(value = "娉ㄩ攢鐧诲綍锛屽墠鎻愭槸鍦ㄧ櫥褰曠姸鎬�")
+ @PostMapping("/loginOut")
+ public Result loginOut() {
+ User currentUser = SecurityUtil.getCurrentUser();
+ if (currentUser == null) {
+ return Result.fail("鎮ㄦ殏鏈櫥褰�");
+ }
+ SecurityUtils.getSubject().logout();
+ return Result.success("娉ㄩ攢鎴愬姛");
+ }
+
+ private class UpdatePasswordRequest {
+ }
+}
\ No newline at end of file
--
Gitblit v1.8.0