From 7543effe8995f8919e3e3c10683c1004351dc595 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Tue, 21 Jun 2022 17:08:18 -0600 Subject: [PATCH] Add SecurityContextHolderStrategy Java Configuration for OAuth2 Issue gh-11061 --- .../OAuth2ClientConfiguration.java | 17 +++++++-- .../oauth2/client/OAuth2ClientConfigurer.java | 3 +- .../OAuth2ResourceServerConfigurer.java | 3 +- .../client/OAuth2LoginConfigurerTests.java | 25 +++++++++++++ .../OAuth2ResourceServerConfigurerTests.java | 36 +++++++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java b/config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java index b48c565a51..4be0a20ebc 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -23,6 +23,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.security.core.context.SecurityContextHolderStrategy; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; @@ -75,11 +76,18 @@ final class OAuth2ClientConfiguration { private OAuth2AuthorizedClientManager authorizedClientManager; + private SecurityContextHolderStrategy securityContextHolderStrategy; + @Override public void addArgumentResolvers(List argumentResolvers) { OAuth2AuthorizedClientManager authorizedClientManager = getAuthorizedClientManager(); if (authorizedClientManager != null) { - argumentResolvers.add(new OAuth2AuthorizedClientArgumentResolver(authorizedClientManager)); + OAuth2AuthorizedClientArgumentResolver resolver = new OAuth2AuthorizedClientArgumentResolver( + authorizedClientManager); + if (this.securityContextHolderStrategy != null) { + resolver.setSecurityContextHolderStrategy(this.securityContextHolderStrategy); + } + argumentResolvers.add(resolver); } } @@ -110,6 +118,11 @@ final class OAuth2ClientConfiguration { } } + @Autowired(required = false) + void setSecurityContextHolderStrategy(SecurityContextHolderStrategy strategy) { + this.securityContextHolderStrategy = strategy; + } + private OAuth2AuthorizedClientManager getAuthorizedClientManager() { if (this.authorizedClientManager != null) { return this.authorizedClientManager; diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurer.java b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurer.java index c81d7b07f8..b583b4b5b7 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurer.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -289,6 +289,7 @@ public final class OAuth2ClientConfigurer> if (this.authorizationRequestRepository != null) { authorizationCodeGrantFilter.setAuthorizationRequestRepository(this.authorizationRequestRepository); } + authorizationCodeGrantFilter.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy()); RequestCache requestCache = builder.getSharedObject(RequestCache.class); if (requestCache != null) { authorizationCodeGrantFilter.setRequestCache(requestCache); diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java index 128174c644..d1cee54096 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -265,6 +265,7 @@ public final class OAuth2ResourceServerConfigurer T verifyBean(Class beanClass, VerificationMode mode) { + return verify(this.spring.getContext().getBean(beanClass), mode); + } + private String json(String name) throws IOException { return resource(name + ".json"); }