Skip to content

Commit 0aa2c3d

Browse files
author
Steve Riesenberg
committed
Add post processor to expose ProviderSettings beans
1 parent 3d4df88 commit 0aa2c3d

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2AuthorizationServerConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
3636
import org.springframework.security.oauth2.jwt.JwtDecoder;
3737
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
38+
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
3839
import org.springframework.security.web.SecurityFilterChain;
3940
import org.springframework.security.web.util.matcher.RequestMatcher;
4041

@@ -89,4 +90,11 @@ public static JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
8990
return new NimbusJwtDecoder(jwtProcessor);
9091
}
9192

93+
@Bean
94+
RegisterMissingBeanPostProcessor registerMissingBeanPostProcessor() {
95+
RegisterMissingBeanPostProcessor postProcessor = new RegisterMissingBeanPostProcessor();
96+
postProcessor.addBeanDefinition(ProviderSettings.class, () -> ProviderSettings.builder().build());
97+
return postProcessor;
98+
}
99+
92100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020-2021 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.web.configuration;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.function.Supplier;
22+
23+
import org.springframework.beans.BeansException;
24+
import org.springframework.beans.factory.BeanFactory;
25+
import org.springframework.beans.factory.BeanFactoryAware;
26+
import org.springframework.beans.factory.BeanFactoryUtils;
27+
import org.springframework.beans.factory.ListableBeanFactory;
28+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
29+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
30+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31+
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
32+
import org.springframework.beans.factory.support.RootBeanDefinition;
33+
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
34+
35+
/**
36+
* Post processor to register one or more bean definitions on container initialization, if not already present.
37+
*
38+
* @author Steve Riesenberg
39+
* @since 0.2.0
40+
*/
41+
final class RegisterMissingBeanPostProcessor implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {
42+
43+
private final AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
44+
private final List<AbstractBeanDefinition> beanDefinitions = new ArrayList<>();
45+
46+
private BeanFactory beanFactory;
47+
48+
@Override
49+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
50+
for (AbstractBeanDefinition beanDefinition : this.beanDefinitions) {
51+
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
52+
(ListableBeanFactory) this.beanFactory, beanDefinition.getBeanClass(), false, false);
53+
if (beanNames.length == 0) {
54+
String beanName = this.beanNameGenerator.generateBeanName(beanDefinition, registry);
55+
registry.registerBeanDefinition(beanName, beanDefinition);
56+
}
57+
}
58+
}
59+
60+
@Override
61+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
62+
}
63+
64+
<T> void addBeanDefinition(Class<T> beanClass, Supplier<T> beanSupplier) {
65+
this.beanDefinitions.add(new RootBeanDefinition(beanClass, beanSupplier));
66+
}
67+
68+
@Override
69+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
70+
this.beanFactory = beanFactory;
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2020-2021 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.web.configuration;
18+
19+
import java.util.function.Supplier;
20+
21+
import org.junit.Test;
22+
import org.mockito.ArgumentCaptor;
23+
24+
import org.springframework.beans.factory.config.BeanDefinition;
25+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
26+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
27+
import org.springframework.beans.factory.support.RootBeanDefinition;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.ArgumentMatchers.endsWith;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
33+
import static org.mockito.Mockito.verifyNoInteractions;
34+
35+
/**
36+
* Tests for {@link RegisterMissingBeanPostProcessor}.
37+
*
38+
* @author Steve Riesenberg
39+
*/
40+
public class RegisterMissingBeanPostProcessorTests {
41+
42+
private final RegisterMissingBeanPostProcessor postProcessor = new RegisterMissingBeanPostProcessor();
43+
44+
@Test
45+
public void postProcessBeanDefinitionRegistryWhenClassAddedThenRegisteredWithClass() {
46+
this.postProcessor.addBeanDefinition(SimpleBean.class, null);
47+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
48+
49+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
50+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
51+
52+
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
53+
verify(beanDefinitionRegistry).registerBeanDefinition(endsWith("SimpleBean"), beanDefinitionCaptor.capture());
54+
55+
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue();
56+
assertThat(beanDefinition.getBeanClass()).isEqualTo(SimpleBean.class);
57+
assertThat(beanDefinition.getInstanceSupplier()).isNull();
58+
}
59+
60+
@Test
61+
public void postProcessBeanDefinitionRegistryWhenSupplierAddedThenRegisteredWithSupplier() {
62+
Supplier<SimpleBean> beanSupplier = () -> new SimpleBean("string");
63+
this.postProcessor.addBeanDefinition(SimpleBean.class, beanSupplier);
64+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
65+
66+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
67+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
68+
69+
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
70+
verify(beanDefinitionRegistry).registerBeanDefinition(endsWith("SimpleBean"), beanDefinitionCaptor.capture());
71+
72+
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue();
73+
assertThat(beanDefinition.getBeanClass()).isEqualTo(SimpleBean.class);
74+
assertThat(beanDefinition.getInstanceSupplier()).isEqualTo(beanSupplier);
75+
}
76+
77+
@Test
78+
public void postProcessBeanDefinitionRegistryWhenNoBeanDefinitionsAddedThenNoneRegistered() {
79+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
80+
81+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
82+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
83+
verifyNoInteractions(beanDefinitionRegistry);
84+
}
85+
86+
@Test
87+
public void postProcessBeanDefinitionRegistryWhenBeanDefinitionAlreadyExistsThenNoneRegistered() {
88+
this.postProcessor.addBeanDefinition(SimpleBean.class, null);
89+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
90+
beanFactory.registerBeanDefinition("simpleBean", new RootBeanDefinition(SimpleBean.class));
91+
this.postProcessor.setBeanFactory(beanFactory);
92+
93+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
94+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
95+
verifyNoInteractions(beanDefinitionRegistry);
96+
}
97+
98+
private static final class SimpleBean {
99+
100+
private final String field;
101+
102+
private SimpleBean(String field) {
103+
this.field = field;
104+
}
105+
106+
public String getField() {
107+
return field;
108+
}
109+
110+
}
111+
112+
}

0 commit comments

Comments
 (0)