Skip to content

Commit 115a78d

Browse files
Steve Riesenbergjgrandja
authored andcommitted
Add post processor to register ProviderSettings Bean
Closes gh-373
1 parent 1929e3a commit 115a78d

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-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,71 @@
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+
private final AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
43+
private final List<AbstractBeanDefinition> beanDefinitions = new ArrayList<>();
44+
private BeanFactory beanFactory;
45+
46+
@Override
47+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
48+
for (AbstractBeanDefinition beanDefinition : this.beanDefinitions) {
49+
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
50+
(ListableBeanFactory) this.beanFactory, beanDefinition.getBeanClass(), false, false);
51+
if (beanNames.length == 0) {
52+
String beanName = this.beanNameGenerator.generateBeanName(beanDefinition, registry);
53+
registry.registerBeanDefinition(beanName, beanDefinition);
54+
}
55+
}
56+
}
57+
58+
@Override
59+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
60+
}
61+
62+
<T> void addBeanDefinition(Class<T> beanClass, Supplier<T> beanSupplier) {
63+
this.beanDefinitions.add(new RootBeanDefinition(beanClass, beanSupplier));
64+
}
65+
66+
@Override
67+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
68+
this.beanFactory = beanFactory;
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
private final RegisterMissingBeanPostProcessor postProcessor = new RegisterMissingBeanPostProcessor();
42+
43+
@Test
44+
public void postProcessBeanDefinitionRegistryWhenClassAddedThenRegisteredWithClass() {
45+
this.postProcessor.addBeanDefinition(SimpleBean.class, null);
46+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
47+
48+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
49+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
50+
51+
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
52+
verify(beanDefinitionRegistry).registerBeanDefinition(endsWith("SimpleBean"), beanDefinitionCaptor.capture());
53+
54+
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue();
55+
assertThat(beanDefinition.getBeanClass()).isEqualTo(SimpleBean.class);
56+
assertThat(beanDefinition.getInstanceSupplier()).isNull();
57+
}
58+
59+
@Test
60+
public void postProcessBeanDefinitionRegistryWhenSupplierAddedThenRegisteredWithSupplier() {
61+
Supplier<SimpleBean> beanSupplier = () -> new SimpleBean("string");
62+
this.postProcessor.addBeanDefinition(SimpleBean.class, beanSupplier);
63+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
64+
65+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
66+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
67+
68+
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
69+
verify(beanDefinitionRegistry).registerBeanDefinition(endsWith("SimpleBean"), beanDefinitionCaptor.capture());
70+
71+
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue();
72+
assertThat(beanDefinition.getBeanClass()).isEqualTo(SimpleBean.class);
73+
assertThat(beanDefinition.getInstanceSupplier()).isEqualTo(beanSupplier);
74+
}
75+
76+
@Test
77+
public void postProcessBeanDefinitionRegistryWhenNoBeanDefinitionsAddedThenNoneRegistered() {
78+
this.postProcessor.setBeanFactory(new DefaultListableBeanFactory());
79+
80+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
81+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
82+
verifyNoInteractions(beanDefinitionRegistry);
83+
}
84+
85+
@Test
86+
public void postProcessBeanDefinitionRegistryWhenBeanDefinitionAlreadyExistsThenNoneRegistered() {
87+
this.postProcessor.addBeanDefinition(SimpleBean.class, null);
88+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
89+
beanFactory.registerBeanDefinition("simpleBean", new RootBeanDefinition(SimpleBean.class));
90+
this.postProcessor.setBeanFactory(beanFactory);
91+
92+
BeanDefinitionRegistry beanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
93+
this.postProcessor.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
94+
verifyNoInteractions(beanDefinitionRegistry);
95+
}
96+
97+
private static final class SimpleBean {
98+
private final String field;
99+
100+
private SimpleBean(String field) {
101+
this.field = field;
102+
}
103+
104+
private String getField() {
105+
return field;
106+
}
107+
108+
}
109+
110+
}

0 commit comments

Comments
 (0)