1.引入jasypt依赖包 <!-- 数据库加密 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.16</version> </dependency>
2.application.properties进行配置加密秘钥,这个可以随便写(这步不能少):
jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7
3. 加密字段生成,用户名,密码都可以通过同样的方法生成加密后的字符串,但是每次运行就会生成新的加密字符串,但原有的加密字符串还可以继续使用,加密后的字符串在进行请求链接的时候会自动进行解密链接:
import org.jasypt.encryption.StringEncryptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.text.DecimalFormat; @Controller @RequestMapping("/demo") public class Demo { @Autowired StringEncryptor encryptor; @GetMapping("/") @ResponseBody public void test() { String username = encryptor.encrypt("root"); System.out.println(username); String password = encryptor.encrypt("123456"); System.out.println(password); } }
4.通过运行上边的代码就会生成加密字符串:
root------------------------------ONb8Jcr6LjLhUq1F8BzMuw==
123456-------------------------A/XAJC73+y+8ayiN4owwWrNdlLqM599j
再将原有的用户名和密码用加密后的字符串替代就好了,记得加ENC()解密方式
jdbc.username=ENC(ONb8Jcr6LjLhUq1F8BzMuw==) jdbc.password=ENC(A/XAJC73+y+8ayiN4owwWrNdlLqM599j)
5.启动连接大功告成!!!!!!!!!!!!!!!!!!!!!