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 | 50 +++++++++++++++++++++++++++++++++++++------------- 1 files changed, 37 insertions(+), 13 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 127e83b..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; @@ -13,6 +14,7 @@ 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.security.authentication.AuthenticationManager; @@ -21,10 +23,12 @@ 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 = "鐢ㄦ埛绠$悊") - @RestController @RequestMapping("/api/user") public class UserController { @@ -40,11 +44,13 @@ @GetMapping("/login") @PostMapping("/login") - @Operation(summary = "鐧诲綍") + @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) { @@ -52,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) @@ -74,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); } @@ -86,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) @@ -122,7 +133,7 @@ } @DeleteMapping("/delete") - @Operation(summary = "鍒犻櫎鐢ㄦ埛") + @Operation(summary = "鍒犻櫎鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) @Parameter(name = "id", description = "鐢ㄦ埛Id", required = true) public void delete(@RequestParam Integer id){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id @@ -138,7 +149,7 @@ } @GetMapping("/all") - @Operation(summary = "鏌ヨ鐢ㄦ埛") + @Operation(summary = "鏌ヨ鐢ㄦ埛", security = @SecurityRequirement(name = JwtUtils.HEADER)) public List<User> all(){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id return userService.getAllUsers(); @@ -152,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) { @@ -160,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) { @@ -178,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) @@ -196,4 +207,17 @@ } } } + + @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