java生成临时令牌和访问令牌

时间:2024-10-10 10:07:44

public String getTicket(String logo, String productId) {
String aTicket = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdfs = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date d = new Date();
// 生成8位随机数字
StringBuilder str = new StringBuilder();// 定义变长字符串
Random random = new Random();
// 随机生成数字,并添加到字符串
for (int i = 0; i < 8; i++) {
str.append(random.nextInt(10));
}
// 0为临时令牌,1为访问令牌
if ("0".equals(logo)) {
// 生成年月日时分秒
String dateNowStr = sdf.format(d);
LogUtil.writeLog("格式化后的日期:" + dateNowStr);
aTicket = productId + "_" + dateNowStr + "_" + str;
LogUtil.writeLog("=========生成临时令牌:" + aTicket);
} else {
// 生成年月日时分秒毫秒
String dateNowStr = sdfs.format(d);
aTicket = "token_" + dateNowStr + "_" + str;
LogUtil.writeLog("=========生成访问令牌:" + aTicket);
}
return aTicket;
}