项目中会遇到数据库名称和密码需要加密防止泄露的需求,因此引入了jasypt。下面的例子是将jasypt的加密密钥写在了配置文件中,为了安全,实际引用时,需要将配置放到服务器的启动命令中,避免在代码中暴露密钥
1.引入maven依赖jasypt-spring-boot-starter
1 2 3 4 5
| <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
|
2.启动类添加注解@EnableEncryptableProperties
1 2 3 4 5 6 7 8 9 10 11
| import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableEncryptableProperties public class AdminMain {
public static void main(String[] args) { SpringApplication.run(AdminMain.class, args); } }
|