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?