SpringCloud之分布式配置中心-本地配置文件(重启生效) |第十一章 -yellowcong

本文介绍如何在 Spring Cloud 配置中心中管理权限验证。包括两种方式:关闭权限验证,允许无需认证访问;开启权限验证,需提供用户名和密码才能访问资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要通过发送post请求http://yellowcong.com:8763/refreh设置刷新配置文件信息,会遇到授权问题Full authentication is required to access this resource.,解决办法有两种:第一种、配置配置文件客户端bootstrap.yml ,设置management.security.enabled=false ,关闭验证。第二种、开启权限验证,设定授权用户信息。

代码地址

https://gitee.com/yellowcong/springcloud/tree/master/chapter11

目录结构

这里写图片描述

项目架构

对于8763 节点,可以在这个服务的客户端刷新节点信息,不需要输入密码,而对于8764节点,访问,就需要输入用户名和密码信息。

节点服务项目名启动顺序
yellowcong.com:8761eureka注册服务eureka-server1
yellowcong.com:8762配置服务端config-server2
yellowcong.com:8763配置客户端(关闭验证)config-client3
yellowcong.com:8764配置客户端(开启验证)config-client23

第一节:关闭权限验证

配置bootstrap.yml

设置management.security.enabled=false ,关闭验证 ,然后重启服务器,即可发现直接post提交,可以正常访问。

#配置服务名称
#访问的配置文件名称为
#{application.name}/{profile}/{label}
#${spring.application.name}-${spring.cloud.config.profile}-${spring.cloud.config.label}
spring:
  application:
    name: config-client #服务名称
  cloud:
    config:
      uri: http://yellowcong.com:8762 #配置服务的地址 ,这个没有生效
      enabled: true #开启配置
      profile: dev  #版本
      label: ""     #git配置的分支信息,master类似的
#取消权限验证
management:
  security:
    enabled: false

2、测试访问

通过发送get请求到http://yellowcong.com:8763/refreh设置刷新配置文件信息,报错,说不允许get请求,然后通过post请求发送,请求正常返回。
这里写图片描述

第二节、开启授权验证

开启授权验证后,访问的时候,需要登录用户名和密码,才可以访问到资源信息。

1、配置pom.xml

添加开启权限的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、配置bootstrap.yml

配置用户的用户名和密码。

security.user.name=admin
security.user.password=123456
management.security.enabled=true
management.security.role=ADMIN

下面是完整的配置

#配置服务名称
#访问的配置文件名称为
#{application.name}/{profile}/{label}
#${spring.application.name}-${spring.cloud.config.profile}-${spring.cloud.config.label}
spring:
  application:
    name: config-client #服务名称
  cloud:
    config:
      uri: http://yellowcong.com:8762 #配置服务的地址 ,这个没有生效
      enabled: true #开启配置
      profile: dev  #版本
      label: ""     #git配置的分支信息,master类似的
#开启权限验证, 默认就是开启的
management:
  security:
    enabled: true
    role: ADMIN #角色信息

security:
  user:
    name: admin
    password: yellowcong

3、测试访问资源

访问资源的时候,需要输入上面配置的用户名和密码,才可访问。
这里写图片描述

参考文章

1、spring-boot-actuator报错Full authentication is required to access this resource

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值