1、引入jasypt依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2、在application.properties中添加密钥配置:jasypt.encryptor.password=密钥值
jasypt.encryptor.password=testEncryptPassword
3、对明文进行加密
3.1、添加类注解StringEncryptor
3.2、注入StringEncryptor
3.3、调用StringEncryptor.encrypt进行加密
3.4、代码示例:
TestService.java:
package com.zjr.donny.helloworld.service;
public interface TestService {
String jasyptEncrypt(String text);
}
TestServiceImpl.java:
package com.zjr.donny.helloworld.service.impl;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.zjr.donny.helloworld.service.TestService;
@Service
@EnableEncryptableProperties
public class TestServiceImpl implements TestService {
@Autowired
private StringEncryptor stringEncryptor;
@Override
public String jasyptEncrypt(String text) {
String encryptedValue = stringEncryptor.encrypt(text);
System.out.println(text + " 加密值:" + encryptedValue);
return encryptedValue;
}
}
在Controller中使用TestService:
package com.zjr.donny.helloworld.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjr.donny.helloworld.service.TestService;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/encrypt/{text}")
public String encrypt(@PathVariable String text) {
return testService.jasyptEncrypt(text);
}
}
启动springboot后,在地址栏车输入需要加密密码
如:http://localhost:8080/encrypt/8080
(加密spring.datasource.driver-class-name的值com.mysql.cj.jdbc.Driver ),结果为:fN6wrENNHnIPNunoN6694iCjYmP0hcobB66zesRFJrlxryONtEtGDUMzEw5fS/SmUKeB29HNwzIQ78jKoebj9Q==
4、把加密后的值替换到配置文件application.properties,用“ENC(”作为前缀,“)”作为后缀包起来
spring.datasource.driver-class-name=ENC(fN6wrENNHnIPNunoN6694iCjYmP0hcobB66zesRFJrlxryONtEtGDUMzEw5fS/SmUKeB29HNwzIQ78jKoebj9Q==)
如果需要修改前后缀的值,在application.properties添加配置:
例:
jasypt.encryptor.property.prefix=HELLO[
jasypt.encryptor.property.suffix=]
5、使用自定义的加密方法
5.1、对步骤3中的TestServiceImpl.java添加Bean,实现StringEncryptor接口,重写里面的encrypt与decrypt方法
TestServiceImpl.java:
package com.zjr.donny.helloworld.service.impl;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.zjr.donny.helloworld.service.TestService;
@Service
@EnableEncryptableProperties
public class TestServiceImpl implements TestService {
@Bean("myStringEncryptor")
public StringEncryptor getStringEncryptor(Environment environment) {
String defaultPassword = "xxxx";
String jasyptPassword = environment.getRequiredProperty("jasypt.encryptor.password") == null ? defaultPassword
: environment.getRequiredProperty("jasypt.encryptor.password");
return new StringEncryptor() {
@Override
public String encrypt(String s) {
return jasyptPassword + s;
}
@Override
public String decrypt(String s) {
System.err.println("jasypt password is " + jasyptPassword);
return s.substring(jasyptPassword.length());
}
};
}
@Autowired
private StringEncryptor stringEncryptor;
@Override
public String jasyptEncrypt(String text) {
String encryptedValue = stringEncryptor.encrypt(text);
System.out.println(text + " 加密值:" + encryptedValue);
return encryptedValue;
}
}
5.2、application.properties里添加配置,指定上一步5.1的类为加密类
spring.datasource.driver-class-name=ENC(testEncryptPasswordcom.mysql.cj.jdbc.Driver)
jasypt.encryptor.password=testEncryptPassword
jasypt.encryptor.bean=myStringEncryptor