1.引入依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
配置文件中
db.password=ENC(XvP2P4H3cLzf8r/ak91xLg==)
2.controller中
package com.example.configcenter; import org.jasypt.intf.service.JasyptStatelessService; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RequestMapping("/ConfigClientDemo") @RestController public class ConfigClientDemoController { @Value("${db.password}") private String testName;
@PostMapping("/getServiceMethod") public String echo(String name){ return "wahaha"+name; } @PostMapping("/encrypt") public String encrypt(String input,String encodeKey){ JasyptStatelessService service = new JasyptStatelessService(); String result = service.encrypt(input, encodeKey, (String)null, (String)null, "PBEWithMD5AndDES", (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null, (String)null); return result ; } }
3.启动参数中加入密钥
-Djasypt.encryptor.password=pwd
4.调用
http://localhost:8705/ConfigClientDemo/encrypt?input=123456&encodeKey=pwd
返回:XvP2P4H3cLzf8r/ak91xLg==
将此 放入配置文件中
重启项目
访问方法
"wahaha"+str