From 9889f908c80cfc69bb3d240ce1705fcddfb5e9fb Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 04 七月 2023 11:08:14 +0800
Subject: [PATCH] 合并主线
---
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java | 29 +++++++++++++++++++++++------
1 files changed, 23 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 826dd51..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
@@ -24,10 +24,10 @@
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 {
@@ -43,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(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password){
- LoginUser user = null;
+ LoginUser user;
try {
user = SecurityUtils.login(username, password, authenticationManager);
} catch (AuthenticationException e) {
@@ -56,11 +58,13 @@
if (user == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒");
}else {
- String jwt = JwtUtils.createToken(username, password);
+ String jwt = JwtUtils.createToken(username, password, user.getRole().getId());
response.setHeader(JwtUtils.getHeader(), jwt);
+ user.setAccessToken(jwt);
}
return user;
}
+
@PostMapping("/changePassword")
@Operation(summary = "淇敼瀵嗙爜")
@@ -80,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);
}
@@ -202,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