51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Spring Boot集成jasypt快速入门Demo

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.代码工程

实验目标

实验配置文件参数加密

<?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;
@RestControllerpublic 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: larryalgorithm: PBEWithMD5AndTripleDESpassword: pkslow
----OUTPUT----------------------SUfiOs8MvmAUjg+oWl/6dQ==

一种密码配置文件里面,这种多用于开发环境 * * * * * * * * *

server:  port: 8088username: ENC(SUfiOs8MvmAUjg+oWl/6dQ==)
jasypt:  encryptor:    #password: pkslow    algorithm: 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@EnableEncryptablePropertiespublic 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.引用

  • https://segmentfault.com/a/1190000021284801?u_atoken=b8ce51db-83ad-4b2a-9782-ad7967e77ce1&u_asig=2760822e17196702357218152ec54c&u_aref=%2BfDlNaFBEeWTu%2FY4ffsxEfUQfEA%3D

  • http://www.jasypt.org/

  • http://www.liuhaihua.cn/archives/710802.html

赞(3)
未经允许不得转载:工具盒子 » Spring Boot集成jasypt快速入门Demo