Skip to content

Commit c45d309

Browse files
committed
Final Commit
1 parent 1106fc0 commit c45d309

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ 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-
builder.addPropertyReference("dataSource", dataSource);
45+
if (StringUtils.hasText(dataSource)) {
46+
builder.addPropertyReference("dataSource", dataSource);
47+
}
48+
else {
49+
parserContext.getReaderContext()
50+
.error(ATT_DATA_SOURCE + " is required for " + Elements.JDBC_USER_SERVICE,
51+
parserContext.extractSource(element));
52+
}
4653
String usersQuery = element.getAttribute(ATT_USERS_BY_USERNAME_QUERY);
4754
String authoritiesQuery = element.getAttribute(ATT_AUTHORITIES_BY_USERNAME_QUERY);
4855
String groupAuthoritiesQuery = element.getAttribute(ATT_GROUP_AUTHORITIES_QUERY);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.security.config.authentication;
1818

19+
import org.assertj.core.api.Assertions;
1920
import org.junit.jupiter.api.AfterEach;
2021
import org.junit.jupiter.api.Test;
2122
import org.w3c.dom.Element;
2223

24+
import org.springframework.beans.factory.BeanDefinitionStoreException;
2325
import org.springframework.security.authentication.AuthenticationManager;
2426
import org.springframework.security.authentication.CachingUserDetailsService;
2527
import org.springframework.security.authentication.ProviderManager;
@@ -160,6 +162,44 @@ public void rolePrefixIsUsedWhenSet() {
160162
assertThat(AuthorityUtils.authorityListToSet(rod.getAuthorities())).contains("PREFIX_ROLE_SUPERVISOR");
161163
}
162164

165+
@Test
166+
public void testEmptyDataSourceRef() {
167+
// @formatter:off
168+
String xml = "<authentication-manager>"
169+
+ " <authentication-provider>"
170+
+ " <jdbc-user-service data-source-ref=''/>"
171+
+ " </authentication-provider>"
172+
+ "</authentication-manager>";
173+
// @formatter:on
174+
175+
try {
176+
setContext(xml);
177+
Assertions.fail("Expected exception due to empty data-source-ref");
178+
}
179+
catch (BeanDefinitionStoreException ex) {
180+
assertThat(ex.getMessage()).contains("data-source-ref is required");
181+
}
182+
}
183+
184+
@Test
185+
public void testMissingDataSourceRef() {
186+
// @formatter:off
187+
String xml = "<authentication-manager>"
188+
+ " <authentication-provider>"
189+
+ " <jdbc-user-service/>"
190+
+ " </authentication-provider>"
191+
+ "</authentication-manager>";
192+
// @formatter:on
193+
194+
try {
195+
setContext(xml);
196+
Assertions.fail("Expected exception due to missing data-source-ref");
197+
}
198+
catch (BeanDefinitionStoreException ex) {
199+
assertThat(ex.getMessage()).contains("XML document from").contains("is invalid");
200+
}
201+
}
202+
163203
private void setContext(String context) {
164204
this.appContext = new InMemoryXmlApplicationContext(context);
165205
}

0 commit comments

Comments
 (0)