Skip to content

Commit 7080ea6

Browse files
Add hints for ProxyFactoryBean AuthenticationManager
Closes gh-12367
1 parent 898c362 commit 7080ea6

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

config/spring-security-config.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ dependencies {
9393
testImplementation 'org.springframework:spring-jdbc'
9494
testImplementation 'org.springframework:spring-orm'
9595
testImplementation 'org.springframework:spring-tx'
96+
testImplementation 'org.springframework:spring-core-test'
9697
testImplementation ('org.springframework.data:spring-data-jpa') {
9798
exclude group: 'org.aspectj', module: 'aspectjrt'
9899
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.authentication.configuration;
18+
19+
import java.util.Set;
20+
21+
import org.springframework.aop.framework.AopProxyUtils;
22+
import org.springframework.aot.generate.GenerationContext;
23+
import org.springframework.aot.hint.ProxyHints;
24+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
25+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
26+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
27+
import org.springframework.beans.factory.support.RegisteredBean;
28+
import org.springframework.security.authentication.AuthenticationManager;
29+
import org.springframework.util.ClassUtils;
30+
31+
/**
32+
* AOT {@code BeanRegistrationAotProcessor} that detects beans that implement
33+
* {@link AuthenticationManager} creates the required proxy hints.
34+
*
35+
* @author Marcus da Coregio
36+
* @since 6.0.1
37+
* @see AuthenticationConfiguration#getAuthenticationManager()
38+
*/
39+
class AuthenticationManagerBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
40+
41+
@Override
42+
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
43+
Class<?> beanClass = registeredBean.getBeanClass();
44+
Set<Class<?>> allInterfacesForClass = ClassUtils.getAllInterfacesForClassAsSet(beanClass);
45+
if (allInterfacesForClass.contains(AuthenticationManager.class)) {
46+
return new AuthenticationManagerBeanRegistrationAotContribution();
47+
}
48+
return null;
49+
}
50+
51+
private static class AuthenticationManagerBeanRegistrationAotContribution
52+
implements BeanRegistrationAotContribution {
53+
54+
@Override
55+
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
56+
ProxyHints proxyHints = generationContext.getRuntimeHints().proxies();
57+
proxyHints.registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(AuthenticationManager.class));
58+
}
59+
60+
}
61+
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
2+
org.springframework.security.config.annotation.authentication.configuration.AuthenticationManagerBeanRegistrationAotProcessor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.authentication.configuration;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.SpringProxy;
22+
import org.springframework.aop.framework.Advised;
23+
import org.springframework.aot.generate.GenerationContext;
24+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
25+
import org.springframework.aot.test.generate.TestGenerationContext;
26+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
27+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
28+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
29+
import org.springframework.beans.factory.support.RegisteredBean;
30+
import org.springframework.beans.factory.support.RootBeanDefinition;
31+
import org.springframework.core.DecoratingProxy;
32+
import org.springframework.lang.Nullable;
33+
import org.springframework.security.authentication.AuthenticationManager;
34+
import org.springframework.security.core.Authentication;
35+
import org.springframework.security.core.AuthenticationException;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.mockito.Mockito.mock;
39+
40+
/**
41+
* Tests for {@link AuthenticationManagerBeanRegistrationAotProcessor}
42+
*
43+
* @author Marcus da Coregio
44+
*/
45+
class AuthenticationManagerBeanRegistrationAotProcessorTests {
46+
47+
private final AuthenticationManagerBeanRegistrationAotProcessor processor = new AuthenticationManagerBeanRegistrationAotProcessor();
48+
49+
private final GenerationContext generationContext = new TestGenerationContext();
50+
51+
@Test
52+
void shouldSkipWhenInterfaceNotImplemented() {
53+
process(NoAuthenticationManager.class);
54+
assertThat(this.generationContext.getRuntimeHints().proxies().jdkProxyHints()).isEmpty();
55+
}
56+
57+
@Test
58+
void shouldProcessWhenImplementsInterface() {
59+
process(MyAuthenticationManager.class);
60+
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AuthenticationManager.class, SpringProxy.class,
61+
Advised.class, DecoratingProxy.class)).accepts(this.generationContext.getRuntimeHints());
62+
}
63+
64+
@Test
65+
void shouldProcessWhenSuperclassImplementsInterface() {
66+
process(ChildAuthenticationManager.class);
67+
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AuthenticationManager.class, SpringProxy.class,
68+
Advised.class, DecoratingProxy.class)).accepts(this.generationContext.getRuntimeHints());
69+
}
70+
71+
private void process(Class<?> beanClass) {
72+
BeanRegistrationAotContribution contribution = createContribution(beanClass);
73+
if (contribution != null) {
74+
contribution.applyTo(this.generationContext, mock(BeanRegistrationCode.class));
75+
}
76+
}
77+
78+
@Nullable
79+
private BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
80+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
81+
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
82+
return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
83+
}
84+
85+
static class NoAuthenticationManager {
86+
87+
}
88+
89+
static class MyAuthenticationManager implements AuthenticationManager {
90+
91+
@Override
92+
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
93+
return null;
94+
}
95+
96+
}
97+
98+
static class ChildAuthenticationManager extends MyAuthenticationManager {
99+
100+
}
101+
102+
}

0 commit comments

Comments
 (0)