chore: 修改 XUserIdHandshakeInterceptor 以支持 Bearer 前缀
All checks were successful
test/timeline-server/pipeline/head This commit looks good
All checks were successful
test/timeline-server/pipeline/head This commit looks good
This commit is contained in:
@@ -20,8 +20,6 @@ import java.util.Map;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class XUserIdHandshakeInterceptor implements HandshakeInterceptor {
|
public class XUserIdHandshakeInterceptor implements HandshakeInterceptor {
|
||||||
|
|
||||||
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(XUserIdHandshakeInterceptor.class);
|
|
||||||
|
|
||||||
// 从配置中获取JWT密钥
|
// 从配置中获取JWT密钥
|
||||||
@Value("${jwt.secret:timelineSecretKey}")
|
@Value("${jwt.secret:timelineSecretKey}")
|
||||||
private String jwtSecret = "timelineSecretKey";
|
private String jwtSecret = "timelineSecretKey";
|
||||||
@@ -75,6 +73,12 @@ public class XUserIdHandshakeInterceptor implements HandshakeInterceptor {
|
|||||||
try {
|
try {
|
||||||
// URL 解码
|
// URL 解码
|
||||||
token = java.net.URLDecoder.decode(token, java.nio.charset.StandardCharsets.UTF_8);
|
token = java.net.URLDecoder.decode(token, java.nio.charset.StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
// 移除 Bearer 前缀(如果存在)
|
||||||
|
if (token.startsWith("Bearer ")) {
|
||||||
|
token = token.substring(7);
|
||||||
|
}
|
||||||
|
|
||||||
Claims claims = JwtUtils.parseToken(token, jwtSecret);
|
Claims claims = JwtUtils.parseToken(token, jwtSecret);
|
||||||
if (claims != null) {
|
if (claims != null) {
|
||||||
userId = claims.get("userId", String.class);
|
userId = claims.get("userId", String.class);
|
||||||
@@ -123,7 +127,6 @@ public class XUserIdHandshakeInterceptor implements HandshakeInterceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class UserPrincipal implements Principal {
|
public static class UserPrincipal implements Principal {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user