登出bug

This commit is contained in:
litianxiang
2026-05-26 14:03:30 +08:00
parent 0eb6426a99
commit 74efef7c24

View File

@@ -41,8 +41,11 @@ public class LogoutBlacklistWebFilter implements WebFilter {
return chain.filter(exchange); return chain.filter(exchange);
} }
// 从请求头读取 X-User-Id(内部调用,不需要鉴权 // 优先从请求头读取 X-User-Id,其次从 query param 读取 userId兼容不同客户端调用方式
String userId = exchange.getRequest().getHeaders().getFirst(AuthConstants.USER_ID_HEADER); String userId = exchange.getRequest().getHeaders().getFirst(AuthConstants.USER_ID_HEADER);
if (userId == null || userId.isBlank()) {
userId = exchange.getRequest().getQueryParams().getFirst("userId");
}
if (userId == null || userId.isBlank()) { if (userId == null || userId.isBlank()) {
return writeResponse(exchange, HttpStatus.BAD_REQUEST, "{\"code\":400,\"message\":\"userId required\"}"); return writeResponse(exchange, HttpStatus.BAD_REQUEST, "{\"code\":400,\"message\":\"userId required\"}");
} }
@@ -50,12 +53,13 @@ public class LogoutBlacklistWebFilter implements WebFilter {
String blacklistKey = AuthConstants.BLACKLIST_PREFIX + userId; String blacklistKey = AuthConstants.BLACKLIST_PREFIX + userId;
// 黑名单 TTL 设为 7 天(与 JWT 有效期保持一致) // 黑名单 TTL 设为 7 天(与 JWT 有效期保持一致)
String finalUserId = userId;
return redisTemplate.opsForValue() return redisTemplate.opsForValue()
.set(blacklistKey, "1") .set(blacklistKey, "1")
.then(redisTemplate.expire(blacklistKey, Duration.ofDays(7))) .then(redisTemplate.expire(blacklistKey, Duration.ofDays(7)))
.then(writeResponse(exchange, HttpStatus.OK, "{\"code\":200,\"message\":\"ok\"}")) .then(writeResponse(exchange, HttpStatus.OK, "{\"code\":200,\"message\":\"ok\"}"))
.onErrorResume(e -> { .onErrorResume(e -> {
log.error("Failed to add token to blacklist, userId={}", userId, e); log.error("Failed to add token to blacklist, userId={}", finalUserId, e);
return writeResponse(exchange, HttpStatus.INTERNAL_SERVER_ERROR, return writeResponse(exchange, HttpStatus.INTERNAL_SERVER_ERROR,
"{\"code\":500,\"message\":\"internal error\"}"); "{\"code\":500,\"message\":\"internal error\"}");
}); });