SpringBoot如何详细整合jasypt加密工具?

2026-05-21 09:041阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计553个文字,预计阅读时间需要3分钟。

SpringBoot如何详细整合jasypt加密工具?

在pom.xml中添加jasypt依赖:

xml com.github.ulisesbocchio jasypt-spring-boot-starter 2.1.1

jasypt 2.1.1与spring-boot 2.2.6的兼容性最佳,避免踩坑。

依赖引入pom.xml

<!-- jasypt核心依赖 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> <!-- jasypt2.1.1与spring-boot2.2.6的兼容性是最好的,避免踩坑,泪呀 --> </dependency> <!-- jasypt-maven插件,不影响基本功能 --> <plugin> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-maven-plugin</artifactId> <version>3.0.3</version> </plugin>

配置参数application.properties

jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)

SpringBoot如何详细整合jasypt加密工具?

测试代码

package com.yang.ftpdemo.controller; import lombok.Data; import org.jasypt.encryption.StringEncryptor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/crypt") public class CryptController { @Resource private StringEncryptor encrypt; @Resource private CryptConfig cryptConfig; @GetMapping("/encrypt") public CryptConfig encrypt() { String username = encrypt.encrypt("root"); String password = encrypt.encrypt("root123"); CryptConfig crypt = new CryptConfig(); crypt.setPassword(password); crypt.setUserName(username); return crypt; } @GetMapping("/decrypt") public CryptConfig decrypt() { CryptConfig crypt = new CryptConfig(); BeanUtils.copyProperties(this.cryptConfig, crypt); return crypt; } } @Data @Configuration @ConfigurationProperties(prefix = "crypt") class CryptConfig { private String userName; private String password; }

测试

浏览器访问得到加密结果,并且每次请求结果不一样:

{ "userName":"XsWOwhZIag8XBh3DFl4sqA==", "password":"3kD2/u+xnM1i5mO2cVMWKw==" }

浏览器访问得到解密结果,每次请求结果一样:

{ "userName":"root", "password":"root123" }

## Maven插件用法不可用,后面补充吧......

mvn jasypt:encrypt -Djasypt.encryptor.password="myPass"

可能会报如下错误,表示本地环境未安装JDK的JCE模块,它是JDK提供的加密扩展,需要到Oracle官网手动下载,并安装:

下载地址

[ERROR] Failed to execute goal com.github.ulisesbocchio:jasypt-maven-plugin:3.0.3:encrypt (default-cli) on project ftp-demo: Error Encrypting: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed
the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine -> [Help 1]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计553个文字,预计阅读时间需要3分钟。

SpringBoot如何详细整合jasypt加密工具?

在pom.xml中添加jasypt依赖:

xml com.github.ulisesbocchio jasypt-spring-boot-starter 2.1.1

jasypt 2.1.1与spring-boot 2.2.6的兼容性最佳,避免踩坑。

依赖引入pom.xml

<!-- jasypt核心依赖 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> <!-- jasypt2.1.1与spring-boot2.2.6的兼容性是最好的,避免踩坑,泪呀 --> </dependency> <!-- jasypt-maven插件,不影响基本功能 --> <plugin> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-maven-plugin</artifactId> <version>3.0.3</version> </plugin>

配置参数application.properties

jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)

SpringBoot如何详细整合jasypt加密工具?

测试代码

package com.yang.ftpdemo.controller; import lombok.Data; import org.jasypt.encryption.StringEncryptor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/crypt") public class CryptController { @Resource private StringEncryptor encrypt; @Resource private CryptConfig cryptConfig; @GetMapping("/encrypt") public CryptConfig encrypt() { String username = encrypt.encrypt("root"); String password = encrypt.encrypt("root123"); CryptConfig crypt = new CryptConfig(); crypt.setPassword(password); crypt.setUserName(username); return crypt; } @GetMapping("/decrypt") public CryptConfig decrypt() { CryptConfig crypt = new CryptConfig(); BeanUtils.copyProperties(this.cryptConfig, crypt); return crypt; } } @Data @Configuration @ConfigurationProperties(prefix = "crypt") class CryptConfig { private String userName; private String password; }

测试

浏览器访问得到加密结果,并且每次请求结果不一样:

{ "userName":"XsWOwhZIag8XBh3DFl4sqA==", "password":"3kD2/u+xnM1i5mO2cVMWKw==" }

浏览器访问得到解密结果,每次请求结果一样:

{ "userName":"root", "password":"root123" }

## Maven插件用法不可用,后面补充吧......

mvn jasypt:encrypt -Djasypt.encryptor.password="myPass"

可能会报如下错误,表示本地环境未安装JDK的JCE模块,它是JDK提供的加密扩展,需要到Oracle官网手动下载,并安装:

下载地址

[ERROR] Failed to execute goal com.github.ulisesbocchio:jasypt-maven-plugin:3.0.3:encrypt (default-cli) on project ftp-demo: Error Encrypting: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed
the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine -> [Help 1]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。