From 764d04b497356ba6bcbb75fd42b51eca750f7223 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期三, 29 五月 2024 15:02:51 +0800 Subject: [PATCH] 调整上级观看消息的发送 --- src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java | 214 +++++++++++++++++++++++----------------------------- 1 files changed, 95 insertions(+), 119 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java old mode 100644 new mode 100755 index 442832b..86a806f --- a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java @@ -1,5 +1,7 @@ package com.genersoft.iot.vmp.vmanager.user; +import com.genersoft.iot.vmp.conf.exception.ControllerException; +import com.genersoft.iot.vmp.conf.security.JwtUtils; import com.genersoft.iot.vmp.conf.security.SecurityUtils; import com.genersoft.iot.vmp.conf.security.dto.LoginUser; import com.genersoft.iot.vmp.service.IRoleService; @@ -7,25 +9,26 @@ import com.genersoft.iot.vmp.storager.dao.dto.Role; import com.genersoft.iot.vmp.storager.dao.dto.User; import com.genersoft.iot.vmp.utils.DateUtil; +import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import com.github.pagehelper.PageInfo; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.util.DigestUtils; -import org.springframework.util.StringUtils; +import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; import javax.security.sasl.AuthenticationException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.time.LocalDateTime; import java.util.List; -@Api(tags = "鐢ㄦ埛绠$悊") -@CrossOrigin +@Tag(name = "鐢ㄦ埛绠$悊") @RestController @RequestMapping("/api/user") public class UserController { @@ -39,87 +42,76 @@ @Autowired private IRoleService roleService; - @ApiOperation("鐧诲綍") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "瀵嗙爜锛�32浣峬d5鍔犲瘑锛�", dataTypeClass = String.class), - }) @GetMapping("/login") - public WVPResult<LoginUser> login(@RequestParam String username, @RequestParam String password){ - LoginUser user = null; - WVPResult<LoginUser> result = new WVPResult<>(); + @PostMapping("/login") + @Operation(summary = "鐧诲綍", description = "鐧诲綍鎴愬姛鍚庤繑鍥濧ccessToken锛� 鍙互浠庤繑鍥炲�艰幏鍙栧埌涔熷彲浠ヤ粠鍝嶅簲澶翠腑鑾峰彇鍒帮紝" + + "鍚庣画鐨勮姹傞渶瑕佹坊鍔犺姹傚ご 'access-token'鎴栬�呮斁鍦ㄥ弬鏁伴噷") + + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "password", description = "瀵嗙爜锛�32浣峬d5鍔犲瘑锛�", required = true) + public LoginUser login(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password){ + LoginUser user; try { user = SecurityUtils.login(username, password, authenticationManager); } catch (AuthenticationException e) { - e.printStackTrace(); - result.setCode(-1); - result.setMsg("fail"); + throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage()); } - if (user != null) { - result.setCode(0); - result.setMsg("success"); - result.setData(user); + if (user == null) { + throw new ControllerException(ErrorCode.ERROR100.getCode(), "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒"); }else { - result.setCode(-1); - result.setMsg("fail"); + String jwt = JwtUtils.createToken(username); + response.setHeader(JwtUtils.getHeader(), jwt); + user.setAccessToken(jwt); } - return result; + return user; } - @ApiOperation("淇敼瀵嗙爜") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "oldpassword", required = true, value = "鏃у瘑鐮侊紙宸瞞d5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - }) + @PostMapping("/changePassword") - public String changePassword(@RequestParam String oldPassword, @RequestParam String password){ + @Operation(summary = "淇敼瀵嗙爜", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "oldpassword", description = "鏃у瘑鐮侊紙宸瞞d5鍔犲瘑鐨勫瘑鐮侊級", required = true) + @Parameter(name = "password", description = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", required = true) + public void changePassword(@RequestParam String oldPassword, @RequestParam String password){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id LoginUser userInfo = SecurityUtils.getUserInfo(); if (userInfo== null) { - return "fail"; + throw new ControllerException(ErrorCode.ERROR100); } String username = userInfo.getUsername(); LoginUser user = null; try { user = SecurityUtils.login(username, oldPassword, authenticationManager); - if (user != null) { - int userId = SecurityUtils.getUserId(); - boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes())); - if (result) { - return "success"; - } + if (user == null) { + throw new ControllerException(ErrorCode.ERROR100); + } + //int userId = SecurityUtils.getUserId(); + boolean result = userService.changePassword(user.getId(), DigestUtils.md5DigestAsHex(password.getBytes())); + if (!result) { + throw new ControllerException(ErrorCode.ERROR100); } } catch (AuthenticationException e) { - e.printStackTrace(); + throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage()); } - return "fail"; } - @ApiOperation("娣诲姞鐢ㄦ埛") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "瀵嗙爜锛堟湭md5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - @ApiImplicitParam(name = "roleId", required = true, value = "瑙掕壊ID", dataTypeClass = String.class), - }) @PostMapping("/add") - public ResponseEntity<WVPResult<Integer>> add(@RequestParam String username, + @Operation(summary = "娣诲姞鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "password", description = "瀵嗙爜锛堟湭md5鍔犲瘑鐨勫瘑鐮侊級", required = true) + @Parameter(name = "roleId", description = "瑙掕壊ID", required = true) + public void add(@RequestParam String username, @RequestParam String password, @RequestParam Integer roleId){ - WVPResult<Integer> result = new WVPResult<>(); - if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || roleId == null) { - result.setCode(-1); - result.setMsg("鍙傛暟涓嶅彲涓虹┖"); - return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) { + throw new ControllerException(ErrorCode.ERROR400.getCode(), "鍙傛暟涓嶅彲涓虹┖"); } // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); if (currenRoleId != 1) { // 鍙敤瑙掕壊id涓�1鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 - result.setCode(-1); - result.setMsg("鐢ㄦ埛鏃犳潈闄�"); - return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "鐢ㄦ埛鏃犳潈闄�"); } User user = new User(); user.setUsername(username); @@ -129,56 +121,38 @@ Role role = roleService.getRoleById(roleId); if (role == null) { - result.setCode(-1); - result.setMsg("roleId is not found"); - // 瑙掕壊涓嶅瓨鍦� - return new ResponseEntity<>(result, HttpStatus.OK); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "瑙掕壊涓嶅瓨鍦�"); } user.setRole(role); user.setCreateTime(DateUtil.getNow()); user.setUpdateTime(DateUtil.getNow()); int addResult = userService.addUser(user); - - - result.setCode(addResult > 0 ? 0 : -1); - result.setMsg(addResult > 0 ? "success" : "fail"); - result.setData(addResult); - return new ResponseEntity<>(result, HttpStatus.OK); + if (addResult <= 0) { + throw new ControllerException(ErrorCode.ERROR100); + } } - @ApiOperation("鍒犻櫎鐢ㄦ埛") - @ApiImplicitParams({ - @ApiImplicitParam(name = "id", required = true, value = "鐢ㄦ埛Id", dataTypeClass = Integer.class), - }) @DeleteMapping("/delete") - public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){ + @Operation(summary = "鍒犻櫎鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "id", description = "鐢ㄦ埛Id", required = true) + public void delete(@RequestParam Integer id){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); - WVPResult<String> result = new WVPResult<>(); if (currenRoleId != 1) { // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 - result.setCode(-1); - result.setMsg("鐢ㄦ埛鏃犳潈闄�"); - return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "鐢ㄦ埛鏃犳潈闄�"); } int deleteResult = userService.deleteUser(id); - - result.setCode(deleteResult>0? 0 : -1); - result.setMsg(deleteResult>0? "success" : "fail"); - return new ResponseEntity<>(result, HttpStatus.OK); + if (deleteResult <= 0) { + throw new ControllerException(ErrorCode.ERROR100); + } } - @ApiOperation("鏌ヨ鐢ㄦ埛") - @ApiImplicitParams({}) @GetMapping("/all") - public ResponseEntity<WVPResult<List<User>>> all(){ + @Operation(summary = "鏌ヨ鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) + public List<User> all(){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id - List<User> allUsers = userService.getAllUsers(); - WVPResult<List<User>> result = new WVPResult<>(); - result.setCode(0); - result.setMsg("success"); - result.setData(allUsers); - return new ResponseEntity<>(result, HttpStatus.OK); + return userService.getAllUsers(); } /** @@ -188,60 +162,62 @@ * @param count 姣忛〉鏌ヨ鏁伴噺 * @return 鍒嗛〉鐢ㄦ埛鍒楄〃 */ - @ApiOperation("鍒嗛〉鏌ヨ鐢ㄦ埛") - @ApiImplicitParams({ - @ApiImplicitParam(name = "page", value = "褰撳墠椤�", required = true, dataTypeClass = Integer.class), - @ApiImplicitParam(name = "count", value = "姣忛〉鏌ヨ鏁伴噺", required = true, dataTypeClass = Integer.class), - }) @GetMapping("/users") + @Operation(summary = "鍒嗛〉鏌ヨ鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "page", description = "褰撳墠椤�", required = true) + @Parameter(name = "count", description = "姣忛〉鏌ヨ鏁伴噺", required = true) public PageInfo<User> users(int page, int count) { return userService.getUsers(page, count); } - @ApiOperation("淇敼pushkey") - @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", required = true, value = "鐢ㄦ埛Id", dataTypeClass = Integer.class), - @ApiImplicitParam(name = "pushKey", required = true, value = "鏂扮殑pushKey", dataTypeClass = String.class), - }) @RequestMapping("/changePushKey") - public ResponseEntity<WVPResult<String>> changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) { + @Operation(summary = "淇敼pushkey", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "userId", description = "鐢ㄦ埛Id", required = true) + @Parameter(name = "pushKey", description = "鏂扮殑pushKey", required = true) + public void changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) { // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); WVPResult<String> result = new WVPResult<>(); if (currenRoleId != 1) { // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 - result.setCode(-1); - result.setMsg("鐢ㄦ埛鏃犳潈闄�"); - return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "鐢ㄦ埛鏃犳潈闄�"); } int resetPushKeyResult = userService.changePushKey(userId,pushKey); - - result.setCode(resetPushKeyResult > 0 ? 0 : -1); - result.setMsg(resetPushKeyResult > 0 ? "success" : "fail"); - return new ResponseEntity<>(result, HttpStatus.OK); + if (resetPushKeyResult <= 0) { + throw new ControllerException(ErrorCode.ERROR100); + } } - @ApiOperation("绠$悊鍛樹慨鏀规櫘閫氱敤鎴峰瘑鐮�") - @ApiImplicitParams({ - @ApiImplicitParam(name = "adminId", required = true, value = "绠$悊鍛榠d", dataTypeClass = String.class), - @ApiImplicitParam(name = "userId", required = true, value = "鐢ㄦ埛id", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - }) @PostMapping("/changePasswordForAdmin") - public String changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) { + @Operation(summary = "绠$悊鍛樹慨鏀规櫘閫氱敤鎴峰瘑鐮�", security = @SecurityRequirement(name = JwtUtils.HEADER)) + @Parameter(name = "adminId", description = "绠$悊鍛榠d", required = true) + @Parameter(name = "userId", description = "鐢ㄦ埛id", required = true) + @Parameter(name = "password", description = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", required = true) + public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) { // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id LoginUser userInfo = SecurityUtils.getUserInfo(); if (userInfo == null) { - return "fail"; + throw new ControllerException(ErrorCode.ERROR100); } Role role = userInfo.getRole(); if (role != null && role.getId() == 1) { boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes())); - if (result) { - return "success"; + if (!result) { + throw new ControllerException(ErrorCode.ERROR100); } } + } - return "fail"; + @PostMapping("/userInfo") + @Operation(summary = "绠$悊鍛樹慨鏀规櫘閫氱敤鎴峰瘑鐮�") + public LoginUser getUserInfo() { + // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id + LoginUser userInfo = SecurityUtils.getUserInfo(); + + if (userInfo == null) { + throw new ControllerException(ErrorCode.ERROR100); + } + User user = userService.getUser(userInfo.getUsername(), userInfo.getPassword()); + return new LoginUser(user, LocalDateTime.now()); } } -- Gitblit v1.8.0