wuyouming666
2023-08-29 fd19536cbf9e5acec9bf7270f3f46037e822827d
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("旧密码不正确");
        }
        // 更新密码
        updatePassword(currentUser, newPassword);
        return Result.success("密码修改成功");
    }
    /**
     * 验证密码是否正确
     */
    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默认的加密工具类为注册用户的密码进行加密
        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 {
    }
}