SpringBoot项目整合jasypt实现过程详解
依赖引入pom.xml
com.github.ulisesbocchio jasypt-spring-boot-starter 2.1.1 com.github.ulisesbocchio jasypt-maven-plugin 3.0.3
配置参数application.properties
jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)
测试代码
packagecom.yang.ftpdemo.controller; importlombok.Data; importorg.jasypt.encryption.StringEncryptor; importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importjavax.annotation.Resource; @RestController @RequestMapping("/crypt") publicclassCryptController{ @Resource privateStringEncryptorencrypt; @Resource privateCryptConfigcryptConfig; @GetMapping("/encrypt") publicCryptConfigencrypt(){ Stringusername=encrypt.encrypt("root"); Stringpassword=encrypt.encrypt("root123"); CryptConfigcrypt=newCryptConfig(); crypt.setPassword(password); crypt.setUserName(username); returncrypt; } @GetMapping("/decrypt") publicCryptConfigdecrypt(){ CryptConfigcrypt=newCryptConfig(); BeanUtils.copyProperties(this.cryptConfig,crypt); returncrypt; } } @Data @Configuration @ConfigurationProperties(prefix="crypt") classCryptConfig{ privateStringuserName; privateStringpassword; }
测试
浏览器访问【http://localhost:8080/crypt/encrypt】得到加密结果,并且每次请求结果不一样:
{ "userName":"XsWOwhZIag8XBh3DFl4sqA==", "password":"3kD2/u+xnM1i5mO2cVMWKw==" }
浏览器访问【http://localhost:8080/crypt/decrypt】得到解密结果,每次请求结果一样:
{ "userName":"root", "password":"root123" }
##Maven插件用法不可用,后面补充吧......
mvnjasypt:encrypt-Djasypt.encryptor.password="myPass"
可能会报如下错误,表示本地环境未安装JDK的JCE模块,它是JDK提供的加密扩展,需要到Oracle官网手动下载,并安装:
下载地址【https://www.oracle.com/technetwork/cn/java/javase/downloads/jce8-download-2133166-zhs.html】
[ERROR]Failedtoexecutegoalcom.github.ulisesbocchio:jasypt-maven-plugin:3.0.3:encrypt(default-cli)onprojectftp-demo:ErrorEncrypting:Encryptionraisedanexception.Apossiblecauseisyouareusingstrongencryptionalgorithmsandyouhavenotinstalled
theJavaCryptographyExtension(JCE)UnlimitedStrengthJurisdictionPolicyFilesinthisJavaVirtualMachine->[Help1]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。