Kaynağa Gözat

增加获取用户名字

david 2 yıl önce
ebeveyn
işleme
1da0a38e3d

+ 9 - 0
src/main/java/com/roma/romaapi/controller/ApiController.java

@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -47,4 +48,12 @@ public class ApiController {
 
         return CustomResponse.formatResponse(loginResult);
     }
+
+    // 获取用户信息
+    @RequestMapping("/api/userInfo")
+    public Map userInfo(@RequestHeader("Authorization") String authorization){
+        Map<String, Object> loginResult = apiService.userInfo(authorization);
+
+        return CustomResponse.formatResponse(loginResult);
+    }
 }

+ 16 - 0
src/main/java/com/roma/romaapi/dao/ApiDao.java

@@ -29,4 +29,20 @@ public class ApiDao {
             return resultMap;
         }
     }
+
+    // 通过用户id,获取用户信息
+    public Map<String, Object> userInfoByIdQueryForMap(String userID) {
+        try {
+            Map<String, Object> paramMap = new HashMap<>();
+            paramMap.put("admin_id", userID);
+            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
+                    "SELECT * FROM `admin_user` where id = :admin_id AND is_enable=1", paramMap);
+            return  resMap2;
+        } catch (EmptyResultDataAccessException e) {
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap.put("sysErrorCode", "500");
+
+            return resultMap;
+        }
+    }
 }

+ 18 - 0
src/main/java/com/roma/romaapi/service/ApiService.java

@@ -1,6 +1,7 @@
 package com.roma.romaapi.service;
 
 import com.roma.romaapi.dao.ApiDao;
+import com.roma.romaapi.utils.CommonUtil;
 import com.roma.romaapi.utils.JWTUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -23,6 +24,8 @@ public class ApiService {
     SecurityUtils securityUtils;
     @Autowired
     JWTUtil jwtUtil;
+    @Autowired
+    CommonUtil commonUtil;
     // 登录方法
     public Map<String, Object> login(Map<String, String[]> maps) {
         // 请求参数,获取验证码,验证码token,用户名,密码
@@ -72,4 +75,19 @@ public class ApiService {
 
         return map;
     }
+
+    // 获取头部用户信息
+    public Map<String, Object> userInfo(String headerToken) {
+        String userId = commonUtil.getUserIdByHeaderAuthorization(headerToken);
+        Map<String, Object> map = new HashMap<>();
+        if (userId.length() > 0) {
+            // 获取用户信息
+            Map<String, Object> userInfo = apiDao.userInfoByIdQueryForMap(userId);
+            if (userInfo.containsKey("admin_name")) {
+                map.put("userName", userInfo.get("admin_name"));
+            }
+        }
+
+        return map;
+    }
 }

+ 26 - 0
src/main/java/com/roma/romaapi/utils/CommonUtil.java

@@ -0,0 +1,26 @@
+package com.roma.romaapi.utils;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
+
+import static com.roma.romaapi.utils.JWTUtil.SIGN;
+
+// 系统公共工具类
+@Component
+public class CommonUtil {
+
+    @Autowired
+    StringRedisTemplate stringRedisTemplate;
+
+    // 通过header中token信息,获取用户Id
+    public String getUserIdByHeaderAuthorization(String headerAuthorization){
+        String token = headerAuthorization.substring(7); // 截取token
+        String redisTokenKey = SIGN + token;
+        String userId = stringRedisTemplate.opsForValue().get(redisTokenKey);
+        if(null == userId || userId.equals("")){
+            return "";
+        }
+        return userId;
+    }
+}

+ 1 - 1
src/main/resources/application.properties

@@ -1 +1 @@
-#服务
server.port=8081
server.servlet.session.timeout=600

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.26.212.216:3306/lowcode?serverTimezone=UTC&tinyInt1isBit=false&transformedBitIsBoolean=false
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=5000
spring.redis.lettuce.pool.max-active=3
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-idle=3
spring.redis.lettuce.pool.max-wait=-1
#spring.redis.lettuce.shutdown-timeout=100
#spring.cache.redis.cache-null-values=false


+#服务
server.port=8081
server.servlet.session.timeout=600

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.110.209:3306/lowcode?serverTimezone=UTC&tinyInt1isBit=false&transformedBitIsBoolean=false
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=5000
spring.redis.lettuce.pool.max-active=3
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-idle=3
spring.redis.lettuce.pool.max-wait=-1
#spring.redis.lettuce.shutdown-timeout=100
#spring.cache.redis.cache-null-values=false