浏览代码

用户密码修改

haogh 1 年之前
父节点
当前提交
e9a29fce25

+ 3 - 0
src/main/java/com/qmth/exam/reserve/bean/login/LoginUser.java

@@ -44,6 +44,9 @@ public class LoginUser implements AccessEntity, IModel {
     @ApiModelProperty(value = "鉴权信息")
     private String token;
 
+    @ApiModelProperty(value = "第一次登录标志")
+    private Boolean firstLogin;
+
     @Override
     @JsonIgnore
     @ApiModelProperty(hidden = true)

+ 10 - 4
src/main/java/com/qmth/exam/reserve/controller/admin/UserLoginController.java

@@ -8,13 +8,11 @@ import com.qmth.exam.reserve.controller.BaseController;
 import com.qmth.exam.reserve.service.AuthService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-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 org.springframework.web.bind.annotation.*;
 
 @RestController
 @Api(tags = "用户登录相关接口")
@@ -40,4 +38,12 @@ public class UserLoginController extends BaseController {
         authService.logout(curLoginUser());
     }
 
+    @Aac(strict = false, auth = true)
+    @ApiOperation(value = "用户修改密码")
+    @PostMapping(value = "/password/modify")
+    public void modify(@ApiParam("用户密码") @RequestParam String password) {
+        LoginUser loginUser = curLoginUser();
+        authService.modifyPassword(loginUser.getId(), password);
+    }
+
 }

+ 5 - 0
src/main/java/com/qmth/exam/reserve/entity/UserEntity.java

@@ -53,4 +53,9 @@ public class UserEntity extends BaseEntity {
      */
     private Boolean enable;
 
+    /**
+     * 第一次登录标志
+     */
+    private Boolean firstLogin;
+
 }

+ 1 - 0
src/main/java/com/qmth/exam/reserve/service/AuthService.java

@@ -14,4 +14,5 @@ public interface AuthService {
 
     void logout(LoginUser user);
 
+    void modifyPassword(Long id, String password);
 }

+ 27 - 0
src/main/java/com/qmth/exam/reserve/service/impl/AuthServiceImpl.java

@@ -1,5 +1,6 @@
 package com.qmth.exam.reserve.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.security.annotation.AuthorizationComponent;
 import com.qmth.boot.core.security.service.AuthorizationService;
@@ -27,6 +28,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 @Service
 @AuthorizationComponent
@@ -90,6 +92,7 @@ public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthSer
 
         loginUser.setSessionId(LoginSessionManager.USER_LOGIN + user.getId());
         loginUser.setToken(FastUUID.get());
+        loginUser.setFirstLogin(user.getFirstLogin() == null ? Boolean.TRUE : user.getFirstLogin());
         loginSessionManager.addLoginSession(loginUser);
 
         log.info("[USER_LOGIN] success! account:{} {} {}", loginUser.getAccount(), loginUser.getName(), loginUser.getRole());
@@ -188,6 +191,30 @@ public class AuthServiceImpl implements AuthorizationService<LoginUser>, AuthSer
         log.warn("[LOGOUT] account:{} {}", loginUser.getAccount(), loginUser.getName());
     }
 
+    @Transactional
+    @Override
+    public void modifyPassword(Long id, String password) {
+        if (StringUtils.isBlank(password)) {
+            throw new StatusException("密码不能为空");
+        }
+        String regex = "^[a-zA-Z0-9]{6,12}$";
+        if (!password.matches(regex)) {
+            throw new StatusException("密码必须为:6-12位的大小写字母或者数字");
+        }
+        String encodePassword = DigestUtils.sha256Hex(password).toUpperCase();
+        UserEntity user = userService.getById(id);
+        if (user.getPassword().equals(encodePassword)) {
+            throw new StatusException("修改的密码不能和原密码相同");
+        }
+        userService.update(new UpdateWrapper<UserEntity>().lambda().set(UserEntity::getPassword, encodePassword).eq(UserEntity::getId, id));
+        //更新修改密码标志
+        if (user.getFirstLogin() == null || user.getFirstLogin()) {
+            userService.update(
+                    new UpdateWrapper<UserEntity>().lambda().set(UserEntity::getFirstLogin, Boolean.FALSE).eq(UserEntity::getId, id));
+        }
+    }
+
+
     @Override
     public LoginUser findByIdentity(String identity, SignatureType type, String path) {
         return loginSessionManager.getLoginSession(identity);