Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA JwtUtil

From Leeroopedia
Knowledge Sources
Domains Security, Authentication
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete tool for creating, verifying, and extracting claims from HMAC256-signed JSON Web Tokens with Redis-based token validation, provided by the auth0 java-jwt library.

Description

The JwtUtil class is a static utility for JWT operations:

  • createToken(user) — Creates a JWT with userId, name, type claims; 8-hour expiry; HMAC256 signing. Stores MD5 hash in Redis (7-day TTL) and client type (isMobile) in Redis.
  • verifierToken() — Verifies JWT signature from the current request's token header
  • getUserId() — Extracts userId claim from the request token
  • getType() — Extracts user type claim (used to block non-admin access to /sys paths)
  • getRemainingTime() — Returns remaining token validity in milliseconds

The secret key TOKEN_KEY is hardcoded. The token is read from the token HTTP header.

Usage

Import this utility when you need to:

  • Create JWT tokens after login/registration
  • Extract user ID from the current request's JWT
  • Verify token validity

Code Reference

Source Location

  • Repository: GPT-WEB-JAVA
  • File: src/main/java/com/intelligent/bot/utils/sys/JwtUtil.java

Signature

public class JwtUtil {
    public static String createToken(User user);
    public static boolean verifierToken();
    public static long getRemainingTime();
    public static Long getUserId();
    public static Integer getType();
}

Import

import com.intelligent.bot.utils.sys.JwtUtil;

I/O Contract

Inputs

Name Type Required Description
user User Yes (createToken) User entity with id, name, type, isMobile
(request) HttpServletRequest Yes (others) Token read from "token" header

Outputs

Name Type Description
createToken returns String Signed JWT string (8h expiry)
verifierToken returns boolean True if JWT signature valid
getUserId returns Long User ID from token, or 0L if invalid
getType returns Integer User type from token, or 0 if invalid

Usage Examples

Creating a Token

String token = JwtUtil.createToken(user);
// Token contains: issuer=SYSTEM, subject=BEAN, audience=SSP
// Claims: userId, name, type
// Expiry: 8 hours from now
// Side effects:
//   Redis: "token:{userId}" → md5(token), TTL=7 days
//   Redis: "client:{userId}" → isMobile, TTL=7 days

Extracting User ID in a Controller

Long userId = JwtUtil.getUserId();
// Reads "token" header from current request
// Verifies JWT signature
// Returns userId claim, or 0L on failure

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment