配置文件不写明文,按照如下配置,一键搞定。
第一步:添加maven依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
第二步,编写加密配置类
import lombok.AllArgsConstructor;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class SpringBootConfigEncrypt implements CommandLineRunner {
private ApplicationContext applicationContext;
private StringEncryptor encryptor;
@Override
public void run(String... args) {
Environment environment = applicationContext.getBean(Environment.class);