You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提取Spring OAuth2 YML配置中的公共值

提取Spring OAuth2配置中的公共值

先把你提供的原始配置贴出来方便对比:

spring:
  security:
    oauth2:
      client:
        registration:
          evesso:
            client-id: xxxxxx
            client-secret: yyyyyy
            authorizationGrantType: authorization_code
            response_type: code
            redirectUriTemplate: http://localhost:8080/login/oauth2/code/
            scope: esi-location.read_location.v1
            clientName: EVE SSO
          evessostats:
            client-id: xxxxxx
            client-secret: yyyyyy
            authorizationGrantType: authorization_code
            response_type: code
            redirectUriTemplate: http://localhost:8080/login/oau...

梳理出的公共配置项

这两个客户端配置里的公共内容主要有这些:

  • 归属路径:所有配置都挂在 spring.security.oauth2.client.registration 这个父节点下
  • 授权模式相关:
    • 都采用授权码模式:authorizationGrantType: authorization_code
    • 响应类型统一为授权码:response_type: code
  • 重定向地址前缀:redirectUriTemplate 的开头都是 http://localhost:8080/login/oauth2/code/(第二个配置虽截断,但从开头能推断前缀一致)
  • 凭证配置项:两个客户端都包含 client-id 和 client-secret 这两个必填的凭证字段(仅具体值不同)

各客户端的独有配置

每个客户端的差异化内容也很明确:

  • evesso有专属的权限范围和客户端名称:
    • scope: esi-location.read_location.v1
    • clientName: EVE SSO
  • evessostats的配置被截断了,暂时看不到它的scope、clientName等独有项,但能确定它的凭证值和evesso不同

额外小技巧:用YAML锚点复用公共配置

如果想优化配置文件,避免重复代码,可以用YAML的锚点&和引用*来复用公共配置,示例如下:

spring:
  security:
    oauth2:
      client:
        registration:
          # 定义公共配置锚点
          common-oauth2-config: &commonOAuth2
            authorizationGrantType: authorization_code
            response_type: code
            redirectUriTemplate: http://localhost:8080/login/oauth2/code/
          evesso:
            <<: *commonOAuth2
            client-id: xxxxxx
            client-secret: yyyyyy
            scope: esi-location.read_location.v1
            clientName: EVE SSO
          evessostats:
            <<: *commonOAuth2
            client-id: xxxxxx
            client-secret: yyyyyy
            # 补充该客户端独有的配置字段

内容的提问来源于stack exchange,提问作者Trynkiewicz Mariusz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:48:27