mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-26 01:16:36 +08:00
feat: v1.7.0
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user