-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Support @ClientRegistrationId at Class Level #17838
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...est/java/org/springframework/security/docs/features/integrations/rest/type/Hovercard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2004-present 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 | ||
* | ||
* https://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.security.docs.features.integrations.rest.type; | ||
|
||
/** | ||
* Used to ensure {@link UserService} compiles, but not show in the documentation. | ||
* | ||
* @author Rob Winch | ||
*/ | ||
public record Hovercard() { | ||
} |
41 changes: 41 additions & 0 deletions
41
...t/java/org/springframework/security/docs/features/integrations/rest/type/UserService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2004-present 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 clients copy of the License at | ||
* | ||
* https://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.security.docs.features.integrations.rest.type; | ||
|
||
import org.springframework.security.docs.features.integrations.rest.clientregistrationid.User; | ||
import org.springframework.security.oauth2.client.annotation.ClientRegistrationId; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.service.annotation.GetExchange; | ||
import org.springframework.web.service.annotation.HttpExchange; | ||
|
||
/** | ||
* Demonstrates a service for {@link ClientRegistrationId} at the type level. | ||
* @author Rob Winch | ||
*/ | ||
// tag::type[] | ||
@HttpExchange | ||
@ClientRegistrationId("github") | ||
public interface UserService { | ||
|
||
@GetExchange("/user") | ||
User getAuthenticatedUser(); | ||
|
||
@GetExchange("/users/{username}/hovercard") | ||
Hovercard getHovercard(@PathVariable String username); | ||
|
||
} | ||
// end::type[] |
24 changes: 24 additions & 0 deletions
24
.../kotlin/org/springframework/security/kt/docs/features/integrations/rest/type/Hovercard.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2004-present 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 | ||
* | ||
* https://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.security.kt.docs.features.integrations.rest.type | ||
|
||
/** | ||
* Used to ensure [UserService] compiles, but not show in the documentation. | ||
* | ||
* @author Rob Winch | ||
*/ | ||
class Hovercard |
39 changes: 39 additions & 0 deletions
39
...otlin/org/springframework/security/kt/docs/features/integrations/rest/type/UserService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2004-present 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 clients copy of the License at | ||
* | ||
* https://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.security.kt.docs.features.integrations.rest.type | ||
|
||
import org.springframework.security.kt.docs.features.integrations.rest.clientregistrationid.User | ||
import org.springframework.security.oauth2.client.annotation.ClientRegistrationId | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.service.annotation.GetExchange | ||
import org.springframework.web.service.annotation.HttpExchange | ||
|
||
/** | ||
* Demonstrates a service for [ClientRegistrationId] at the type level. | ||
* @author Rob Winch | ||
*/ | ||
// tag::type[] | ||
@HttpExchange | ||
@ClientRegistrationId("github") | ||
interface UserService { | ||
@GetExchange("/user") | ||
fun getAuthenticatedUser(): User | ||
|
||
@GetExchange("/users/{username}/hovercard") | ||
fun getHovercard(@PathVariable username: String): Hovercard | ||
} | ||
// end::type[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,14 @@ | |
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.annotation.AnnotationConfigurationException; | ||
import org.springframework.security.oauth2.client.annotation.ClientRegistrationId; | ||
import org.springframework.security.oauth2.client.web.ClientAttributes; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.web.service.invoker.HttpRequestValues; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
/** | ||
* Unit tests for {@link ClientRegistrationIdProcessor}. | ||
|
@@ -39,6 +41,8 @@ | |
*/ | ||
class ClientRegistrationIdProcessorTests { | ||
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. Add tests that verify failure in the event that there are duplicate annotations. This should be handled by the
|
||
|
||
private static final String REGISTRATION_ID = "registrationId"; | ||
|
||
ClientRegistrationIdProcessor processor = ClientRegistrationIdProcessor.DEFAULT_INSTANCE; | ||
|
||
@Test | ||
|
@@ -48,7 +52,7 @@ void processWhenClientRegistrationIdPresentThenSet() { | |
this.processor.process(hasClientRegistrationId, null, null, builder); | ||
|
||
String registrationId = ClientAttributes.resolveClientRegistrationId(builder.build().getAttributes()); | ||
assertThat(registrationId).isEqualTo(RestService.REGISTRATION_ID); | ||
assertThat(registrationId).isEqualTo(REGISTRATION_ID); | ||
} | ||
|
||
@Test | ||
|
@@ -58,7 +62,7 @@ void processWhenMetaClientRegistrationIdPresentThenSet() { | |
this.processor.process(hasClientRegistrationId, null, null, builder); | ||
|
||
String registrationId = ClientAttributes.resolveClientRegistrationId(builder.build().getAttributes()); | ||
assertThat(registrationId).isEqualTo(RestService.REGISTRATION_ID); | ||
assertThat(registrationId).isEqualTo(REGISTRATION_ID); | ||
} | ||
|
||
@Test | ||
|
@@ -71,9 +75,28 @@ void processWhenNoClientRegistrationIdPresentThenNull() { | |
assertThat(registrationId).isNull(); | ||
} | ||
|
||
interface RestService { | ||
@Test | ||
void processWhenClientRegistrationIdPresentOnDeclaringClassThenSet() { | ||
HttpRequestValues.Builder builder = HttpRequestValues.builder(); | ||
Method declaringClassHasClientRegistrationId = ReflectionUtils.findMethod(TypeAnnotatedRestService.class, | ||
"declaringClassHasClientRegistrationId"); | ||
this.processor.process(declaringClassHasClientRegistrationId, null, null, builder); | ||
|
||
String registrationId = ClientAttributes.resolveClientRegistrationId(builder.build().getAttributes()); | ||
assertThat(registrationId).isEqualTo(REGISTRATION_ID); | ||
} | ||
|
||
String REGISTRATION_ID = "registrationId"; | ||
@Test | ||
void processWhenDuplicateClientRegistrationIdPresentOnAggregateServiceThenException() { | ||
HttpRequestValues.Builder builder = HttpRequestValues.builder(); | ||
Method shouldFailDueToDuplicateClientRegistrationId = ReflectionUtils.findMethod(AggregateRestService.class, | ||
"shouldFailDueToDuplicateClientRegistrationId"); | ||
|
||
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( | ||
() -> this.processor.process(shouldFailDueToDuplicateClientRegistrationId, null, null, builder)); | ||
} | ||
|
||
interface RestService { | ||
|
||
@ClientRegistrationId(REGISTRATION_ID) | ||
void hasClientRegistrationId(); | ||
|
@@ -86,9 +109,32 @@ interface RestService { | |
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@ClientRegistrationId(RestService.REGISTRATION_ID) | ||
@ClientRegistrationId(REGISTRATION_ID) | ||
@interface MetaClientRegistrationId { | ||
|
||
} | ||
|
||
@ClientRegistrationId(REGISTRATION_ID) | ||
interface TypeAnnotatedRestService { | ||
|
||
void declaringClassHasClientRegistrationId(); | ||
|
||
} | ||
|
||
@ClientRegistrationId("a") | ||
interface ARestService { | ||
|
||
} | ||
|
||
@ClientRegistrationId("b") | ||
interface BRestService { | ||
|
||
} | ||
|
||
interface AggregateRestService extends ARestService, BRestService { | ||
|
||
void shouldFailDueToDuplicateClientRegistrationId(); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use a
SecurityAnnotationScanner<ClientRegistrationId>
instead. This will automatically follow the same logic as we do for method security. It ensures that we do not get duplicate annotations which might provide conflicting information and thus use the wrong registration id.