1.什么是Jasypt?
Jasypt(Java Simplified Encryption)是一个专注于简化Java加密操作的工具。 它提供了一种简单而强大的方式来处理数据的加密和解密,使开发者能够轻松地保护应用程序中的敏感信息,如数据库密码、API密钥等。 Jasypt的设计理念是简化加密操作,使其对开发者更加友好。
Jasypt加密场景
- System Property 系统变量
- Envirnment Property 环境变量
- Command Line argument 命令行参数
- Application.properties 应用配置文件
- Yaml properties 应用配置文件
- other custom property sources 其它配置文件
2.代码工程
实验目标
实验配置文件参数加密
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>jasypt</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>2.1.1</version></dependency></dependencies>
</project>
controller
获取加密的username,得到的应该是解密之后的数据
package com.et.jasypt.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
public class HelloWorldController {@Value("${username}")private String username;@RequestMapping("/hello")public Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");map.put("username", username);return map;}
}
application.yaml
Jasypt提供了一个类专门用于加密解密,提供了main方法,调用如下:
java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=pkslow algorithm=PBEWithMD5AndTripleDES input=larry
输出为:
----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.212-b10 ----ARGUMENTS------------------- input: larry algorithm: PBEWithMD5AndTripleDES password: pkslow----OUTPUT---------------------- SUfiOs8MvmAUjg+oWl/6dQ==
一种密码配置文件里面,这种多用于开发环境
server:port: 8088
username: ENC(SUfiOs8MvmAUjg+oWl/6dQ==)jasypt:encryptor:#password: pkslowalgorithm: PBEWithMD5AndTripleDES
还有一种配置启动参数里面,多用户生产环境
java -jar -Djasypt.encryptor.password=pkslow xxx.jar
DemoApplication.java
package com.et.jasypt;import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableEncryptableProperties
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- https://github.com/Harries/springboot-demo
3.测试
- 启动Spring Boot应用
- 访问http://127.0.0.1:8088/hello
- 返回明文数据
{"msg":"HelloWorld","username":"larry"}
4.引用
- Jasypt: Java simplified encryption - Jasypt: Java simplified encryption - Main
- Spring Boot集成jasypt快速入门Demo | Harries Blog™