眾所周知,阿里云的服務都是基于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.en