Skip to content

Commit 528373a

Browse files
committed
AOT contributions will be registered for JbcOAuth2AuthorizationService subclasses
Prior to this commit, String-based class name comparisons were used for determining if a bean was of type JdbcOAuth2AuthorizationService or JdbcRegisteredClientRepository. Now JdbcOAuth2AuthorizationService.class.isAssignableFrom(...) and JdbcRegisteredClientRepository.class.isAssignableFrom(...) is used so that any subclasses are detected and the necessary AOT hints are contributed. closes gh-1737
1 parent 3798a4d commit 528373a

File tree

2 files changed

+126
-5
lines changed

2 files changed

+126
-5
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/aot/hint/OAuth2AuthorizationServerBeanRegistrationAotProcessor.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,8 @@
4343
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
4444
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
4545
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
46+
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService;
47+
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
4648
import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module;
4749
import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat;
4850
import org.springframework.security.web.authentication.WebAuthenticationDetails;
@@ -57,6 +59,7 @@
5759
*
5860
* @author Joe Grandja
5961
* @author Josh Long
62+
* @author William Koch
6063
* @since 1.2
6164
*/
6265
class OAuth2AuthorizationServerBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@@ -65,11 +68,15 @@ class OAuth2AuthorizationServerBeanRegistrationAotProcessor implements BeanRegis
6568

6669
@Override
6770
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
68-
String beanClassName = registeredBean.getBeanClass().getName();
71+
boolean isJdbcBasedOAuth2AuthorizationService = JdbcOAuth2AuthorizationService.class
72+
.isAssignableFrom(registeredBean.getBeanClass());
73+
74+
boolean isJdbcBasedRegisteredClientRepository = JdbcRegisteredClientRepository.class
75+
.isAssignableFrom(registeredBean.getBeanClass());
76+
6977
// @formatter:off
70-
if ((beanClassName.equals("org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService") ||
71-
beanClassName.equals("org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository")) &&
72-
!this.jackson2Contributed) {
78+
if ((isJdbcBasedOAuth2AuthorizationService || isJdbcBasedRegisteredClientRepository)
79+
&& !this.jackson2Contributed) {
7380
Jackson2ConfigurationBeanRegistrationAotContribution jackson2Contribution =
7481
new Jackson2ConfigurationBeanRegistrationAotContribution();
7582
this.jackson2Contributed = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2020-2024 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+
package org.springframework.security.oauth2.server.authorization.aot.hint;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.ValueSource;
22+
23+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
24+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
25+
import org.springframework.beans.factory.support.RegisteredBean;
26+
import org.springframework.beans.factory.support.RootBeanDefinition;
27+
import org.springframework.jdbc.core.JdbcOperations;
28+
import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService;
29+
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService;
30+
import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
31+
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
32+
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link OAuth2AuthorizationServerBeanRegistrationAotProcessor}.
38+
*
39+
* @author William Koch
40+
* @author Joe Grandja
41+
*/
42+
class OAuth2AuthorizationServerBeanRegistrationAotProcessorTests {
43+
44+
private OAuth2AuthorizationServerBeanRegistrationAotProcessor processor;
45+
46+
private DefaultListableBeanFactory defaultListableBeanFactory;
47+
48+
@BeforeEach
49+
void setUp() {
50+
this.processor = new OAuth2AuthorizationServerBeanRegistrationAotProcessor();
51+
this.defaultListableBeanFactory = new DefaultListableBeanFactory();
52+
53+
}
54+
55+
@ParameterizedTest
56+
@ValueSource(classes = { JdbcOAuth2AuthorizationService.class, CustomJdbcOAuth2AuthorizationService.class,
57+
JdbcRegisteredClientRepository.class, CustomJdbcRegisteredClientRepository.class })
58+
void processAheadOfTimeWhenBeanTypeJdbcBasedImplThenReturnContribution(Class<?> beanClass) {
59+
this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass));
60+
61+
BeanRegistrationAotContribution aotContribution = this.processor
62+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName"));
63+
64+
assertThat(aotContribution).isNotNull();
65+
}
66+
67+
@ParameterizedTest
68+
@ValueSource(classes = { InMemoryOAuth2AuthorizationService.class, InMemoryRegisteredClientRepository.class,
69+
Object.class })
70+
void processAheadOfTimeWhenBeanTypeNotJdbcBasedImplThenDoesNotReturnContribution(Class<?> beanClass) {
71+
this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass));
72+
73+
BeanRegistrationAotContribution aotContribution = this.processor
74+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName"));
75+
76+
assertThat(aotContribution).isNull();
77+
}
78+
79+
@Test
80+
void processAheadOfTimeWhenMultipleBeanTypeJdbcBasedImplThenReturnContributionOnce() {
81+
this.defaultListableBeanFactory.registerBeanDefinition("oauth2AuthorizationService",
82+
new RootBeanDefinition(JdbcOAuth2AuthorizationService.class));
83+
84+
this.defaultListableBeanFactory.registerBeanDefinition("registeredClientRepository",
85+
new RootBeanDefinition(CustomJdbcRegisteredClientRepository.class));
86+
87+
BeanRegistrationAotContribution firstAotContribution = this.processor
88+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "oauth2AuthorizationService"));
89+
90+
BeanRegistrationAotContribution secondAotContribution = this.processor
91+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "registeredClientRepository"));
92+
93+
assertThat(firstAotContribution).isNotNull();
94+
assertThat(secondAotContribution).isNull();
95+
}
96+
97+
static class CustomJdbcOAuth2AuthorizationService extends JdbcOAuth2AuthorizationService {
98+
99+
CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
100+
RegisteredClientRepository registeredClientRepository) {
101+
super(jdbcOperations, registeredClientRepository);
102+
}
103+
104+
}
105+
106+
static class CustomJdbcRegisteredClientRepository extends JdbcRegisteredClientRepository {
107+
108+
CustomJdbcRegisteredClientRepository(JdbcOperations jdbcOperations) {
109+
super(jdbcOperations);
110+
}
111+
112+
}
113+
114+
}

0 commit comments

Comments
 (0)