com.github.dennisit.vplus.data.security.jwt.JWTConfig Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.security.jwt;
import com.github.dennisit.vplus.data.utils.RSAUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.Serializable;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* security config with jwt
*/
@Data
@Slf4j
@ConfigurationProperties(prefix = "wuyu.jwt")
public class JWTConfig implements Serializable {
/**
* 密钥
*/
private String secret;
/**
* 公钥
*/
private PublicKey publicKey;
/**
* 私钥
*/
private PrivateKey privateKey;
/**
* 公钥
*/
private String publicKeyPath;
/**
* 私钥
*/
private String privateKeyPath;
/**
* token过期时间
*/
private int expire;
/**
* 归属域
*/
private String domain;
/**
* cookie名
*/
private String cookieName;
/**
* cookie最大时间
*/
private Integer cookieMaxAge;
@PostConstruct
public void init() {
try {
File pubKey = new File(publicKeyPath);
File priKey = new File(privateKeyPath);
if (!pubKey.exists() || !priKey.exists()) {
// 生成公钥和私钥
RSAUtils.generateKey(publicKeyPath, privateKeyPath, secret);
}
// 获取公钥和私钥
this.publicKey = RSAUtils.getPublicKey(publicKeyPath);
this.privateKey = RSAUtils.getPrivateKey(privateKeyPath);
} catch (Exception e) {
throw new RuntimeException("初始化公钥和私钥失败!", e);
}
}
}