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 | 48 +++++++++++++++++++++++++++++------------------- 1 files changed, 29 insertions(+), 19 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 7e40720..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,6 +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; @@ -11,24 +12,23 @@ import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import com.github.pagehelper.PageInfo; - 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.ObjectUtils; -import org.springframework.util.StringUtils; 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; @Tag(name = "鐢ㄦ埛绠$悊") -@CrossOrigin @RestController @RequestMapping("/api/user") public class UserController { @@ -43,11 +43,14 @@ private IRoleService roleService; @GetMapping("/login") - @Operation(summary = "鐧诲綍") + @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(@RequestParam String username, @RequestParam String password){ - LoginUser user = null; + 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) { @@ -55,12 +58,17 @@ } if (user == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒"); + }else { + String jwt = JwtUtils.createToken(username); + response.setHeader(JwtUtils.getHeader(), jwt); + user.setAccessToken(jwt); } return user; } + @PostMapping("/changePassword") - @Operation(summary = "淇敼瀵嗙爜") + @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) @@ -77,8 +85,8 @@ if (user == null) { throw new ControllerException(ErrorCode.ERROR100); } - int userId = SecurityUtils.getUserId(); - boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes())); + //int userId = SecurityUtils.getUserId(); + boolean result = userService.changePassword(user.getId(), DigestUtils.md5DigestAsHex(password.getBytes())); if (!result) { throw new ControllerException(ErrorCode.ERROR100); } @@ -89,7 +97,7 @@ @PostMapping("/add") - @Operation(summary = "鍋滄瑙嗛鍥炴斁") + @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) @@ -124,8 +132,8 @@ } } - @DeleteMapping("/鍒犻櫎鐢ㄦ埛") - @Operation(summary = "鍋滄瑙嗛鍥炴斁") + @DeleteMapping("/delete") + @Operation(summary = "鍒犻櫎鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) @Parameter(name = "id", description = "鐢ㄦ埛Id", required = true) public void delete(@RequestParam Integer id){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id @@ -141,7 +149,7 @@ } @GetMapping("/all") - @Operation(summary = "鏌ヨ鐢ㄦ埛") + @Operation(summary = "鏌ヨ鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) public List<User> all(){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id return userService.getAllUsers(); @@ -155,7 +163,7 @@ * @return 鍒嗛〉鐢ㄦ埛鍒楄〃 */ @GetMapping("/users") - @Operation(summary = "鍒嗛〉鏌ヨ鐢ㄦ埛") + @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) { @@ -163,7 +171,7 @@ } @RequestMapping("/changePushKey") - @Operation(summary = "淇敼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) { @@ -181,7 +189,7 @@ } @PostMapping("/changePasswordForAdmin") - @Operation(summary = "绠$悊鍛樹慨鏀规櫘閫氱敤鎴峰瘑鐮�") + @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) @@ -205,9 +213,11 @@ public LoginUser getUserInfo() { // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id LoginUser userInfo = SecurityUtils.getUserInfo(); + if (userInfo == null) { throw new ControllerException(ErrorCode.ERROR100); } - return userInfo; + User user = userService.getUser(userInfo.getUsername(), userInfo.getPassword()); + return new LoginUser(user, LocalDateTime.now()); } } -- Gitblit v1.8.0