Skip to content

Commit a60b1f6

Browse files
committed
Merge branch '1.2.x' into 1.3.x
Closes gh-1782
2 parents 096bd72 + c6c20b9 commit a60b1f6

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

Diff for: 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,8 +43,10 @@
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;
4647
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeActor;
4748
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeCompositeAuthenticationToken;
49+
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
4850
import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module;
4951
import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat;
5052
import org.springframework.security.web.authentication.WebAuthenticationDetails;
@@ -59,6 +61,7 @@
5961
*
6062
* @author Joe Grandja
6163
* @author Josh Long
64+
* @author William Koch
6265
* @since 1.2
6366
*/
6467
class OAuth2AuthorizationServerBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@@ -67,11 +70,15 @@ class OAuth2AuthorizationServerBeanRegistrationAotProcessor implements BeanRegis
6770

6871
@Override
6972
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
70-
String beanClassName = registeredBean.getBeanClass().getName();
73+
boolean isJdbcBasedOAuth2AuthorizationService = JdbcOAuth2AuthorizationService.class
74+
.isAssignableFrom(registeredBean.getBeanClass());
75+
76+
boolean isJdbcBasedRegisteredClientRepository = JdbcRegisteredClientRepository.class
77+
.isAssignableFrom(registeredBean.getBeanClass());
78+
7179
// @formatter:off
72-
if ((beanClassName.equals("org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService") ||
73-
beanClassName.equals("org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository")) &&
74-
!this.jackson2Contributed) {
80+
if ((isJdbcBasedOAuth2AuthorizationService || isJdbcBasedRegisteredClientRepository)
81+
&& !this.jackson2Contributed) {
7582
Jackson2ConfigurationBeanRegistrationAotContribution jackson2Contribution =
7683
new Jackson2ConfigurationBeanRegistrationAotContribution();
7784
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)