自定义生成token 使用UUID加时间戳的方式或使用账号、密码加时间戳的方式

 一、使用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);
    }
}

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值