社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
3、两种方式给明文进行加密
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static sun.plugin.javascript.navig.JSType.Embed;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class JasyptTest {
@Autowired
StringEncryptor encryptor;
//加密
@Test
public void getPass(){
String name = encryptor.encrypt("hello");
String password = encryptor.encrypt("world");
System.out.println(name); //BEJsOY+ny6/Bo+B8Rv2f9Q==
System.out.println(password); //+w8Jn3Iv1R7nsFa6fO24Dw==
}
//解密
@Test
public void passDecrypt(){
String username = encryptor.decrypt("BEJsOY+ny6/Bo+B8Rv2f9Q==");
String password = encryptor.decrypt("+w8Jn3Iv1R7nsFa6fO24Dw==");
System.out.println(username+"--"+password);
}
}
2、通过命令行运行 jasypt-1.9.2.jar 包命令来加密解密;
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI
input=需要被加密的明文内容
password=密钥(盐值)
algorithm=加密方式
例如,我们要对明文用户名“hello”进行加密,密码为helloworld,加密方式为PBEWithMD5AndDES
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="hello" password=helloworld algorithm=PBEWithMD5AndDES
运行结果如下:
D:usermavenrespositoryorgjasyptjasypt1.9.2>java -cp jasypt-1.9.2.jar
org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="hello"
password=helloworld algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.172-b11
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: hello
password: helloworld
----OUTPUT----------------------
wizvEdztMylxBFptawIo/w==
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="wizvEdztMylxBFptawIo/w==" password=helloworld algorithm=PBEWithMD5AndDES
运行结果如下:
D:usermavenrespositoryorgjasyptjasypt1.9.2>java -cp jasypt-1.
9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="wizvEdztMyl
xBFptawIo/w==" password=helloworld algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.172-b11
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: wizvEdztMylxBFptawIo/w==
password: helloworld
----OUTPUT----------------------
hello
4、将配置文件中原来的明文账号密码改为加密后的密文
注意:因为默认情况下jasypt-spring-boot解析密码的规则是前缀是ENC
配置文件中jasypt.encryptor.password的值必须和命令行中的password属性值保持一致,否则springboot在启动时候自动解析密文的时候会报错,无法解析密文
5、测试
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JasyptController {
@Value("${cmd.username}")
private String cmdUsername;
@Value("${cmd.password}")
private String cmdPassword;
@Value("${code.username}")
private String codeUsername;
@Value("${code.password}")
private String codePassord;
@RequestMapping("/jasypt")
public String testJasypt() {
return "我是命令行加密的:" +cmdUsername+"-->"+cmdPassword +"<br>"+"我是代码生成的:"+codeUsername+"-->"+ codePassord;
}
}
运行结果如下:
备注: 使用代码和命令行 密钥(盐值)也是一样的对同一字符串进行加密,为什么生成的密文不一样的;猜测可能是加密方式不一样,后续 查看源码看看代码加密使用什么加密方式
密钥放在启动脚本里 java -jar test.jar jasypt.encryptor.password=text & 或者放在环境变量里
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!