java 做直播網(wǎng)站有哪些軟件有哪些最新app推廣項目平臺
文章目錄
- 一、介紹
- 二、Spring集成
- 1、 Maven依賴
- 2、application.xml的配置
- 3、配置文件使用
- 4、方法加密
- 二、SpringBoot集成
- 1、 Maven依賴
- 2、 Java Bean配置jasyptStringEncryptor
- 3、配置文件使用
- 4、Bean使用加密字段自動解密
一、介紹
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
Jasypt是一個Java庫,它允許開發(fā)人員以最小的努力為他/她的項目添加基本的加密功能,而無需深入了解密碼學(xué)的工作原理。
- High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries…
基于標(biāo)準(zhǔn)的高安全性加密技術(shù),適用于單向和雙向加密。加密密碼,文本,數(shù)字,二進(jìn)制文件…- Transparent integration with Hibernate.
完美地與 Hibernate 集成。- Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.
適合集成到 Spring項目中,也可以完美地與 Spring Security集成。- Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
用于加密應(yīng)用程序(即數(shù)據(jù)源)配置的集成功能。- Specific features for high-performance encryption in multi-processor/multi-core systems.
多處理器/多核系統(tǒng)中高性能加密的特定功能。- Open API for use with any JCE provider.
開放 API 以與任何 JCE 提供程序一起使用。- …and much more
…等等
二、Spring集成
1、 Maven依賴
<dependency><groupId>org.jasypt</groupId><artifactId>jasypt-spring31</artifactId><version>1.9.3</version>
</dependency>
<dependency><groupId>org.jasypt</groupId><artifactId>jasypt</artifactId><version>1.9.3</version>
</dependency>
2、application.xml的配置
<bean id="environmentVariablesConfiguration"class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"><!--加密方法--><property name="algorithm" value="PBEWithMD5AndDES" /><!--密碼--><property name="password" value="mysqlPwd" /><!-- 如果想獲得系統(tǒng)環(huán)境變量 --><!-- <property name="password" value="#{systemEnvironment['ENV_VARIABLE_NAME']}"/> -->
</bean>
<!-- 配置加密器,將用于解密 -->
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"><property name="config" ref="environmentVariablesConfiguration" />
</bean>
<!--jasypt掃描配置文件,對配置文件里加密數(shù)據(jù)進(jìn)行解密-->
<bean id="propertyConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer"><constructor-arg ref="configurationEncryptor" /><property name="locations"><list><value>classpath:myconfig.properties</value><value>classpath:application.properties</value></list></property>
</bean>
<!--給bean的屬性解密并賦值-->
<bean id="myBean" class="com.example.MyBean"><property name="pwd" value="${datasource.username}"/><property name="user" value="${datasource.password}"/>
</bean>
3、配置文件使用
使用 ENC(密文)
datasource.driver_class=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/mydatabase
datasource.username=ENC(aXZJTKwF6Vt147qUJOrMuT3NDV4y0NzG)
datasource.password=ENC(LWzlg7fvAhO8RMIDDxifEORimjA91ibn)
4、方法加密
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword("myPassword");
String encrypted = encryptor.encrypt("敏感信息");//密碼加密
System.out.println(encrypted);
二、SpringBoot集成
1、 Maven依賴
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.5</version>
</dependency>
2、 Java Bean配置jasyptStringEncryptor
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword("mypassword");config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);return encryptor;
}
3、配置文件使用
使用 ENC(密文)
datasource.driver_class=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/mydatabase
datasource.username=ENC(aXZJTKwF6Vt147qUJOrMuT3NDV4y0NzG)
datasource.password=ENC(LWzlg7fvAhO8RMIDDxifEORimjA91ibn)
4、Bean使用加密字段自動解密
直接用@Value(“${配置文件的Key}”)
@RestController
@RequestMapping("/test")
public class TestController {@Value("${username}")private String username;@Value("${password}")private String password;
}
參考:https://blog.csdn.net/weixin_42962634/article/details/122455123