RequestUtil工具类方法补充

This commit is contained in:
caozehui
2025-02-24 10:00:37 +08:00
parent 633f6e449c
commit b995175113

View File

@@ -2,6 +2,9 @@ package com.njcn.web.utils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.njcn.common.bean.CustomCacheUtil;
import com.njcn.common.pojo.constant.SecurityConstants;
import com.njcn.common.utils.JwtUtil;
@@ -30,6 +33,16 @@ public class RequestUtil {
return accessToken;
}
/**
* 获取当前请求的用户IP
*
* @return
*/
public static String getUserIp() {
HttpServletRequest request = HttpServletUtil.getRequest();
return request.getRemoteAddr();
}
/**
* 获取当前登录用户的ID
*
@@ -45,6 +58,20 @@ public class RequestUtil {
return userId;
}
/**
* 获取当前登录用户的用户名
* @return 若成功返回当前登录用户的用户名若失败返回null
*/
public static String getUserName(){
String userJson = getUserJson();
String username = null;
if (StrUtil.isNotBlank(userJson)) {
JSONObject obj = JSONUtil.parseObj(userJson);
username = obj.getStr("username");
}
return username;
}
/**
* 从缓冲中获取当前登录用户的JSON格式的用户信息
* @return 若成功返回当前登录用户的JSON格式的用户信息若失败返回null