From 42a2772d1aa7493bcc4fac3e24ee8eda4eebc23d Mon Sep 17 00:00:00 2001
From: xubinbin <1323875150@qq.com>
Date: 星期二, 12 十二月 2023 17:09:04 +0800
Subject: [PATCH] bugfix:请求头带token, SecurityUtils 获取用户id 一直为0 #1195

---
 src/main/java/com/genersoft/iot/vmp/conf/security/JwtAuthenticationFilter.java |    1 +
 src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java                |    1 +
 src/main/java/com/genersoft/iot/vmp/conf/security/dto/JwtUser.java             |    9 +++++++++
 3 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtAuthenticationFilter.java b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtAuthenticationFilter.java
index f35b5bd..f45f89a 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtAuthenticationFilter.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtAuthenticationFilter.java
@@ -78,6 +78,7 @@
 
         // 鏋勫缓UsernamePasswordAuthenticationToken,杩欓噷瀵嗙爜涓簄ull锛屾槸鍥犱负鎻愪緵浜嗘纭殑JWT,瀹炵幇鑷姩鐧诲綍
         User user = new User();
+        user.setId(jwtUser.getUserId());
         user.setUsername(jwtUser.getUserName());
         user.setPassword(jwtUser.getPassword());
         Role role = new Role();
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java
index 65e9de3..3df7593 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java
@@ -144,6 +144,7 @@
             jwtUser.setUserName(username);
             jwtUser.setPassword(user.getPassword());
             jwtUser.setRoleId(user.getRole().getId());
+            jwtUser.setUserId(user.getId());
 
             return jwtUser;
         } catch (InvalidJwtException e) {
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/dto/JwtUser.java b/src/main/java/com/genersoft/iot/vmp/conf/security/dto/JwtUser.java
index 8921a30..df29c33 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/security/dto/JwtUser.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/security/dto/JwtUser.java
@@ -21,6 +21,7 @@
         EXCEPTION
     }
 
+    private int userId;
     private String userName;
 
     private String password;
@@ -29,6 +30,14 @@
 
     private TokenStatus status;
 
+    public int getUserId() {
+        return userId;
+    }
+
+    public void setUserId(int userId) {
+        this.userId = userId;
+    }
+
     public String getUserName() {
         return userName;
     }

--
Gitblit v1.8.0