Repository navigation
refactor(auth): migrate login attempts persistence from database to R… - #336
Conversation
…edis
Refactors login attempts and account lockout tracking to leverage Redis instead of
the PostgreSQL database. This improves performance and lightens the strain on the DB.
- Adds Flyway migration V61 to drop the `login_attempts` table.
- Removes JPA `@OneToOne` mapping from the `User` entity to `LoginAttempts`.
- Converts `LoginAttempts` to a simple POJO and deletes `LoginAttemptsRepository`.
- Rewrites `LoginAttemptsImpl` using `StringRedisTemplate` to store data:
- User attempts: Hash key `login_attempts:user:{userId}`.
- Active bans: ZSet key `login_attempts:blocked_users` (scored by epoch millisecond).
- Failed logins: ZSet key `login_attempts:failed_logins_history` with 24h automatic pruning.
- Integrates the non-deprecated `redisTemplate.expire(key, Duration)` signature.
- Updates dependent statistics, KPI dashboards, and profile views.
- Refactors user query filtering in `CustomUserRepositoryImpl` to query Redis blocked IDs and append JPQL `IN` / `NOT IN` clauses.
- Updates all corresponding unit tests, verifying with a clean build.
Signed-off-by: aniebietafia <aniebietafia87@gmail.com>
ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (41)
💤 Files with no reviewable changes (17)
📝 WalkthroughWalkthroughLogin attempt tracking is migrated from JPA/database persistence ( ChangesLoginAttempts: JPA → Redis migration
OTP removal and CLI admin provisioning
Sequence Diagram(s)sequenceDiagram
participant CLI as mvn/java CLI
participant AdminCliRunner
participant CreateAdminService
participant LoginAttemptsImpl
participant Redis
CLI->>AdminCliRunner: --create-admin --email ... --password ...
AdminCliRunner->>AdminCliRunner: validate args
AdminCliRunner->>CreateAdminService: createAdminUser(firstName, lastName, email, password, country, role)
CreateAdminService->>CreateAdminService: hash password, build User, afterCommit publish AdminCreatedEvent
CreateAdminService-->>AdminCliRunner: User
AdminCliRunner->>CLI: exit(0)
note over LoginAttemptsImpl,Redis: Login attempt tracking (runtime)
LoginAttemptsImpl->>Redis: HGETALL user:{id}
Redis-->>LoginAttemptsImpl: {attempts, isBlocked, blockedUntil}
LoginAttemptsImpl->>Redis: HSET user:{id} / ZADD blocked-users / ZADD failed-logins
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
Note 🎁 Summarized by CodeRabbit FreeYour organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login. Comment |
michael-whyte
left a comment
There was a problem hiding this comment.
Really good job. Good to go.
|
|
| .build(); | ||
| } | ||
|
|
||
| int loginAttempts = Integer.parseInt((String) entries.get("attempts")); |
…edis
Refactors login attempts and account lockout tracking to leverage Redis instead of the PostgreSQL database. This improves performance and lightens the strain on the DB.
login_attemptstable.@OneToOnemapping from theUserentity toLoginAttempts.LoginAttemptsto a simple POJO and deletesLoginAttemptsRepository.LoginAttemptsImplusingStringRedisTemplateto store data:login_attempts:user:{userId}.login_attempts:blocked_users(scored by epoch millisecond).login_attempts:failed_logins_historywith 24h automatic pruning.redisTemplate.expire(key, Duration)signature.CustomUserRepositoryImplto query Redis blocked IDs and append JPQLIN/NOT INclauses.Summary by CodeRabbit
New Features
Bug Fixes
Chores