当前位置:网站首页>Micro combat | centralized configuration service center Config asymmetric encryption and security management
Micro combat | centralized configuration service center Config asymmetric encryption and security management
2022-07-29 15:05:00 【Rain _ time to cook】
前言
上一篇:微服务实战|集中配置组件Config规避敏感信息泄露
上一篇文章中,我们介绍了使用集中配置中心组件ConfigImplemented symmetric encryption algorithm Implemented encryption and decryption of configuration information,This article will introduce the use of asymmetric encryption algorithms to achieve the same function and the security management of the configuration center.
一、ConfigAsymmetric encryption in action
1、生成秘钥文件
使用 JDK In their own digital certificate management toolskeytool .进入jdk安装目录下的bin目录,Execute the following command to generate the key file:
D:\soft\Java\jdk1.8.0_181\bin>keytool -genkeypair -alias config-server -keyalg RSA -dname "CN=cn,OU=cn,O=cn,L=cn,S=cn,C=cn" -keypass 123456 -keystroe d:/config-server.jks -storepass 123456
将生成的config-server.jks文件copy到config-server项目的resource目录下面.
2、配置秘钥
继续修改我们的config-server项目,将bootstrap.yml文件内容修改如下:
#Symmetric encryption configuration
#encrypt:
# key: 123456
#Asymmetric encryption configuration
encrypt:
key-store:
location: config-server.jks
alias: config-server
password: 123456
secret: 123456
3、启动验证
启动config-server项目,验证一下加密解密是否正常:
同样访问http://localhost:8005/encrypt/status,返回ok

再来访问http://localhost:8081/encrypt 加密接口:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7yY7TB6s-1659000221062)(D:\document\cxy\art\09-1.png)]](/img/eb/35d589c8ca2d5c017bc8e9b7dfd4a2.png)
Also everything works,能够正常加密和解密.
4、项目实战
加密和解密功能正常之后,就要修改我们的配置文件内容了.
修改我们的config-server项目,在repo文件夹下,修改客户端的配置文件configclient-dev.yml,修改内容如下:
#Symmetrically encrypted ciphertext
#app:
# key: '{cipher}5ab3ce1502c40c276074f400aee0be0f6279d6a85bb9d8d315a78c7a91603dde58d4512a6bc9f6492a8eddd34dbeeac0'
#Asymmetric encryption ciphertext
app:
key: '{cipher}AQBFywM+SuemUIS18U8wXrw96hxmhq26u4UUKCxQ1jLTLEcT7MEldJ9C7H4USE+bxVIlHernOewcfWQBg8SwSqK9MuLr07Z1bTEwPEu8KfbEMKTXJBaK6OOVLPFKjaUS/ezHKlke1DYjPpxuo9QO5GOTXIGZOBIlcclJbBCAc2/JIEK34Na/vJdbmSVtNdo6qLw1ufDNPES5q7pweWrnaP4vmtzV8JSs7+UAOV4caf8Zxrv7Sp6KuxuRlEn2yNPoi9bzf4pBWoqxZ5cUsEkzMb5ZEEkmqXCXT4o6+AWdantXbzUXCVculh0FGHvJnnvzsDjoKnVJm0JULjNa5yWNik42tdqsj4KlW76XWcBNv4ditrNsYz13IhZpwsZGHtqTZ97AwobvaTY/UrV1We5ssx3O'
注意密文前面增加{cipher},这样才能正常解密.
5、验证
最后,再次重启registry项目以及config-server,config-client两个服务,访问client端的hello接口:http://localhost:8007/hello,Also returned to normal plaintext encrypted:

二、Configuration Center Security Management
just now we noticed,visit between us as:http://localhost:8005/encrypt/status、http://localhost:8081/encrypt 等状态,加密、The decryption interface is accessible between.Even we can access the configuration center to get the interface address of the configuration file:http://localhost:8005/configclient/dev:

returned by the browserjsonWhy is the message formatted directly??Just install this plugin:Chrome浏览器插件:FeHelper
This is also a security risk!We also need to do further security management:
1、引入依赖
继续修改我们的config-server项目的pom文件,增加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加完成之后,重启 config_server 项目,Note that the following log will be printed in the background when restarting:
Using generated security password: 8dc1da66-b6ea-4aa7-a67d-3d02f97e179c
Then the browser accesses the address again: http://localhost:8005/configclient/dev,就会跳转到登录页面,without directly returning configuration information,The default username for the login information isuser,And the password is the random password generated in the log printed above,We can also specify username and password:
2、Increase safety management configuration
修改config-server项目中的application.yml配置文件:
server:
port: 8005
spring:
application:
name: config-server
profiles:
active: native #本地文件
cloud:
config:
server:
native:
search-locations: classpath:/repo
security: #Increase safety management configuration
user:
name: cxy965
roles: admin
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
这样,You can log in with the username and password we specified.
3、Adjust client calls
Increase safety management configuration,我们重新启动config-client项目,就会发现,Unable to pull configuration from configuration center:
2022-07-28 17:09:03.416 WARN 29296 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.cloud.config.environment.Environment] and content type [text/html;charset=UTF-8]
2022-07-28 17:09:03.418 INFO 29296 --- [ main] com.cxy965.demo.ConfigClientApplication : No active profile set, falling back to default profiles: default
Means through our configuration information,Unable to get available configuration resources,It is because the configuration center adds security management,The client has no right to access,这时,Just add the user name and password for access on the client side..
修改config-client项目的bootstrap.yml,如下:
spring:
application:
name: configclient
cloud:
config:
username: cxy965 #Increased access to configuration center user name information
password: 123456 #Add password information for accessing configuration center
name: configclient
profile: dev
discovery:
enabled: true
service‐id: config-server
eureka:
client:
service‐url:
defaultZone: http://localhost:8001/eureka/
再重新启动就可以了!
边栏推荐
猜你喜欢

第4章_2——视图的使用

【yolov7系列二】正负样本分配策略

<老式喜剧>

Instant Messaging - New Software that Changes Social and Work Status

ArcGIS Molder Builder模型构建器基本知识

C语言 5:bool类型,关系表达式,逻辑表达式,分支语句,函数调用机制,break,continue,goto,return/exit跳转语句

双非渣渣的上岸之路!备战60天,三战滴滴侥幸收获Offer

WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1

Linux installation of MySQL (super detailed)

基于JSP&Servlet实现的众筹平台系统
随机推荐
QT通过UDP分包传输大图像(测试可传6M)
深陷盈利困境,“寒冬”中也要二次递表,北森上市迫切
这个 MySQL bug,99% 的人会踩坑!
如何获取本地json
xss内容总结
《外太空的莫扎特》
这 6 款在线 PDF 转换工具,得试
C语言 4:汇编语言指令介绍
Google Cloud X Kyligence|如何从业务视角管理数据湖?
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
这 6 款在线 PDF 转换工具,得试试
【LeetCode】121. 买卖股票的最佳时机
About inner classes
【表达式计算】表达式计算问题的通用解法(练习加强版,含总结)
嵌入式开发经验分享,把学习当作一种兴趣
软件测试架构师的工作日常
Why do strings use the final keyword
【 LeetCode 】 121. The best time to buy stocks
uni 的下拉式筛选菜单的功能/图片懒加载
WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1