欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > SpringBoot中安全的设置阿里云日志SLS的accessKey

SpringBoot中安全的设置阿里云日志SLS的accessKey

2025/3/26 18:04:22 来源:https://blog.csdn.net/a825245794/article/details/146461699  浏览:    关键词:SpringBoot中安全的设置阿里云日志SLS的accessKey

  众所周知,阿里云的服务都是基于accesskeyId和accesskeySecret来进行身份鉴权的,但唯独日志因为需要写入到.xml文件里对于accesskeyId和accesskeySecret需要进行一定程度的改进,尤其是使用了jasypt进行加密的参数传递进去logback.xml更是会遇到需要对参数进行解密的问题,而官网只有简单粗略的带过如何自定义传入accessKey,以下由我来说一下我的改造计划. 

1.首先需要引入阿里云日志SLS和jasypt相关Maven包

 <dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>2.5.0</version>
</dependency><dependency><groupId>com.aliyun.openservices</groupId><artifactId>aliyun-log-logback-appender</artifactId><version>0.1.29</version>
</dependency>

2.自定义 jasypt.encryptor.password 密码并创建自定义jasypt的bean(务必先自定义jasypt密码),然后放入环境变量中或者你偷懒直接先写死在代码里,在环境变量中通过System.getenv来获取


import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.iv.RandomIvGenerator;
import org.jasypt.salt.RandomSaltGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class JasyptConfig {@Bean("jasyptStringEncryptor")public StringEncryptor stringEncryptor(){StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();encryptor.setAlgorithm("PBEWithMD5AndDES");encryptor.setIvGenerator(new RandomIvGenerator());encryptor.setSaltGenerator(new RandomSaltGenerator());encryptor.setStringOutputType("base64");encryptor.setPassword(System.getenv("jasypt.e

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词