一、使用uuid加时间戳的方式
自定义时间expiryTime,因为时间为毫秒值,所以推荐使用乘的方式,这样方便后续修改,1000L就是1秒的意思,60*1000L就是一分钟,以此类推。
private static long expiryTime = 24*60*60*1000L;
使用uuid加时间戳的方式生成自定义的token。当然也可以加入其他的字段进行拼接生成token。
public static String generateToken() {
String uuid = UUID.randomUUID().toString(); // 使用UUID生成唯一的token
long expiryTimestamp = Instant.now().getEpochSecond() + expiryTime; // 计算过期时间戳
String token = uuid + "_" + expiryTimestamp;
return token;
}
isTokenExpired是验证token的方法,通过截取时间戳的方式进行验证,如果返回的是true,则表示token没有失效。反之,token失效。
public static boolean isTokenExpired(String token) {
String[] tokenParts = token.split("_");
long expiryTimestamp = Long.parseLong(tokenParts[tokenParts.length - 1]); // 获取 token 中的过期时间戳
long currentTimestamp = Instant.now().getEpochSecond(); // 获取当前时间戳
return currentTimestamp < expiryTimestamp;
}
二、融合username(用户名),password(密码的token)。
因为用户名和密码都是敏感数据,所以对他们进行加密操作。
使用AES对称加密算法加密用户名和密码。
package org.example;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
private static final String AES_ALGORITHM = "AES";
private static final String KEY = "YourSecretKey123";
// 加密方法
public static String encrypt(String data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密方法
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes);
}
}
将加密的用户名和密码塞入生成的token中。
private static long expiryTime = 24*60*60*1000L;
public static String generateToken(String username,String password) {
String uuid = UUID.randomUUID().toString(); // 使用UUID生成唯一的token
long expiryTimestamp = Instant.now().getEpochSecond() + expiryTime; // 计算过期时间戳
String token = username + "-" + password + "-" + expiryTimestamp ;
return token;
}
可以看到都能正常使用
全部代码
生成token以及,获取token中的数值。
import java.time.Instant;
public class TokenGenerator {
private static long expiryTime = 24*60*60*1000L;
public static String generateToken(String username,String password) {
long expiryTimestamp = Instant.now().getEpochSecond() + expiryTime; // 计算过期时间戳
String token = username + "-" + password + "-" + expiryTimestamp ;
return token;
}
public static long getCurrentTimestamp(String token) {
String[] tokenParts = token.split("-");
long expiryTimestamp = Long.parseLong(tokenParts[tokenParts.length - 1]); // 获取 token 中的过期时间戳
return expiryTimestamp;
}
public static String getUsername(String token) {
String[] tokenParts = token.split("-");
String username = tokenParts[0]; // 获取 token 中的用户名
return username;
}
public static String getPassword(String token) {
String[] tokenParts = token.split("-");
String password = tokenParts[1]; // 获取 token 中的密码
return password;
}
public static boolean isTokenExpired(String token) {
String[] tokenParts = token.split("-");
long expiryTimestamp = Long.parseLong(tokenParts[tokenParts.length - 1]); // 获取 token 中的过期时间戳
long currentTimestamp = Instant.now().getEpochSecond(); // 获取当前时间戳
return expiryTimestamp < currentTimestamp ;
}
}
加密以及解密方法
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
private static final String AES_ALGORITHM = "AES";
private static final String KEY = "YourSecretKey123";
// 加密方法
public static String encrypt(String data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密方法
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes);
}
}