feat: v1.7.0

This commit is contained in:
meilin.huang
2024-01-17 17:02:15 +08:00
parent 94da6df33e
commit 63f0615445
6 changed files with 47 additions and 23 deletions

View File

@@ -142,32 +142,48 @@ const (
// 获取系统的RSA公钥
func GetRsaPublicKey() (string, error) {
content, err := os.ReadFile(publicKeyFile)
if err != nil {
if cache.UseRedisCache() {
publicKey := cache.GetStr(publicKeyK)
if publicKey != "" {
return publicKey, nil
}
_, pubKey, err := GenerateAndSaveRSAKey()
return pubKey, err
} else {
content, err := os.ReadFile(publicKeyFile)
if err != nil {
publicKey := cache.GetStr(publicKeyK)
if publicKey != "" {
return publicKey, nil
}
} else {
return string(content), nil
}
}
return string(content), nil
_, pubKey, err := GenerateAndSaveRSAKey()
return pubKey, err
}
// 获取系统私钥
func GetRsaPrivateKey() (string, error) {
content, err := os.ReadFile(privateKeyFile)
if err != nil {
privateKey := cache.GetStr(privateKeyK)
if privateKey != "" {
return privateKey, nil
if cache.UseRedisCache() {
priKey := cache.GetStr(privateKeyK)
if priKey != "" {
return priKey, nil
}
} else {
content, err := os.ReadFile(privateKeyFile)
if err != nil {
priKey := cache.GetStr(privateKeyK)
if priKey != "" {
return priKey, nil
}
} else {
return string(content), nil
}
privateKey, _, err := GenerateAndSaveRSAKey()
return privateKey, err
}
return string(content), nil
priKey, _, err := GenerateAndSaveRSAKey()
return priKey, err
}
// 生成并保存rsa key优先保存于磁盘若磁盘保存失败则保存至缓存
@@ -179,6 +195,14 @@ func GenerateAndSaveRSAKey() (string, string, error) {
return "", "", err
}
// 如果使用了redis缓存则优先存入redis
if cache.UseRedisCache() {
logx.Debug("系统配置了redis, rsa存入redis")
cache.SetStr(privateKeyK, privateKey, -1)
cache.SetStr(publicKeyK, publicKey, -1)
return privateKey, publicKey, nil
}
err = os.WriteFile(privateKeyFile, []byte(privateKey), 0644)
if err != nil {
logx.ErrorTrace("RSA私钥写入磁盘文件失败, 使用缓存存储该私钥", err)