fix: 优化逻辑

This commit is contained in:
tangwei 2026-07-30 15:47:21 +08:00
parent a3589c7ceb
commit 0f5a172f08
2 changed files with 10 additions and 10 deletions

View File

@ -39,7 +39,7 @@ public class BearerTokenService {
* @return token 信息accessToken, expiresIn
*/
public BearerTokenResult createToken(HttpServletRequest request, String clientId) {
String token = UUID.randomUUID().toString().replace("-", "");
String token = UUID.randomUUID().toString();
long expireAt = System.currentTimeMillis() + DEFAULT_EXPIRE_MILLIS;
JSONObject payload = new JSONObject();

View File

@ -70,7 +70,7 @@ public class OAuth2TokenController {
}
String clientId = resolveClientId(authHeader);
if (StrUtil.isBlank(clientId)|| !"client".equals(clientId)) {
if (StrUtil.isBlank(clientId)|| !"client:secret".equals(clientId)) {
log.warn("获取 Token 失败: 无法解析 client");
result.put("success", false);
result.put("errorCode", -1);
@ -84,7 +84,8 @@ public class OAuth2TokenController {
log.warn("获取 Token 失败: 不支持的 grant_type={}", grantType);
result.put("success", false);
result.put("errorCode", -1);
result.put("message", "仅支持 client_credentials 授权模式");
// 仅支持 client_credentials 授权模式
result.put("message", "授权模式错误");
return result;
}
@ -111,13 +112,12 @@ public class OAuth2TokenController {
try {
String base64 = authHeader.substring(6).trim();
byte[] decoded = Base64.getDecoder().decode(base64);
String credentials = new String(decoded, StandardCharsets.UTF_8);
// 格式: clientId:clientSecret
int colonIdx = credentials.indexOf(':');
if (colonIdx > 0) {
return credentials.substring(0, colonIdx);
}
return credentials;
// // 格式: client:secret
// int colonIdx = credentials.indexOf(':');
// if (colonIdx > 0) {
// return credentials.substring(0, colonIdx);
// }
return new String(decoded, StandardCharsets.UTF_8);
} catch (Exception e) {
log.warn("解析 Basic 认证信息失败: {}", e.getMessage());
return null;