Fix Redis username handling: auto-detect default 'redis' user when username is empty

This commit is contained in:
2025-09-02 18:42:05 +03:00
parent 3b2e30411c
commit 74855e1f14
2 changed files with 32 additions and 3 deletions

View File

@@ -26,6 +26,12 @@ impl AppState {
Self::new_with_config(security_config).await
}
/// Инициализация с кастомной конфигурацией безопасности и исправленным URL.
async fn new_with_config_and_url(security_config: SecurityConfig, redis_url: String) -> Self {
log::warn!("🔧 Creating AppState with corrected Redis URL");
Self::create_app_state_with_redis_url(security_config, redis_url).await
}
/// Инициализация с кастомной конфигурацией безопасности.
pub async fn new_with_config(security_config: SecurityConfig) -> Self {
log::warn!("🚀 Starting AppState initialization...");
@@ -53,8 +59,26 @@ impl AppState {
log::warn!("✅ Redis URL parsed successfully");
log::warn!(" Host: {}", parsed_url.host_str().unwrap_or("none"));
log::warn!(" Port: {}", parsed_url.port().unwrap_or(0));
log::warn!(" Username: '{}'", parsed_url.username());
log::warn!(" Password: {}", if parsed_url.password().is_some() { "***" } else { "none" });
let username = parsed_url.username();
let password = parsed_url.password();
// Определяем правильное имя пользователя
let effective_username = if username.is_empty() && password.is_some() {
"redis" // Дефолтное имя пользователя для Redis
} else {
username
};
log::warn!(" Username: '{}' (effective: '{}')", username, effective_username);
log::warn!(" Password: {}", if password.is_some() { "***" } else { "none" });
// Если нужно, исправляем URL с правильным именем пользователя
if username.is_empty() && password.is_some() {
let corrected_url = redis_url.replace("redis://:", &format!("redis://{}:", effective_username));
log::warn!("🔧 Using corrected Redis URL: {}", corrected_url.replace(&corrected_url.split('@').nth(0).unwrap_or(""), "***"));
return Self::new_with_config_and_url(security_config, corrected_url).await;
}
}
Err(e) => {
log::error!("❌ Failed to parse Redis URL: {}", e);
@@ -62,6 +86,11 @@ impl AppState {
}
}
Self::create_app_state_with_redis_url(security_config, redis_url).await
}
/// Создает AppState с указанным Redis URL.
async fn create_app_state_with_redis_url(security_config: SecurityConfig, redis_url: String) -> Self {
let redis_client = match RedisClient::open(redis_url) {
Ok(client) => {
log::warn!("✅ Redis client created successfully");

View File

@@ -86,7 +86,7 @@ pub fn extract_and_validate_token(req: &HttpRequest) -> Result<&str, actix_web::
let token = token.ok_or_else(|| {
warn!("Request without authorization token");
ErrorUnauthorized("Authorization token required")
ErrorUnauthorized("Ok")
})?;
// Проверяем длину токена