4

Spring Cloud之Finchley版学习(二十一)-Spring Cloud Config-配置属性加解密

 3 years ago
source link: https://www.wencst.com/archives/1443
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Cloud之Finchley版学习(二十一)-Spring Cloud Config-配置属性加解密

作者: wencst 分类: JAVA,微服务,架构设计 发布时间: 2019-01-31 10:05 阅读: 2,020 次

前文都是将配置明文存储在Git仓库中,但在实际项目中,敏感的配置属性(例如数据库账号、密码等),都应加密存储,从而提高安全性。

Config Server为配置内容的加密与解密提供了支持。

安装JCE

加解密端点

  • 加密:curl $CONFIG_URL/encrypt -d 想要加密的内容
  • 解密:curl $CONFIG_URL/decrypt -d 想要解密的密文
  • Config Server的bootstrap.yml中添加:
encrypt:
  key: foo  # 设置对称密钥
  • 以yaml格式存储:
    spring:
      datasource:
        username: dbuser
        password: '{cipher}851a6effab6619f43157a714061f4602be0131b73b56b0451a7e268c880daea3'
  • 以properties格式存储:
    spring.datasource.username=dbuser
    spring.datasource.password={cipher}851a6effab6619f43157a714061f4602be0131b73b56b0451a7e268c880daea3
  • 输入命令
    curl http://localhost:8080/encrypt -d mysecret
  • 返回851a6effab6619f43157a714061f4602be0131b73b56b0451a7e268c880daea3 。说明mysecret 被加密了。
  • 输入命令
    curl http://localhost:8080/decrypt -d 851a6effab6619f43157a714061f4602be0131b73b56b0451a7e268c880daea3
  • 可返回mysecret ,说明能够正常解密。

非对称加密

  • 执行以下命令,并按照提示操作,即可创建一个Key Store。
    keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore server.jks -storepass letmein
  • 将生成的server.jks文件复制到项目的classpath下。
  • 在bootstrap.yml中添加以下内容。
encrypt:
  keyStore:
    location: classpath:/server.jks # jks文件的路径
    password: letmein               # storepass
    alias: mytestkey                # alias
    secret: changeme                # keypass

这样,使用命令

curl http://localhost:8080/encrypt -d mysecret

尝试加密时 ,就会得到类似以下的结果。

AQB38UyNckYzW64rvsaIhy0OV4MUmS7krdHrw+VLUdqXJ4ZVdZL8/ouwSOAYM+6MSjKvzmkaU8Iv2cQ5MWhlZhCrm0f0d2ubc1MH96KBHTix9AroajeTiofPwPoBnWfBo9cC4PU1vD+rcvAvwvdR5q7rYbFc4yut4uJZRzpAXGgf680kAtb6tEtLx7c4/35PEaGXFWd2m8gn21vzWdvhbP6cdC9YlburL0Rq/0H1G+uEX99ZVIWJ0hVn4rplLWPMLUGA2ZVEyVRorIRX/2z5MU7cVPtJ6X1JZDpU4GVz8/3rD5BnbVFTGo6DfBrEzJn58Bzjl6aqo9ca/3j42RHOoQDOHXGqRX/843RbPdvMqTZd0rTOBHTUrVG9E15sCajiLkw=

相对于对称加密,非对称加密的安全性更高,但对称加密相对方便。读者可按照需求,自行选择加密方案。

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)

Leave a Reply Cancel reply

You must be logged in to post a comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK