Priyansusahoo / Priyansusahoo/security_core

feat: Implement Redis-based Refresh Token, Multi-Device Session Management & Token Blacklist

Open
#1 0 comments 0 reactions 1 assignee View on GitHub

@Priyansusahoo is already working on this.

Since Aug 16, 2026.

enhancement
Dominant language
Java
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

πŸ“‹ Overview

This issue tracks the implementation of a production-grade stateless JWT security upgrade.
The current implementation issues a 24-hour access token with no refresh mechanism, no logout
capability, and no multi-device session management.

🚨 Current Problems

  • Access token lifetime is 24 hours β€” a stolen token is valid for an entire day
  • No logout endpoint β€” once issued, a token cannot be revoked
  • Single-device only β€” a second login overwrites the first device's session
  • loadUserByUsername() hits PostgreSQL on every API request β€” no caching
  • No CORS configuration β€” frontend applications cannot call this API from a browser

βœ… What We Will Implement

1. Redis Infrastructure
  • docker-compose.yml with Redis (AOF persistence + volume mount)
  • spring-boot-starter-data-redis dependency
  • Redis connection configuration in application.yml
2. Refresh Token (Multi-Device)
  • Reduce Access Token lifetime: 24 hours β†’ 15 minutes
  • Issue a Refresh Token (7 days) on login/register
  • Store Refresh Token in Redis: refresh:<userId>:<sessionId>
  • POST /v1/api/auth/refresh endpoint β€” validates + rotates refresh token
  • sessionId embedded as a claim inside the Access Token
3. Token Blacklist (Logout)
  • POST /v1/api/auth/logout β€” blacklists current access token in Redis
  • Blacklisted token TTL = remaining lifetime of the access token
  • JwtAuthenticationFilter updated to check blacklist on every request
4. Logout From All Devices
  • POST /v1/api/auth/logout-all endpoint
  • Stores invalidated_before:<userId> timestamp in Redis
  • Deletes ALL refresh:<userId>:* keys from Redis
  • Filter rejects any token issued before the invalidated_before timestamp
5. Password/Email Update Handling
  • On credential update β†’ trigger "logout from all devices" automatically
6. CORS Configuration
  • CorsConfigurationSource bean in SecurityConfiguration
  • Profile-based allowed origins (local vs prod)
7. Flyway Migration
  • V2__add_session_tracking.sql β€” (if any DB changes needed)

πŸ—‚οΈ Files To Be Created

New:
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ src/main/java/.../token/RefreshTokenService.java
β”œβ”€β”€ src/main/java/.../token/TokenBlacklistService.java
β”œβ”€β”€ src/main/java/.../auth/dto/RefreshTokenRequest.java
└── src/main/java/.../auth/dto/LogoutRequest.java

Modified:
β”œβ”€β”€ pom.xml                        (add Redis dependency)
β”œβ”€β”€ application.yml                (Redis config)
β”œβ”€β”€ application-local.yml          (Redis local config)
β”œβ”€β”€ AuthController.java            (new endpoints)
β”œβ”€β”€ AuthService.java               (refresh + blacklist logic)
β”œβ”€β”€ AuthResponse.java              (add refreshToken + sessionId fields)
β”œβ”€β”€ JwtService.java                (reduce expiry, add sessionId claim)
β”œβ”€β”€ JwtAuthenticationFilter.java   (blacklist + invalidatedBefore checks)
└── SecurityConfiguration.java    (CORS config)

πŸ”’ Security Improvements Summary

Threat Before After
Token theft 24hr window 15min window
Logout Not possible Immediate blacklist
Multi-device Broken Full session management
All-device logout Not possible invalidated_before timestamp
DB load per request 1 query/request Redis cache (sub-millisecond)
Browser CORS Blocked Properly configured

🏷️ Labels

enhancement security redis jwt

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up β€” it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.