【7.2.4】【rule】更新token签名工具

pull/34/head
fengshuonan 2022-08-01 18:00:44 +08:00
parent 83b1f3a480
commit 1cb6a9a455
1 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,90 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
/**
* Token
*
* @author fengshuonan
* @date 2022/8/1 17:41
*/
@Slf4j
public class TokenSignUtil {
/**
* md5
*
* @param timestamp sign
* @param secretKey
* @author fengshuonan
* @date 2022/8/1 17:43
*/
public static String createSignStr(Long timestamp, String secretKey) {
if (ObjectUtil.isEmpty(timestamp) || ObjectUtil.isEmpty(secretKey)) {
return null;
} else {
return SecureUtil.md5(timestamp + secretKey);
}
}
/**
*
*
* @param timestamp
* @param secretKey
* @param signStr
* @param expiredSeconds
* @author fengshuonan
* @date 2022/8/1 17:48
*/
public static boolean validateSignStr(Long timestamp, String secretKey, String signStr, Integer expiredSeconds) {
if (ObjectUtil.isEmpty(timestamp) || ObjectUtil.isEmpty(secretKey) || ObjectUtil.isEmpty(signStr) || ObjectUtil.isEmpty(expiredSeconds)) {
return false;
}
long betweenSeconds = (System.currentTimeMillis() - timestamp) / 1000;
// 如果当前时间和时间戳时间相隔太长,则返回校验失败,过期
if (betweenSeconds > expiredSeconds) {
return false;
}
// 正确的签名
String signRight = TokenSignUtil.createSignStr(timestamp, secretKey);
// 比对签名
if (signRight.equals(signStr)) {
return true;
}
return false;
}
}