-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OAuth2 auto configuration support for Eureka Client. #2563
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
<spring-cloud-commons.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-commons.version> | ||
<spring-cloud-config.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-config.version> | ||
<spring-cloud-stream.version>Elmhurst.BUILD-SNAPSHOT</spring-cloud-stream.version> | ||
<spring-security-oauth2.version>2.2.1.RELEASE</spring-security-oauth2.version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't managed by boot? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still a WIP : spring-attic/spring-security-oauth#1240 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if there are plans to make this managed on boot. @rwinch any opinion on that? I don't need autoconfiguration, just the dependencies. We are talking about boot 2.0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daniellavoie Thanks for reaching out! There are no plans for the old OAuth project version to be managed by Spring Boot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had a chat with Rob, best option is to migrate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it won't be as easy. Spring Security 5 doesn't support automatic token retrieval for resource servers. The old
I think the most reasonable option is to wait for Security 5.1 @ryanjbaxter @spencergibb Any opinion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see any reason why we need this PR right now so I dont see why we cant wait. |
||
<!-- Has to be a stable version (not one that depends on this version of netflix): --> | ||
<donotreplacespring-cloud-contract.version>1.2.0.RELEASE</donotreplacespring-cloud-contract.version> | ||
|
||
|
@@ -119,6 +120,11 @@ | |
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security.oauth</groupId> | ||
<artifactId>spring-security-oauth2</artifactId> | ||
<version>${spring-security-oauth2.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-codec-http</artifactId> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.springframework.cloud.netflix.eureka.config; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.netflix.eureka.http.EurekaRestTemplateFactory; | ||
import org.springframework.cloud.netflix.eureka.http.oauth2.EurekaOAuth2ResourceDetails; | ||
import org.springframework.cloud.netflix.eureka.http.oauth2.OAuth2EurekaRestTemplateFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails; | ||
|
||
@Configuration | ||
@ConditionalOnClass(BaseOAuth2ProtectedResourceDetails.class) | ||
public class EurekaOAuth2AutoConfiguration { | ||
@Bean | ||
@ConditionalOnProperty("eureka.client.oauth2.client-id") | ||
public EurekaOAuth2ResourceDetails eurekaOAuth2ResourceDetails() { | ||
return new EurekaOAuth2ResourceDetails(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnBean(EurekaOAuth2ResourceDetails.class) | ||
public EurekaRestTemplateFactory eurekaRestTemplateFactory( | ||
EurekaOAuth2ResourceDetails eurekaOAuth2ResourceDetails) { | ||
return new OAuth2EurekaRestTemplateFactory(eurekaOAuth2ResourceDetails); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.eureka.http; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.client.support.BasicAuthorizationInterceptor; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @author Daniel Lavoie | ||
*/ | ||
public class BasicEurekaRestTemplateFactory implements EurekaRestTemplateFactory { | ||
@Override | ||
public RestTemplate newRestTemplate(String serviceUrl) { | ||
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); | ||
try { | ||
URI serviceURI = new URI(serviceUrl); | ||
if (serviceURI.getUserInfo() != null) { | ||
String[] credentials = serviceURI.getUserInfo().split(":"); | ||
if (credentials.length == 2) { | ||
restTemplateBuilder.interceptors(new BasicAuthorizationInterceptor( | ||
credentials[0], credentials[1])); | ||
} | ||
} | ||
} | ||
catch (URISyntaxException ignore) { | ||
|
||
} | ||
|
||
return restTemplateBuilder.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.eureka.http; | ||
|
||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @author Daniel Lavoie | ||
*/ | ||
public interface EurekaRestTemplateFactory { | ||
RestTemplate newRestTemplate(String serviceUrl); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention this is for jersey specifically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well not exactly true, if you use the rest template you can still extend the
DiscoveryClientOptionalArgs
(that's what we do on SCS)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's right