用户信息改成json字符串存储

This commit is contained in:
2025-07-29 23:18:46 +08:00
parent 0edbf400ef
commit 6c0f4b58b2

View File

@ -1,5 +1,6 @@
package com.sczx.user.util; package com.sczx.user.util;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.sczx.user.dto.SimpleUserInfoDTO; import com.sczx.user.dto.SimpleUserInfoDTO;
import io.jsonwebtoken.JwtException; import io.jsonwebtoken.JwtException;
@ -42,7 +43,8 @@ public class JwtUtil {
// 支持自定义 claims 的 generateToken 方法 // 支持自定义 claims 的 generateToken 方法
public String generateToken(SimpleUserInfoDTO simpleUserInfoDTO, String subject){ public String generateToken(SimpleUserInfoDTO simpleUserInfoDTO, String subject){
Map<String, Object> claims = Maps.newHashMap(); Map<String, Object> claims = Maps.newHashMap();
claims.put("userInfo", simpleUserInfoDTO); String userInfoJson = JSONObject.toJSONString(simpleUserInfoDTO);
claims.put("userInfo", userInfoJson);
return Jwts.builder() return Jwts.builder()
.setClaims(claims) // 设置自定义 claims .setClaims(claims) // 设置自定义 claims
.setSubject(subject) .setSubject(subject)
@ -116,8 +118,9 @@ public class JwtUtil {
String authHeader = getAuthorizationHeader(); String authHeader = getAuthorizationHeader();
if (authHeader != null && authHeader.startsWith("Bearer ")) { if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7); String token = authHeader.substring(7);
String userInfoJson = getClaim(token, "userInfo", String.class);
// 进行token验证等操作 // 进行token验证等操作
return getClaim(token, "userInfo", SimpleUserInfoDTO.class); return JSONObject.parseObject(userInfoJson, SimpleUserInfoDTO.class);
}else { }else {
throw new RuntimeException("token不存在"); throw new RuntimeException("token不存在");
} }