Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
bbudano wants to merge 1 commit into spring-projects:main
base: main
Choose a base branch
Loading
from bbudano:gh-17806
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @since 7.0
* @see org.springframework.security.oauth2.client.web.client.ClientRegistrationIdProcessor
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClientRegistrationId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,17 +38,20 @@ public final class ClientRegistrationIdProcessor implements HttpRequestValues.Pr

public static ClientRegistrationIdProcessor DEFAULT_INSTANCE = new ClientRegistrationIdProcessor();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private SecurityAnnotationScanner<ClientRegistrationId> scanner = SecurityAnnotationScanners.requireUnique(ClientRegistrationId.class);

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.

private ClientRegistrationIdProcessor() {
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Up @@ -39,6 +39,8 @@
*/
class ClientRegistrationIdProcessorTests {
Copy link
Member

Choose a reason for hiding this comment

The 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 SecurityAnnotationScanners.requireUnique. An example would be AggregateService.toTest() should fail due to duplicate annotations at the same level.

@ClientRegistrationId("a")
interface AService {}
@ClientRegistrationId("b")
interface BService {}
interface AggregateService extends AService, BService {
	void toTest();
}


private static final String REGISTRATION_ID = "registrationId";

ClientRegistrationIdProcessor processor = ClientRegistrationIdProcessor.DEFAULT_INSTANCE;

@Test
Expand All @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The 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();
Expand All @@ -86,9 +98,16 @@ interface RestService {
}

@Retention(RetentionPolicy.RUNTIME)
@ClientRegistrationId(RestService.REGISTRATION_ID)
@ClientRegistrationId(REGISTRATION_ID)
@interface MetaClientRegistrationId {

}

@ClientRegistrationId(REGISTRATION_ID)
interface AnnotatedRestService {
Copy link
Member

Choose a reason for hiding this comment

The 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();

}

}

AltStyle によって変換されたページ (->オリジナル) /