基于REST的应用是否可将加密账号密码缓存1小时?
Short answer: Yes, it’s feasible—but you need to nail several security and reliability details to avoid introducing risks while skipping database persistence.
Here’s what you should focus on to make this work safely:
Validate upstream encryption strength first
Make sure the username/password sent by the upstream app uses a strong, standards-compliant algorithm (like AES-256-GCM) with proper key management. Even though you’re only caching the encrypted blob, weak encryption could still lead to credential exposure if the cache is compromised. Never store encryption keys alongside cached data—use a dedicated secure key store (like an internal KMS or local encrypted keystore) instead of hardcoding keys.Lock down your cache environment
Your cache (e.g., Redis, Memcached) needs end-to-end security:- Enable SSL/TLS for all connections to/from the cache to prevent interception.
- Enforce strict authentication (passwords, granular ACLs) so only your application can access the cache.
- Deploy the cache in a private, isolated network (no public internet access) to shrink the attack surface.
Enforce precise TTL and cleanup rules
Set an explicit TTL of exactly 1 hour for each cached credential entry—no longer. Most caches let you configure this per entry. Don’t rely solely on generic eviction policies (like LRU), as they might evict entries before the 1-hour window ends and break your workflow. Also, add a manual cleanup trigger: delete the cached entry as soon as you send the downstream response back to the upstream app, instead of waiting for the TTL to expire.Use a secure request correlation method
Don’t tie cached credentials to raw request details (like IP addresses or user agents). Instead, generate a unique, random request ID when you first receive the upstream request, store this ID mapped to the encrypted credentials in the cache, and return the ID to the upstream app. Have the upstream use this ID to poll for the downstream result. This minimizes what you’re caching and avoids mixing up credentials between requests.Plan for failure scenarios
What if the cache goes down mid-process? Or the downstream job fails unexpectedly? Build a fallback: let the upstream app re-submit the encrypted credentials if the request ID lookup fails, and ensure your downstream process is idempotent (so re-submitting doesn’t trigger duplicate work).
If you cover all these bases, caching the encrypted credentials for 1 hour is a solid approach to avoid persisting sensitive data to a database. It balances security, reliability, and simplicity for your long-running downstream workflow.
内容的提问来源于stack exchange,提问作者Sandy

