Skip to content

Commit 16e2bdc

Browse files
committed
Merge branch '6.1.x' into 6.2.x
2 parents 40a16d4 + c2447ec commit 16e2bdc

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

config/src/main/java/org/springframework/security/config/authentication/JdbcUserServiceBeanDefinitionParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-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.
@@ -42,7 +42,7 @@ protected String getBeanClassName(Element element) {
4242
@Override
4343
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
4444
String dataSource = element.getAttribute(ATT_DATA_SOURCE);
45-
if (dataSource != null) {
45+
if (StringUtils.hasText(dataSource)) {
4646
builder.addPropertyReference("dataSource", dataSource);
4747
}
4848
else {

config/src/test/java/org/springframework/security/config/authentication/JdbcUserServiceBeanDefinitionParserTests.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-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.
@@ -19,7 +19,10 @@
1919
import org.junit.jupiter.api.AfterEach;
2020
import org.junit.jupiter.api.Test;
2121
import org.w3c.dom.Element;
22+
import org.xml.sax.SAXParseException;
2223

24+
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
25+
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
2326
import org.springframework.security.authentication.AuthenticationManager;
2427
import org.springframework.security.authentication.CachingUserDetailsService;
2528
import org.springframework.security.authentication.ProviderManager;
@@ -33,6 +36,7 @@
3336
import org.springframework.security.util.FieldUtils;
3437

3538
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3640
import static org.mockito.Mockito.mock;
3741

3842
/**
@@ -160,6 +164,38 @@ public void rolePrefixIsUsedWhenSet() {
160164
assertThat(AuthorityUtils.authorityListToSet(rod.getAuthorities())).contains("PREFIX_ROLE_SUPERVISOR");
161165
}
162166

167+
@Test
168+
public void testEmptyDataSourceRef() {
169+
// @formatter:off
170+
String xml = "<authentication-manager>"
171+
+ " <authentication-provider>"
172+
+ " <jdbc-user-service data-source-ref=''/>"
173+
+ " </authentication-provider>"
174+
+ "</authentication-manager>";
175+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
176+
.isThrownBy(() -> setContext(xml))
177+
.withFailMessage("Expected exception due to empty data-source-ref")
178+
.withMessageContaining("data-source-ref is required for jdbc-user-service");
179+
// @formatter:on
180+
}
181+
182+
@Test
183+
public void testMissingDataSourceRef() {
184+
// @formatter:off
185+
String xml = "<authentication-manager>"
186+
+ " <authentication-provider>"
187+
+ " <jdbc-user-service/>"
188+
+ " </authentication-provider>"
189+
+ "</authentication-manager>";
190+
assertThatExceptionOfType(XmlBeanDefinitionStoreException.class)
191+
.isThrownBy(() -> setContext(xml))
192+
.withFailMessage("Expected exception due to missing data-source-ref")
193+
.havingRootCause()
194+
.isInstanceOf(SAXParseException.class)
195+
.withMessageContaining("Attribute 'data-source-ref' must appear on element 'jdbc-user-service'");
196+
// @formatter:on
197+
}
198+
163199
private void setContext(String context) {
164200
this.appContext = new InMemoryXmlApplicationContext(context);
165201
}

0 commit comments

Comments
 (0)