How to Build Distributed Unified Login with Redis, Spring Security, and OAuth2

This guide explains how to integrate Redis with Spring Security and OAuth2 to create a distributed, stateless authentication system that supports multiple client types, details request encryption, token generation, persistence, and the complete server‑side login flow.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How to Build Distributed Unified Login with Redis, Spring Security, and OAuth2

1. Effect

The authentication token and refresh token, together with user identity information, are stored in Redis instead of using JWT, as illustrated below.

Token storage in Redis
Token storage in Redis

2. Client Types

The system supports several login clients:

WEB

PDA

PAD

Patient app

Mini‑program

This article covers the first three.

3. WEB Client

The login page requires three parameters: username, password, and hospital ID. The request example is:

POST /auth/oauth2/token?grant_type=password&scope=server HTTP/1.1
Host: codeape-gateway:9999
Authorization: Basic dGVzdDp0ZXN0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
username=admin&password=YehdBPev&hosId=1659018792143663105
WEB login page
WEB login page

4. PDA Client

PDA devices (nurse handhelds) authenticate with only username and password because the device SN uniquely identifies the hospital. The login request is:

POST /auth/oauth2/token?grant_type=password&scope=server HTTP/1.1
Host: codeape-gateway:9999
Authorization: Basic dGVzdDp0ZXN0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
username=admin&password=YehdBPev&sn=3981293B102
PDA device registration
PDA device registration

5. PAD Client

PAD devices (doctor tablets) can log in either like the WEB client (selecting a hospital) or by binding the device MAC address. The project uses the hospital‑selection method with a request similar to the WEB example.

6. Password‑Mode Login

The password‑mode endpoint is imported into Apifox; after execution it returns an access_token (a UUID, not a JWT), a refresh_token, and a user_info object containing username, authorities, IDs, department, hospital, role codes, phone, clientId, SN, and name.

Login response
Login response

7. Encryption Details

Two fields are encrypted in the request:

Authorization : the client_id:client_secret pair is Base64‑encoded (e.g., Basic web:web).

password : the password is AES‑encrypted.

8. Server Authentication Flow

The overall flow is shown below.

Authentication flow diagram
Authentication flow diagram

9. Gateway Filters

The gateway performs two pre‑processing steps for login requests:

Captcha validation (disabled for internal clients but enabled for external patient apps).

Password decryption.

Both are implemented as filters configured under codeape-auth.

Gateway filter configuration
Gateway filter configuration

10. OAuth2ClientAuthenticationFilter

This filter validates client credentials for /oauth2/token requests, extracts the client information, and stores the authenticated client in the SecurityContext. The conversion chain includes ClientSecretBasicAuthenticationConverter and ClientSecretPostAuthenticationConverter.

OAuth2ClientAuthenticationFilter flow
OAuth2ClientAuthenticationFilter flow

11. Token Generation and Persistence

After successful authentication, the system generates an access_token using a custom CustomeOAuth2AccessTokenGenerator and a refresh_token. Tokens are persisted in Redis via the custom CodeapeRedisOAuth2AuthorizationService.

Access token generation
Access token generation
Token stored in Redis
Token stored in Redis

12. Login Success Handling

When authentication succeeds, the custom CodeapeAuthenticationSuccessEventHandler writes the token and user information back to the client.

Success handler
Success handler

Summary

The article provides a complete walkthrough of implementing a distributed authentication solution using Redis, Spring Security, and OAuth2, covering client types, request encryption, token lifecycle, gateway filtering, and custom extensions for token storage and success handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisOAuth2spring-security
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.