提取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.v1clientName: 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
相关产品推荐
相关产品推荐

