parent
0e2d4f52d4
commit
7ec99eb6d7
@ -0,0 +1,50 @@
|
||||
package com.dsideal.aiSupport.Util.KeLing;
|
||||
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.dsideal.aiSupport.Plugin.YamlProp;
|
||||
import com.jfinal.kit.Prop;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
|
||||
|
||||
public class JWTDemo {
|
||||
|
||||
static String ak; // 填写access key
|
||||
static String sk; // 填写secret key
|
||||
public static Prop PropKit; // 配置文件工具
|
||||
static {
|
||||
//加载配置文件
|
||||
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
||||
PropKit = new YamlProp(configFile);
|
||||
ak = PropKit.get("KeLing.ak");
|
||||
sk = PropKit.get("KeLing.sk");
|
||||
}
|
||||
static String sign(String ak,String sk) {
|
||||
try {
|
||||
Date expiredAt = new Date(System.currentTimeMillis() + 1800*1000); // 有效时间,此处示例代表当前时间+1800s(30min)
|
||||
Date notBefore = new Date(System.currentTimeMillis() - 5*1000); //开始生效的时间,此处示例代表当前时间-5秒
|
||||
Algorithm algo = Algorithm.HMAC256(sk);
|
||||
Map<String, Object> header = new HashMap<>();
|
||||
header.put("alg", "HS256");
|
||||
return JWT.create()
|
||||
.withIssuer(ak)
|
||||
.withHeader(header)
|
||||
.withExpiresAt(expiredAt)
|
||||
.withNotBefore(notBefore)
|
||||
.sign(algo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String token = sign(ak, sk);
|
||||
System.out.println(token); // 打印生成的API_TOKEN
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue