Spring Cloud Config Client 在 2026.0.0-RC1 里用 OAuth2 client_credentials 给 Config Server 请求带上 Bearer。关掉 spring.cloud.config.oauth2.enabled 时,同一地址返回 401,Client 以 fail-fast 退出。打开后 Client 日志出现 Located environment,GET /demo 读到远程 demo.message=hello-from-oauth2-config。
相对 Spring Cloud 2026.0.0-M1(Paddington):Gateway、Config 与依赖对齐变更,本文只补 RC1 增量。官方发布说明写明 Config Client 现支持 OAuth 2 访问 Config Server(#2348)。对照 jar:5.1.0-M1 的 spring-cloud-config-client 无 oauth2 包;5.1.0-RC1 含 ConfigClientOAuth2Properties、ConfigClientOAuth2Support 等类。
版本:JDK 21.0.12.1、Spring Boot 4.2.0-RC1、Spring Cloud BOM 2026.0.0-RC1、Config 5.1.0-RC1、Spring Authorization Server 7.2.0-RC1(随 Boot 引入)。依赖从 Spring milestone 仓库解析。
三个进程:Authorization Server(9000)、Config Server 作 JWT Resource Server(8888,native 仓库)、Config Client(8081)。issuer 与 token-uri 一律用 http://127.0.0.1:9000。
Client 侧关键配置:
spring.config.import=optional:configserver:http://127.0.0.1:8888
spring.cloud.config.fail-fast=true
spring.cloud.config.oauth2.enabled=true
spring.cloud.config.oauth2.client-registration-id=config-client
spring.security.oauth2.client.registration.config-client.client-id=config-client
spring.security.oauth2.client.registration.config-client.client-secret=config-secret
spring.security.oauth2.client.registration.config-client.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.config-client.scope=config.read
spring.security.oauth2.client.provider.config-client.token-uri=http://127.0.0.1:9000/oauth2/token
Config Server 用 oauth2ResourceServer().jwt() 保护全部端点,spring.security.oauth2.resourceserver.jwt.issuer-uri=http://127.0.0.1:9000。native 仓库里放了 demo.message 与 demo.source。
Authorization Server 启动日志:
2026-10-24T08:05:31.353+08:00 INFO 74715 --- [auth-server] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 9000 (http) with context path '/'
2026-10-24T08:05:31.361+08:00 INFO 74715 --- [auth-server] [ main] com.example.auth.AuthServerApplication : Started AuthServerApplication in 1.846 seconds (process running for 2.144)
对 /oauth2/token 用 client_secret_basic 申请 client_credentials,响应 HTTP/1.1 200,body 中 token_type=Bearer、scope=config.read、expires_in=299。
Config Server 启动:
2026-10-24T08:05:40.725+08:00 INFO 76174 --- [config-server] [ main] c.e.cfgserver.ConfigServerApplication : The following 1 profile is active: "native"
2026-10-24T08:05:42.206+08:00 INFO 76174 --- [config-server] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8888 (http) with context path '/'
2026-10-24T08:05:42.219+08:00 INFO 76174 --- [config-server] [ main] c.e.cfgserver.ConfigServerApplication : Started ConfigServerApplication in 1.807 seconds (process running for 2.199)
无 Token 访问 http://127.0.0.1:8888/application/default:
HTTP/1.1 401
WWW-Authenticate: Bearer resource_metadata="http://127.0.0.1:8888/.well-known/oauth-protected-resource"
带上刚拿到的 Bearer 后同一 URL 返回 200,JSON 里含 demo.message=hello-from-oauth2-config、demo.source=config-server-native。
Client 打开 OAuth2 后的启动片段:
2026-10-24T08:06:09.320+08:00 INFO 77240 --- [config-client] [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://127.0.0.1:8888
2026-10-24T08:06:09.320+08:00 INFO 77240 --- [config-client] [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Located environment: name=config-client, profiles=[default], label=null, version=null, state=null
2026-10-24T08:06:09.321+08:00 DEBUG 77240 --- [config-client] [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Environment config-client has 1 property sources with 2 properties.
2026-10-24T08:06:10.392+08:00 INFO 77240 --- [config-client] [ main] c.e.cfgclient.ConfigClientApplication : Started ConfigClientApplication in 2.047 seconds (process running for 2.394)
GET http://127.0.0.1:8081/demo 返回 hello-from-oauth2-config|config-server-native,HTTP 200。
同一 Client jar 加上 --spring.cloud.config.oauth2.enabled=false 与非 optional 的 configserver: import,进程退出码 1:
2026-10-24T08:06:23.217+08:00 INFO 78212 --- [config-client] [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://127.0.0.1:8888
org.springframework.cloud.config.client.ConfigClientFailFastException: Could not locate PropertySource and the fail fast property is set, failing
Caused by: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 on GET request for "http://127.0.0.1:8888/application/default": [no body]
源码侧(据 5.1.0-RC1 sources):ConfigServerConfigDataLocationResolver 在解析时调用 ConfigClientOAuth2Support.registerInterceptor;属性前缀 spring.cloud.config.oauth2,默认 enabled=false。grant 走 client_credentials,经 OAuth2ClientHttpRequestInterceptor 挂到 Config Client 的 RestTemplate。
发布说明还提到 Gateway MVC WebSocket(#3442)。本次环境按 #2348 跑通,未测 Gateway。
依据:Spring Cloud 2026.0.0-RC1 (Paddington) Has Been Released(HTTP 200)、issue #2348(HTTP 200)、milestone BOM 2026.0.0-RC1(HTTP 200)。
一起交流
分享你的思考,让讨论更进一步。