From 9bd588957a08ae3fa574fddacc2eebc5ba79553b Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期三, 28 六月 2023 20:27:25 +0800 Subject: [PATCH] 修复获取pushKey --- src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java | 35 +++++++++++++++++++++++++++++------ 1 files changed, 29 insertions(+), 6 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 index 157cb75..090cf33 100644 --- 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; @@ -21,10 +22,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 = "鐢ㄦ埛绠$悊") -@CrossOrigin @RestController @RequestMapping("/api/user") public class UserController { @@ -40,11 +43,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,9 +57,14 @@ } if (user == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒"); + }else { + String jwt = JwtUtils.createToken(username, password, user.getRole().getId()); + response.setHeader(JwtUtils.getHeader(), jwt); + user.setAccessToken(jwt); } return user; } + @PostMapping("/changePassword") @Operation(summary = "淇敼瀵嗙爜") @@ -74,8 +84,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); } @@ -196,4 +206,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