Backend Development 13 min read

Implementing Token Storage and Validation in a Distributed Microservices Architecture with Spring Cloud

This article demonstrates how to design and implement token generation, multi‑endpoint storage in Redis, custom login annotations, AOP verification, and corresponding Vue.js client handling for secure authentication across PC and mobile platforms within a Spring Cloud micro‑service system.

Architect
Architect
Architect
Implementing Token Storage and Validation in a Distributed Microservices Architecture with Spring Cloud

The article introduces a token‑based authentication solution for a distributed micro‑service environment, explaining why a gateway‑centric approach is insufficient and how token handling must support both PC and mobile clients.

Token storage entity

A @Data @AllArgsConstructor @NoArgsConstructor public class LoginToken { private String PcLoginToken; private String MobileLoginToken; private String LoginIP; } object is stored in Redis to keep separate tokens for each client type.

Login service implementation

The @Service public class loginServiceImpl implements LoginService { @Autowired UserService userService; @Autowired RedisUtils redisUtils; ... public R Login(LoginEntity entity) { /* validate request frequency, retrieve user, generate JWT, store token in Redis with a 7‑day TTL, and return the token */ } } method creates a JWT, records the client IP, and saves the token under a Redis key that includes the user ID and client type.

Enum and exception definitions

The public enum BizCodeEnum { UNKNOW_EXCEPTION(10000,"系统未知异常"), ... SUCCESSFUL(200,"successful"); private int code; private String msg; ... } enum standardises response codes, while custom exceptions such as public class BadLoginParamsException extends Exception {} , public class BadLoginTokenException extends Exception {} , and public class NotLoginException extends Exception {} represent specific authentication errors.

Client‑side token handling

Vue.js components store the token in a custom Storage.prototype.setExpire = (key, value, expire) => { let obj = { data: value, time: Date.now(), expire: expire }; localStorage.setItem(key, JSON.stringify(obj)); } method, and retrieve it with Storage.prototype.getExpire = key => { ... } , ensuring automatic expiration after seven days.

Front‑end login form

The login page uses Element UI components to collect username, password, and a captcha, then posts to /user/user/login . Upon success, the returned token and user ID are saved via the extended localStorage methods and the user is redirected to the personal space page.

Custom annotation and AOP verification

A @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface NeedLogin { String value() default ""; } annotation marks methods that require authentication. The VerificationAspect class intercepts these methods, extracts headers ( loginType , userid , loginToken ), validates the token against Redis, and throws the appropriate custom exception if validation fails.

Usage in controllers

Controllers simply annotate protected endpoints with @NeedLogin ; the aspect handles token verification before the method proceeds, returning an error response if the token is missing, expired, or mismatched.

Result

When a user accesses a protected page, the Vue hook calls the verification endpoint; the backend validates the token via the aspect, and the client proceeds only after successful authentication, demonstrating a complete end‑to‑end token validation flow.

microservicesRedisauthenticationVue.jsSpring CloudTokenAspect-Oriented Programming
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login 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.