Skip to content

Commit c1c8325

Browse files
committed
refactor: 生成 Token 时添加 username
1 parent 4da4dc1 commit c1c8325

3 files changed

Lines changed: 47 additions & 9 deletions

File tree

simple-application/src/test/java/top/cadecode/common/util/TokenUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class TokenUtilTest {
2424

2525
@Test
2626
public void generateToken() {
27-
String token = tokenUtil.generateToken(1, Collections.singletonList("user"));
27+
String token = tokenUtil.generateToken(1, "user", Collections.singletonList("user"));
2828
log.info("token: {}", token);
2929
JWTClaimsSet set = tokenUtil.verifyToken(token);
3030
log.info("set: {}", set);

simple-common/src/main/java/top/cadecode/common/util/TokenUtil.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import top.cadecode.common.core.exception.CommonException;
1515
import top.cadecode.common.enums.FrameErrorEnum;
1616

17+
import java.text.ParseException;
1718
import java.util.Date;
1819
import java.util.List;
1920

@@ -33,19 +34,24 @@ public class TokenUtil {
3334
private Long refreshExpiration;
3435
private String secret;
3536

37+
private static final String ID_KEY = "id";
38+
private static final String NAME_KEY = "name";
39+
private static final String ROLES_KEY = "roles";
40+
3641
/**
3742
* 生成 token
3843
*
3944
* @param id 用户 ID
4045
* @param roles 角色
4146
* @return token 字符串
4247
*/
43-
public String generateToken(long id, List<String> roles) {
48+
public String generateToken(long id, String name, List<String> roles) {
4449
long expiredTime = System.currentTimeMillis() + expiration * 1000;
4550
JWSHeader jwsHeader = new JWSHeader(JWSAlgorithm.HS256);
4651
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet.Builder()
47-
.claim("id", id).
48-
claim("roles", roles)
52+
.claim(ID_KEY, id)
53+
.claim(NAME_KEY, name)
54+
.claim(ROLES_KEY, roles)
4955
.expirationTime(new Date(expiredTime)).build();
5056
SignedJWT signedJWT = new SignedJWT(jwsHeader, jwtClaimsSet);
5157
try {
@@ -71,10 +77,41 @@ public JWTClaimsSet verifyToken(String token) {
7177
return signedJWT.getJWTClaimsSet();
7278
}
7379
return null;
74-
} catch (CommonException e) {
75-
throw e;
7680
} catch (Exception e) {
7781
throw CommonException.of(FrameErrorEnum.JWT_VERIFY_ERROR).suppressed(e);
7882
}
7983
}
84+
85+
/**
86+
* 从 claimsSet 获取 id
87+
*
88+
* @param claimsSet claims
89+
* @return id
90+
* @throws ParseException 转换异常
91+
*/
92+
public long getIdFromClaims(JWTClaimsSet claimsSet) throws ParseException {
93+
return claimsSet.getLongClaim(ID_KEY);
94+
}
95+
96+
/**
97+
* 从 claimsSet 获取 name
98+
*
99+
* @param claimsSet claims
100+
* @return name
101+
* @throws ParseException 转换异常
102+
*/
103+
public String getNameFromClaims(JWTClaimsSet claimsSet) throws ParseException {
104+
return claimsSet.getStringClaim(NAME_KEY);
105+
}
106+
107+
/**
108+
* 从 claimsSet 获取 roles
109+
*
110+
* @param claimsSet claims
111+
* @return roles
112+
* @throws ParseException 转换异常
113+
*/
114+
public List<String> getRolesFromClaims(JWTClaimsSet claimsSet) throws ParseException {
115+
return claimsSet.getStringListClaim(ROLES_KEY);
116+
}
80117
}

simple-framework/src/main/java/top/cadecode/framework/security/LoginSuccessHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@ public class LoginSuccessHandler implements AuthenticationSuccessHandler {
3636

3737
@Override
3838
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
39-
Authentication authentication) {
39+
Authentication authentication) {
4040
// 从认证信息中获取用户对象
4141
SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
4242
// 创建用户 VO,并设置属性
4343
SecurityUserVo securityUserVo = new SecurityUserVo();
4444
BeanUtils.copyProperties(securityUser, securityUserVo);
45-
// 获取用户名和角色
45+
// 获取 ID,用户名和角色
4646
Long id = securityUser.getId();
47+
String username = securityUser.getUsername();
4748
List<String> roles = securityUser.getAuthorities().stream()
4849
.map(GrantedAuthority::getAuthority)
4950
.collect(Collectors.toList());
5051
// 为用户 VO 设置角色
5152
securityUserVo.setRoles(roles);
5253
// 为用户 VO 设置 JWT Token
53-
securityUserVo.setToken(tokenUtil.generateToken(id, roles));
54+
securityUserVo.setToken(tokenUtil.generateToken(id, username, roles));
5455
// 为用户 VO 设置刷新 Token
5556
String refreshToken = UUID.randomUUID().toString();
5657
securityUserVo.setRefreshToken(refreshToken);

0 commit comments

Comments
 (0)