Skip to content

Commit 717529d

Browse files
committed
Add Generic Type to ObjectPostProcessor Lookups
Issue gh-15678
1 parent 8616044 commit 717529d

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import org.apache.commons.logging.LogFactory;
3535

3636
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
37+
import org.springframework.beans.factory.ObjectProvider;
3738
import org.springframework.context.ApplicationContext;
39+
import org.springframework.core.ResolvableType;
3840
import org.springframework.http.HttpMethod;
3941
import org.springframework.lang.Nullable;
4042
import org.springframework.security.config.annotation.ObjectPostProcessor;
@@ -113,7 +115,9 @@ public C anyRequest() {
113115
*/
114116
protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, String... mvcPatterns) {
115117
Assert.state(!this.anyRequestConfigured, "Can't configure mvcMatchers after anyRequest");
116-
ObjectPostProcessor<Object> opp = this.context.getBean(ObjectPostProcessor.class);
118+
ResolvableType type = ResolvableType.forClassWithGenerics(ObjectPostProcessor.class, Object.class);
119+
ObjectProvider<ObjectPostProcessor<Object>> postProcessors = this.context.getBeanProvider(type);
120+
ObjectPostProcessor<Object> opp = postProcessors.getObject();
117121
if (!this.context.containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
118122
throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME
119123
+ " of type " + HandlerMappingIntrospector.class.getName()

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,9 @@ private List<RequestMatcher> createAntMatchers(String... patterns) {
36863686
}
36873687

36883688
private List<RequestMatcher> createMvcMatchers(String... mvcPatterns) {
3689-
ObjectPostProcessor<Object> opp = getContext().getBean(ObjectPostProcessor.class);
3689+
ResolvableType type = ResolvableType.forClassWithGenerics(ObjectPostProcessor.class, Object.class);
3690+
ObjectProvider<ObjectPostProcessor<Object>> postProcessors = getContext().getBeanProvider(type);
3691+
ObjectPostProcessor<Object> opp = postProcessors.getObject();
36903692
if (!getContext().containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
36913693
throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME
36923694
+ " of type " + HandlerMappingIntrospector.class.getName()

config/src/test/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistryTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
28+
import org.springframework.beans.factory.ObjectProvider;
2829
import org.springframework.context.ApplicationContext;
2930
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.ResolvableType;
3032
import org.springframework.http.HttpMethod;
3133
import org.springframework.mock.web.MockHttpServletRequest;
3234
import org.springframework.security.config.MockServletContext;
@@ -79,7 +81,11 @@ public <O> O postProcess(O object) {
7981
public void setUp() {
8082
this.matcherRegistry = new TestRequestMatcherRegistry();
8183
this.context = mock(WebApplicationContext.class);
82-
given(this.context.getBean(ObjectPostProcessor.class)).willReturn(NO_OP_OBJECT_POST_PROCESSOR);
84+
ObjectProvider<ObjectPostProcessor<Object>> postProcessors = mock(ObjectProvider.class);
85+
ResolvableType type = ResolvableType.forClassWithGenerics(ObjectPostProcessor.class, Object.class);
86+
ObjectProvider<ObjectPostProcessor<Object>> given = this.context.getBeanProvider(type);
87+
given(given).willReturn(postProcessors);
88+
given(postProcessors.getObject()).willReturn(NO_OP_OBJECT_POST_PROCESSOR);
8389
given(this.context.getServletContext()).willReturn(MockServletContext.mvc());
8490
this.matcherRegistry.setApplicationContext(this.context);
8591
mockMvcIntrospector(true);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public void configureWhenDefaultLogoutSuccessHandlerForHasNullMatcherInLambdaThe
110110
@Test
111111
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnLogoutFilter() {
112112
this.spring.register(ObjectPostProcessorConfig.class).autowire();
113-
ObjectPostProcessor<LogoutFilter> objectPostProcessor = this.spring.getContext()
114-
.getBean(ObjectPostProcessor.class);
113+
ObjectPostProcessor<Object> objectPostProcessor = this.spring.getContext()
114+
.getBean(ObjectPostProcessorConfig.class).objectPostProcessor;
115115
verify(objectPostProcessor).postProcess(any(LogoutFilter.class));
116116
}
117117

config/src/test/java/org/springframework/security/config/annotation/web/configurers/RememberMeConfigurerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void postWhenNoUserDetailsServiceThenException() {
102102
@Test
103103
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnRememberMeAuthenticationFilter() {
104104
this.spring.register(ObjectPostProcessorConfig.class).autowire();
105-
verify(this.spring.getContext().getBean(ObjectPostProcessor.class))
105+
verify(this.spring.getContext().getBean(ObjectPostProcessorConfig.class).objectPostProcessor)
106106
.postProcess(any(RememberMeAuthenticationFilter.class));
107107
}
108108

0 commit comments

Comments
 (0)