Implementation:A616567126 GPT WEB JAVA AuthorizationInterceptor
| Knowledge Sources | |
|---|---|
| Domains | Security, Access_Control |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete tool for intercepting HTTP requests to validate JWT tokens against Redis-stored hashes and enforce URL whitelist and admin path access control, provided by the Spring MVC framework.
Description
The AuthorizationInterceptor class implements HandlerInterceptor and runs before every controller method. The preHandle() method:
- Checks if the request path is in the URL whitelist (
CommandLineInitResource.urlList); rejects if not - Blocks non-admin users (type != -1) from accessing paths containing "sys"
- Extracts userId from JWT and validates the MD5(token) against the Redis-stored hash
- Returns 403 with JSON error if any check fails
This provides a hybrid stateless/stateful authentication: the JWT signature is verified (stateless), and the token is also checked against Redis (stateful, enabling server-side revocation).
Usage
Import this interceptor when you need to:
- Understand request authentication flow
- Add or modify URL whitelist entries
- Debug authentication failures
Code Reference
Source Location
- Repository: GPT-WEB-JAVA
- File: src/main/java/com/intelligent/bot/config/interceptor/AuthorizationInterceptor.java
Signature
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object handler) throws IOException;
}
Import
import com.intelligent.bot.config.interceptor.AuthorizationInterceptor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | HttpServletRequest | Yes | Current HTTP request with "token" header |
Outputs
| Name | Type | Description |
|---|---|---|
| preHandle returns | boolean | true if request allowed, false if blocked (with JSON error response) |
Usage Examples
Authentication Flow
// preHandle() executes on every request:
// 1. Check URL whitelist: CommandLineInitResource.urlList.contains(servletPath)
// → If not found: return 403 "Illegal exception request"
// 2. Check admin access: JwtUtil.getType() != -1 && path.contains("sys")
// → If non-admin accessing /sys: return 403 "暂无访问权限"
// 3. Validate token: Redis token hash == MD5(request token)
// → If mismatch: return 403 "身份校验失败,请重新登录"
// 4. Return true (request proceeds to controller)
Registered in InterceptorConfiguration
// InterceptorConfiguration registers the interceptor:
// registry.addInterceptor(authorizationInterceptor)
// .addPathPatterns("/**")
// .excludePathPatterns(WhiteApiList.WHITE_API_LIST);