From 403da64eae0a1357d80b6ce44391af1f11b835a3 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 24 十月 2022 09:08:47 +0800 Subject: [PATCH] 添加授权功能 --- src/main/java/com/lf/server/controller/sys/UserController.java | 76 ++++++++++++++++++++------------------ 1 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/lf/server/controller/sys/UserController.java b/src/main/java/com/lf/server/controller/sys/UserController.java index b679e79..415abd2 100644 --- a/src/main/java/com/lf/server/controller/sys/UserController.java +++ b/src/main/java/com/lf/server/controller/sys/UserController.java @@ -4,7 +4,7 @@ import com.lf.server.controller.all.BaseController; import com.lf.server.entity.all.ResponseMsg; import com.lf.server.entity.sys.UserEntity; -import com.lf.server.helper.Md5Helper; +import com.lf.server.entity.ctrl.UserUpdateEntity; import com.lf.server.helper.StringHelper; import com.lf.server.service.sys.TokenService; import com.lf.server.service.sys.UserService; @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.util.List; /** @@ -142,6 +141,26 @@ } @SysLog() + @ApiOperation(value = "鏍规嵁鐢ㄦ埛ID鏌ヨ") + @ApiImplicitParams({ + @ApiImplicitParam(name = "uid", value = "鐢ㄦ埛ID", dataType = "String", paramType = "query", example = "admin") + }) + @GetMapping(value = "/selectByUid") + public ResponseMsg<UserEntity> selectByUid(String uid) { + try { + if (StringHelper.isEmpty(uid)) { + fail("鐢ㄦ埛ID涓嶈兘涓虹┖", null); + } + + UserEntity userEntity = userService.selectByUid(uid); + + return success(userEntity); + } catch (Exception ex) { + return fail(ex.getMessage(), null); + } + } + + @SysLog() @ApiOperation(value = "鏌ヨ鎵�鏈�") @GetMapping(value = "/selectUserAll") public ResponseMsg<List<UserEntity>> selectUserAll() { @@ -162,7 +181,7 @@ @PostMapping(value = "/insertUser", produces = "application/json; charset=UTF-8") public ResponseMsg<Integer> insertUser(@RequestBody UserEntity entity, HttpServletRequest req) { try { - String str = userService.ValidateNewPwd(entity); + String str = userService.validateNewPwd(entity); if (str != null) { return fail(str, -1); } @@ -194,11 +213,10 @@ UserEntity ue = tokenService.getCurrentUser(req); for (UserEntity entity : list) { - String str = userService.ValidateNewPwd(entity); + String str = userService.validateNewPwd(entity); if (str != null) { return fail(str, -1); } - if (ue != null) { entity.setCreateUser(ue.getId()); } @@ -257,7 +275,7 @@ @PostMapping(value = "/updateUser", produces = "application/json; charset=UTF-8") public ResponseMsg<Integer> updateUser(@RequestBody UserEntity entity, HttpServletRequest req) { try { - String str = userService.ValidateOldPwd(entity); + String str = userService.validateOldPwd(entity); if (str != null) { return fail(str, -1); } @@ -276,49 +294,35 @@ } @SysLog() - @ApiOperation(value = "鏇存柊鐢ㄦ埛瀵嗙爜") + @ApiOperation(value = "鏇存柊澶氫釜鐢ㄦ埛瀵嗙爜") @ApiImplicitParams({ - @ApiImplicitParam(name = "user", value = "鐢ㄦ埛瀹炰綋绫�", dataType = "UsersEntity", paramType = "body", example = "") + @ApiImplicitParam(name = "adminPwd", value = "绠$悊鍛樺瘑鐮�", dataType = "String", paramType = "body", example = ""), + @ApiImplicitParam(name = "newPwd", value = "鏂板瘑鐮�", dataType = "String", paramType = "body", example = ""), + @ApiImplicitParam(name = "ids", value = "鐢ㄦ埛ID闆嗗悎", dataType = "List<Integer>", paramType = "body", example = "") }) - @PostMapping(value = "/updateUserPwd") - public ResponseMsg<Boolean> updateUserPwd(@RequestBody UserEntity user, HttpServletRequest req, HttpServletResponse res) { + @PostMapping(value = "/updateUsersPwd", produces = "application/json; charset=UTF-8") + public ResponseMsg<Boolean> updateUsersPwd(@RequestBody UserUpdateEntity uue, HttpServletRequest req) { try { - if (user == null) { - return fail("璇锋彁浜ょ敤鎴蜂俊鎭紒", false); - } - if (StringHelper.isEmpty(user.getPwd())) { - return fail("璇疯緭鍏ョ敤鎴峰瘑鐮侊紒", false); - } - if (StringHelper.isEmpty(user.getSalt())) { - return fail("璇疯緭鍏ョ鐞嗗憳瀵嗙爜锛�", false); - } - if (!StringHelper.checkPwdValid(user.getPwd())) { - return fail("鏂板瘑鐮佷笉绗﹀悎瑙勫垯瑕佹眰锛�", false); + if (uue == null || uue.getIds() == null || uue.getIds().isEmpty()) { + return fail("娌℃湁鎵惧埌鏁版嵁", false); } UserEntity ue = tokenService.getCurrentUser(req); - if (ue == null) { - return fail("娌℃湁鐧诲綍鎴栫櫥褰曡秴鏃讹紒", false); - } - if (!Md5Helper.validatePassword(user.getSalt(), ue.getPwd())) { - return fail("绠$悊鍛樺瘑鐮佷笉姝g‘锛�", false); + String str = userService.validateAdminPwd(ue, uue.getAdminPwd()); + if (str != null) { + return fail(str, false); } - UserEntity userEntity = userService.selectUser(user.getId()); - if (userEntity == null) { - return fail("娌℃湁鎵惧埌瑕佷慨鏀圭殑鐢ㄦ埛锛�", false); + str = userService.validateNewPwd(ue, uue.getNewPwd()); + if (str != null) { + return fail(str, false); } - // 璁剧疆鏂板瘑鐮� - String md5 = Md5Helper.reverse(Md5Helper.generate(user.getPwd())); - userEntity.setPwd(md5); - // 璁剧疆鏇存柊淇℃伅 - userEntity.setUpdateUser(ue.getId()); - Integer rows = userService.updateUsers(userEntity); + Integer rows = userService.updateUsersPwd(ue.getId(), ue.getSalt(), uue.getIds()); return success(rows > 0 ? "鏇存柊鎴愬姛" : "鏇存柊澶辫触", rows > 0); } catch (Exception ex) { return fail(ex.getMessage(), false); } } -} \ No newline at end of file +} -- Gitblit v1.9.3