Skip to content

Commit c6c20b9

Browse files
billkochjgrandja
authored andcommitted
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-1778
1 parent 6240ad8 commit c6c20b9

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

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

+12-5
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,113 @@
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+
*/
41+
class OAuth2AuthorizationServerBeanRegistrationAotProcessorTests {
42+
43+
private OAuth2AuthorizationServerBeanRegistrationAotProcessor processor;
44+
45+
private DefaultListableBeanFactory defaultListableBeanFactory;
46+
47+
@BeforeEach
48+
void setUp() {
49+
this.processor = new OAuth2AuthorizationServerBeanRegistrationAotProcessor();
50+
this.defaultListableBeanFactory = new DefaultListableBeanFactory();
51+
52+
}
53+
54+
@ParameterizedTest
55+
@ValueSource(classes = { JdbcOAuth2AuthorizationService.class, CustomJdbcOAuth2AuthorizationService.class,
56+
JdbcRegisteredClientRepository.class, CustomJdbcRegisteredClientRepository.class })
57+
void processAheadOfTimeWhenBeanTypeJdbcBasedImplThenReturnContribution(Class<?> beanClass) {
58+
this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass));
59+
60+
BeanRegistrationAotContribution aotContribution = this.processor
61+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName"));
62+
63+
assertThat(aotContribution).isNotNull();
64+
}
65+
66+
@ParameterizedTest
67+
@ValueSource(classes = { InMemoryOAuth2AuthorizationService.class, InMemoryRegisteredClientRepository.class,
68+
Object.class })
69+
void processAheadOfTimeWhenBeanTypeNotJdbcBasedImplThenDoesNotReturnContribution(Class<?> beanClass) {
70+
this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass));
71+
72+
BeanRegistrationAotContribution aotContribution = this.processor
73+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName"));
74+
75+
assertThat(aotContribution).isNull();
76+
}
77+
78+
@Test
79+
void processAheadOfTimeWhenMultipleBeanTypeJdbcBasedImplThenReturnContributionOnce() {
80+
this.defaultListableBeanFactory.registerBeanDefinition("oauth2AuthorizationService",
81+
new RootBeanDefinition(JdbcOAuth2AuthorizationService.class));
82+
83+
this.defaultListableBeanFactory.registerBeanDefinition("registeredClientRepository",
84+
new RootBeanDefinition(CustomJdbcRegisteredClientRepository.class));
85+
86+
BeanRegistrationAotContribution firstAotContribution = this.processor
87+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "oauth2AuthorizationService"));
88+
89+
BeanRegistrationAotContribution secondAotContribution = this.processor
90+
.processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "registeredClientRepository"));
91+
92+
assertThat(firstAotContribution).isNotNull();
93+
assertThat(secondAotContribution).isNull();
94+
}
95+
96+
static class CustomJdbcOAuth2AuthorizationService extends JdbcOAuth2AuthorizationService {
97+
98+
CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations,
99+
RegisteredClientRepository registeredClientRepository) {
100+
super(jdbcOperations, registeredClientRepository);
101+
}
102+
103+
}
104+
105+
static class CustomJdbcRegisteredClientRepository extends JdbcRegisteredClientRepository {
106+
107+
CustomJdbcRegisteredClientRepository(JdbcOperations jdbcOperations) {
108+
super(jdbcOperations);
109+
}
110+
111+
}
112+
113+
}

0 commit comments

Comments
 (0)