ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

jasypt加密

2022-06-16 16:33:28  阅读:177  来源: 互联网

标签:加密 String jasypt text encryptor password


Jasypt Spring Boot 为 Spring Boot 应用程序中的属性源提供加密支持。
有 3 种方法可以集成jasypt-spring-boot到您的项目中:

  • jasypt-spring-boot-starter如果使用@SpringBootApplication@EnableAutoConfiguration将在整个 Spring 环境中启用可加密属性,只需将启动器 jar 添加到您的类路径
  • 添加jasypt-spring-boot到您的类路径并添加@EnableEncryptableProperties到您的主配置类以在整个 Spring 环境中启用可加密属性
  • 添加jasypt-spring-boot到您的类路径并声明单独的可加密属性源@EncrytablePropertySource

 

1 pom.xml文件添加依赖 

<dependency>     <groupId>com.github.ulisesbocchio</groupId>     <artifactId>jasypt-spring-boot-starter</artifactId>     <version>3.0.3</version> </dependency>

2 参数

Key Required Default Value
jasypt.encryptor.password True -
jasypt.encryptor.algorithm False PBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.key-obtention-iterations False 1000
jasypt.encryptor.pool-size False 1
jasypt.encryptor.provider-name False SunJCE
jasypt.encryptor.provider-class-name False null
jasypt.encryptor.salt-generator-classname False org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.iv-generator-classname False org.jasypt.iv.RandomIvGenerator
jasypt.encryptor.string-output-type False base64
jasypt.encryptor.proxy-property-sources False false
jasypt.encryptor.skip-property-sources False empty list

2 cmd方式加密密码

input:为要加密的密码

password:密钥

algorithm:采用的加密算法

java -cp C:/Users/Administrator/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=qwer*1234* algorithm=PBEWithMD5AndDES

3 工具类加解密:

@Slf4j public class JasyptUtil {       /**      * 加密      * @param text 需要加密的字符串      * @return String      */     public static String encrypt(String text) {         return encrypt(text,"scrm");     }       /**      *  解密      * @param text 需要加密的字符串      * @param password 加密密码      * @return String      */     public static String encrypt(String text,String password) {         StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();         //加密配置         EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();         config.setAlgorithm("PBEWithMD5AndDES");         //自己在用的时候更改此密码         config.setPassword(password);         //应用配置         encryptor.setConfig(config);         return encryptor.encrypt(text);     }       /**      *  解密      * @param text 需要加密的字符串      * @return String      */     public static String decrypt(String text) {         return decrypt(text,"scrm");     }       /**      *  解密      * @param text 需要解密的字符串      * @param password 解密密码      * @return String      */     public static String decrypt(String text,String password) {           StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();         //加密配置         EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();         config.setAlgorithm("PBEWithMD5AndDES");         //自己在用的时候更改此密码         config.setPassword(password);         //应用配置         encryptor.setConfig(config);           //解密         return encryptor.decrypt(text);     } }

4 配置文件中 

jasypt.encryptor.algorithm = scrm或者zstest jasypt.encryptor.algorithm = PBEWithMD5AndDES jasypt.encryptor.iv-generator-classname = org.jasypt.iv.NoIvGenerator   spring.datasource.password = ENC(HTkX8gEYcsi60YFmsoGCrQ==)

二:原理

2.1、执行过程

2.2、主要组件

StringEncryptor 加密解密处理器

EncryptablePropertySourceConverter  用于调用配置文件转件器

EncryptablePropertyResolver 配置处理器

EncryptablePropertyDetector 是否加密的检查器

EncryptablePropertyFilter 过滤器,用于过滤哪些class无需处理

标签:加密,String,jasypt,text,encryptor,password
来源: https://www.cnblogs.com/yaochunhui/p/16382448.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有