前端新增记住我勾选框,登录页挂载时自动恢复已保存的用户名,勾选时将用户名存入本地存储并在登录请求中携带对应参数;后端新增rememberMe参数处理逻辑,配置7天免登录的JWT有效期,重载JWT工具类以支持自定义令牌过期时间
150 lines
3.9 KiB
Java
150 lines
3.9 KiB
Java
package com.bycrm.util;
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Jwts;
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
import io.jsonwebtoken.security.Keys;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.crypto.SecretKey;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* JWT 工具类
|
|
*/
|
|
@Component
|
|
public class JwtUtil {
|
|
|
|
@Value("${jwt.secret}")
|
|
private String secret;
|
|
|
|
@Value("${jwt.expiration}")
|
|
private Long expiration;
|
|
|
|
@Value("${jwt.remember-expiration:604800000}")
|
|
private Long rememberExpiration;
|
|
|
|
public Long getRememberExpiration() {
|
|
return rememberExpiration;
|
|
}
|
|
|
|
/**
|
|
* 生成 Token
|
|
*/
|
|
public String generateToken(Long userId, String username, Integer role, Long dealerId) {
|
|
return generateToken(userId, username, role, dealerId, expiration);
|
|
}
|
|
|
|
/**
|
|
* 生成指定有效期的 Token(用于"记住我"延长有效期)
|
|
*/
|
|
public String generateToken(Long userId, String username, Integer role, Long dealerId, long expirationMillis) {
|
|
Map<String, Object> claims = new HashMap<>();
|
|
claims.put("userId", userId);
|
|
claims.put("username", username);
|
|
claims.put("role", role);
|
|
claims.put("dealerId", dealerId);
|
|
return generateToken(claims, expirationMillis);
|
|
}
|
|
|
|
/**
|
|
* 生成 Token
|
|
*/
|
|
public String generateToken(Map<String, Object> claims) {
|
|
return generateToken(claims, expiration);
|
|
}
|
|
|
|
/**
|
|
* 生成指定有效期的 Token
|
|
*/
|
|
public String generateToken(Map<String, Object> claims, long expirationMillis) {
|
|
Date now = new Date();
|
|
Date expiryDate = new Date(now.getTime() + expirationMillis);
|
|
|
|
return Jwts.builder()
|
|
.setClaims(claims)
|
|
.setIssuedAt(now)
|
|
.setExpiration(expiryDate)
|
|
.signWith(getSigningKey(), SignatureAlgorithm.HS512)
|
|
.compact();
|
|
}
|
|
|
|
/**
|
|
* 从 Token 中获取 Claims
|
|
*/
|
|
public Claims getClaimsFromToken(String token) {
|
|
return Jwts.parserBuilder()
|
|
.setSigningKey(getSigningKey())
|
|
.build()
|
|
.parseClaimsJws(token)
|
|
.getBody();
|
|
}
|
|
|
|
/**
|
|
* 从 Token 中获取用户ID
|
|
*/
|
|
public Long getUserIdFromToken(String token) {
|
|
Claims claims = getClaimsFromToken(token);
|
|
return claims.get("userId", Long.class);
|
|
}
|
|
|
|
/**
|
|
* 从 Token 中获取用户名
|
|
*/
|
|
public String getUsernameFromToken(String token) {
|
|
Claims claims = getClaimsFromToken(token);
|
|
return claims.get("username", String.class);
|
|
}
|
|
|
|
/**
|
|
* 从 Token 中获取角色
|
|
*/
|
|
public Integer getRoleFromToken(String token) {
|
|
Claims claims = getClaimsFromToken(token);
|
|
return claims.get("role", Integer.class);
|
|
}
|
|
|
|
/**
|
|
* 从 Token 中获取经销商ID
|
|
*/
|
|
public Long getDealerIdFromToken(String token) {
|
|
Claims claims = getClaimsFromToken(token);
|
|
return claims.get("dealerId", Long.class);
|
|
}
|
|
|
|
/**
|
|
* 验证 Token 是否过期
|
|
*/
|
|
public boolean isTokenExpired(String token) {
|
|
try {
|
|
Date expiration = getClaimsFromToken(token).getExpiration();
|
|
return expiration.before(new Date());
|
|
} catch (Exception e) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证 Token
|
|
*/
|
|
public boolean validateToken(String token) {
|
|
try {
|
|
return !isTokenExpired(token);
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取签名密钥
|
|
*/
|
|
private SecretKey getSigningKey() {
|
|
byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
|
|
return Keys.hmacShaKeyFor(keyBytes);
|
|
}
|
|
}
|