Skip to content

Commit a362572

Browse files
authored
Support dash-case for oauth2.clientRegistrationId. Fixes gh-1270. (#1312)
- Create FeignOAuth2Properties with @ConfigurationProperties - Replace @value with FeignOAuth2Properties injection - Enable relaxed binding for OAuth2 configuration Signed-off-by: jaehun lee <jhl9838@naver.com>
1 parent 81a06b1 commit a362572

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@
100100
* @author Wojciech Mąka
101101
* @author Dangzhicairang(小水牛)
102102
* @author changjin wei(魏昌进)
103+
* @author jaehun lee
103104
*/
104105
@Configuration(proxyBeanMethods = false)
105106
@ConditionalOnClass(Feign.class)
106107
@EnableConfigurationProperties({ FeignClientProperties.class, FeignHttpClientProperties.class,
107-
FeignEncoderProperties.class })
108+
FeignEncoderProperties.class, FeignOAuth2Properties.class })
108109
public class FeignAutoConfiguration {
109110

110111
private static final Log LOG = LogFactory.getLog(FeignAutoConfiguration.class);
@@ -392,10 +393,10 @@ OAuth2AuthorizedClientManager feignOAuth2AuthorizedClientManager(
392393

393394
@Bean
394395
@ConditionalOnBean(OAuth2AuthorizedClientManager.class)
395-
public OAuth2AccessTokenInterceptor defaultOAuth2AccessTokenInterceptor(
396-
@Value("${spring.cloud.openfeign.oauth2.clientRegistrationId:}") String clientRegistrationId,
396+
public OAuth2AccessTokenInterceptor defaultOAuth2AccessTokenInterceptor(FeignOAuth2Properties oAuth2Properties,
397397
OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
398-
return new OAuth2AccessTokenInterceptor(clientRegistrationId, oAuth2AuthorizedClientManager);
398+
return new OAuth2AccessTokenInterceptor(oAuth2Properties.getClientRegistrationId(),
399+
oAuth2AuthorizedClientManager);
399400
}
400401

401402
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.openfeign;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for Feign OAuth2 support.
23+
*
24+
* @author jaehun lee
25+
*/
26+
@ConfigurationProperties(prefix = "spring.cloud.openfeign.oauth2")
27+
public class FeignOAuth2Properties {
28+
29+
/**
30+
* Enables feign interceptor for managing oauth2 access token.
31+
*/
32+
private boolean enabled = false;
33+
34+
/**
35+
* Client registration id to be used to retrieve the OAuth2 access token. If not
36+
* specified, the {@code serviceId} retrieved from the {@code url} host segment will
37+
* be used. This is convenient for load-balanced Feign clients. For non-load-balanced
38+
* clients, specifying the {@code clientRegistrationId} is recommended.
39+
*/
40+
private String clientRegistrationId = "";
41+
42+
public boolean isEnabled() {
43+
return enabled;
44+
}
45+
46+
public void setEnabled(boolean enabled) {
47+
this.enabled = enabled;
48+
}
49+
50+
public String getClientRegistrationId() {
51+
return clientRegistrationId;
52+
}
53+
54+
public void setClientRegistrationId(String clientRegistrationId) {
55+
this.clientRegistrationId = clientRegistrationId;
56+
}
57+
58+
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignAutoConfigurationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Kwangyong Kim
4343
* @author Wojciech Mąka
4444
* @author Dangzhicairang(小水牛)
45+
* @author jaehun lee
4546
*/
4647
class FeignAutoConfigurationTests {
4748

@@ -103,6 +104,19 @@ void shouldInstantiateFeignOAuth2FeignRequestInterceptorWithoutInterceptors() {
103104
});
104105
}
105106

107+
@Test
108+
void shouldInstantiateFeignOAuth2FeignRequestInterceptorWithDashCaseProperty() {
109+
runner
110+
.withPropertyValues("spring.cloud.openfeign.oauth2.enabled=true",
111+
"spring.cloud.openfeign.oauth2.client-registration-id=feign-client")
112+
.withBean(OAuth2AuthorizedClientService.class, () -> mock(OAuth2AuthorizedClientService.class))
113+
.withBean(ClientRegistrationRepository.class, () -> mock(ClientRegistrationRepository.class))
114+
.run(ctx -> {
115+
assertOauth2AccessTokenInterceptorExists(ctx);
116+
assertThatOauth2AccessTokenInterceptorHasSpecifiedIdsPropertyWithValue(ctx, "feign-client");
117+
});
118+
}
119+
106120
private void assertOauth2AccessTokenInterceptorExists(ConfigurableApplicationContext ctx) {
107121
AssertableApplicationContext context = AssertableApplicationContext.get(() -> ctx);
108122
assertThat(context).hasSingleBean(OAuth2AccessTokenInterceptor.class);

0 commit comments

Comments
 (0)