Spring Cloud 2020之后版本针对nacos账号密码加密
    		       		warning:
    		            这篇文章距离上次修改已过420天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Cloud 2020.0.x之后的版本中,Nacos配置中心和服务注册中心支持账号密码的加密。以下是如何进行配置以启用账号密码加密的步骤:
- 添加依赖: - 在Spring Cloud项目的 - pom.xml中添加以下依赖:- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
- 配置加密: - 在 - bootstrap.properties或- bootstrap.yml中,配置Nacos的服务器地址、加密的用户名和密码:- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.username=${NACOS_USERNAME:nacos} spring.cloud.nacos.discovery.password=${NACOS_PASSWORD:nacos}- 使用 - ${NACOS_USERNAME:nacos}和- ${NACOS_PASSWORD:nacos}来指定默认用户名和密码,这些值会在应用启动时被替换。
- 启用密码加密: - 在 - application.properties或- application.yml中,启用Nacos的密码加密特性:- spring.cloud.nacos.config.username=nacos spring.cloud.nacos.config.password-encryptor-class-name=org.springframework.cloud.bootstrap.encrypt.EnvironmentEncryptor- 这里 - spring.cloud.nacos.config.password-encryptor-class-name指定了加密类,它会使用- EnvironmentEncryptor来处理密码加密。
- 加密密码: - 在启动Spring Cloud应用之前,需要对配置中心或服务注册中心的密码进行加密。可以通过以下命令行工具进行加密: - java -cp 'spring-cloud-context-2.2.5.RELEASE.jar;spring-cloud-starter-bootstrap-2.2.5.RELEASE.jar;spring-cloud-starter-encrypt-2.2.5.RELEASE.jar' \ org.springframework.cloud.bootstrap.encrypt.EnvironmentEncryptor \ [YOUR_ENCRYPT_KEY]- 将 - [YOUR_ENCRYPT_KEY]替换为您的密钥,它会输出加密后的密码,然后将其替换到配置中。
请注意,上述版本号和依赖可能会随着Spring Cloud的更新而变化,请根据您实际使用的版本进行相应的调整。
评论已关闭