-
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
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.springframework.security.oauth2.client.web.client; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
|
@@ -37,17 +38,20 @@ public final class ClientRegistrationIdProcessor implements HttpRequestValues.Pr | |
|
||
public static ClientRegistrationIdProcessor DEFAULT_INSTANCE = new ClientRegistrationIdProcessor(); | ||
|
||
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.
Suggested change
private SecurityAnnotationScanner<ClientRegistrationId> scanner = SecurityAnnotationScanners.requireUnique(ClientRegistrationId.class);
Use a |
||
private ClientRegistrationIdProcessor() { | ||
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. Please don't make changes that are unrelated to the PR |
||
} | ||
|
||
@Override | ||
public void process(Method method, MethodParameter[] parameters, @Nullable Object[] arguments, | ||
HttpRequestValues.Builder builder) { | ||
ClientRegistrationId registeredId = AnnotationUtils.findAnnotation(method, ClientRegistrationId.class); | ||
ClientRegistrationId registeredId = Optional | ||
.ofNullable(AnnotationUtils.findAnnotation(method, ClientRegistrationId.class)) | ||
.orElseGet(() -> AnnotationUtils.findAnnotation(method.getDeclaringClass(), ClientRegistrationId.class)); | ||
|
||
Comment on lines
+47
to
+50
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.
Suggested change
ClientRegistrationId registeredId = Optional
.ofNullable(AnnotationUtils.findAnnotation(method, ClientRegistrationId.class))
.orElseGet(() -> AnnotationUtils.findAnnotation(method.getDeclaringClass(), ClientRegistrationId.class));
ClientRegistrationId registeredId = this.scanner.scan(method, method.getDeclaringClass());
|
||
if (registeredId != null) { | ||
String registrationId = registeredId.registrationId(); | ||
builder.configureAttributes(ClientAttributes.clientRegistrationId(registrationId)); | ||
} | ||
} | ||
|
||
private ClientRegistrationIdProcessor() { | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,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,32 +50,42 @@ 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 | ||
void processWhenMetaClientRegistrationIdPresentThenSet() { | ||
HttpRequestValues.Builder builder = HttpRequestValues.builder(); | ||
Method hasClientRegistrationId = ReflectionUtils.findMethod(RestService.class, "hasMetaClientRegistrationId"); | ||
this.processor.process(hasClientRegistrationId, null, null, builder); | ||
Method hasMetaClientRegistrationId = ReflectionUtils.findMethod(RestService.class, | ||
"hasMetaClientRegistrationId"); | ||
this.processor.process(hasMetaClientRegistrationId, null, null, builder); | ||
|
||
String registrationId = ClientAttributes.resolveClientRegistrationId(builder.build().getAttributes()); | ||
assertThat(registrationId).isEqualTo(RestService.REGISTRATION_ID); | ||
assertThat(registrationId).isEqualTo(REGISTRATION_ID); | ||
} | ||
|
||
@Test | ||
void processWhenNoClientRegistrationIdPresentThenNull() { | ||
HttpRequestValues.Builder builder = HttpRequestValues.builder(); | ||
Method hasClientRegistrationId = ReflectionUtils.findMethod(RestService.class, "noClientRegistrationId"); | ||
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. While these changes are valuable, please stick to the changes related to this ticket. This helps us to track issues and ensure that, when applicable (e.g. a bug fix that is applicable to older supported branches), we can easily selectively apply changes to other branches. |
||
this.processor.process(hasClientRegistrationId, null, null, builder); | ||
Method noClientRegistrationId = ReflectionUtils.findMethod(RestService.class, "noClientRegistrationId"); | ||
this.processor.process(noClientRegistrationId, null, null, builder); | ||
|
||
String registrationId = ClientAttributes.resolveClientRegistrationId(builder.build().getAttributes()); | ||
assertThat(registrationId).isNull(); | ||
} | ||
|
||
interface RestService { | ||
@Test | ||
void processWhenClientRegistrationIdPresentOnDeclaringClassThenSet() { | ||
HttpRequestValues.Builder builder = HttpRequestValues.builder(); | ||
Method declaringClassHasClientRegistrationId = ReflectionUtils.findMethod(AnnotatedRestService.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"; | ||
interface RestService { | ||
|
||
@ClientRegistrationId(REGISTRATION_ID) | ||
void hasClientRegistrationId(); | ||
|
@@ -86,9 +98,16 @@ interface RestService { | |
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@ClientRegistrationId(RestService.REGISTRATION_ID) | ||
@ClientRegistrationId(REGISTRATION_ID) | ||
@interface MetaClientRegistrationId { | ||
|
||
} | ||
|
||
@ClientRegistrationId(REGISTRATION_ID) | ||
interface AnnotatedRestService { | ||
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.
Suggested change
interface AnnotatedRestService {
interface TypeAnnotatedRestService {
This is more clear since all of the interfaces are annotated. |
||
|
||
void declaringClassHasClientRegistrationId(); | ||
|
||
} | ||
|
||
} |