|
|
|
@ -2,15 +2,13 @@ package org.springframework.security.config; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.AfterClass; |
|
|
|
|
|
|
|
import org.junit.BeforeClass; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
|
|
import org.junit.After; |
|
|
|
import org.springframework.security.providers.ProviderManager; |
|
|
|
|
|
|
|
import org.springframework.security.providers.dao.DaoAuthenticationProvider; |
|
|
|
|
|
|
|
import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; |
|
|
|
import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; |
|
|
|
|
|
|
|
import org.springframework.security.util.InMemoryXmlApplicationContext; |
|
|
|
|
|
|
|
import org.springframework.security.AuthenticationManager; |
|
|
|
|
|
|
|
import org.springframework.security.providers.UsernamePasswordAuthenticationToken; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Ben Alex |
|
|
|
* @author Ben Alex |
|
|
|
@ -18,15 +16,21 @@ import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; |
|
|
|
* @version $Id$ |
|
|
|
* @version $Id$ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class JdbcUserServiceBeanDefinitionParserTests { |
|
|
|
public class JdbcUserServiceBeanDefinitionParserTests { |
|
|
|
private static ClassPathXmlApplicationContext appContext; |
|
|
|
private InMemoryXmlApplicationContext appContext; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass |
|
|
|
private static String DATA_SOURCE = |
|
|
|
public static void loadContext() { |
|
|
|
" <b:bean id='populator' class='org.springframework.security.config.DataSourcePopulator'>" + |
|
|
|
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/jdbc-user-details.xml"); |
|
|
|
" <b:property name='dataSource' ref='dataSource'/>" + |
|
|
|
} |
|
|
|
" </b:bean>" + |
|
|
|
|
|
|
|
" <b:bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'>" + |
|
|
|
|
|
|
|
" <b:property name='driverClassName' value='org.hsqldb.jdbcDriver'/>" + |
|
|
|
|
|
|
|
" <b:property name='url' value='jdbc:hsqldb:mem:jdbcnamespaces'/>" + |
|
|
|
|
|
|
|
" <b:property name='username' value='sa'/>" + |
|
|
|
|
|
|
|
" <b:property name='password' value=''/>" + |
|
|
|
|
|
|
|
" </b:bean>"; |
|
|
|
|
|
|
|
|
|
|
|
@AfterClass |
|
|
|
@After |
|
|
|
public static void closeAppContext() { |
|
|
|
public void closeAppContext() { |
|
|
|
if (appContext != null) { |
|
|
|
if (appContext != null) { |
|
|
|
appContext.close(); |
|
|
|
appContext.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -34,12 +38,28 @@ public class JdbcUserServiceBeanDefinitionParserTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void validUsernameIsFound() { |
|
|
|
public void validUsernameIsFound() { |
|
|
|
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE); |
|
|
|
setContext("<jdbc-user-service data-source-ref='dataSource'/>" + DATA_SOURCE); |
|
|
|
|
|
|
|
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE); |
|
|
|
assertTrue(mgr.loadUserByUsername("rod") != null); |
|
|
|
assertTrue(mgr.loadUserByUsername("rod") != null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void beanIdIsParsedCorrectly() { |
|
|
|
public void beanIdIsParsedCorrectly() { |
|
|
|
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService"); |
|
|
|
setContext("<jdbc-user-service id='customUserService' data-source-ref='dataSource'/>" + DATA_SOURCE); |
|
|
|
|
|
|
|
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void isSupportedByAuthenticationProviderElement() { |
|
|
|
|
|
|
|
setContext( |
|
|
|
|
|
|
|
"<authentication-provider>" + |
|
|
|
|
|
|
|
" <jdbc-user-service data-source-ref='dataSource'/>" + |
|
|
|
|
|
|
|
"</authentication-provider>" + DATA_SOURCE); |
|
|
|
|
|
|
|
AuthenticationManager mgr = (AuthenticationManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER); |
|
|
|
|
|
|
|
mgr.authenticate(new UsernamePasswordAuthenticationToken("rod", "koala")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setContext(String context) { |
|
|
|
|
|
|
|
appContext = new InMemoryXmlApplicationContext(context); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|