1
0
mirror of synced 2026-08-04 09:17:02 +00:00

Changes to namespace reinstating authentication-provider element in preference to "repository" to wrap convey that a user-service will be used as to authenticate against. Also introduced separate password-encoder element for use within authentication-provider.

This commit is contained in:
Luke Taylor
2007-12-21 15:50:56 +00:00
parent 70286f1197
commit cf80292de3
23 changed files with 455 additions and 395 deletions
@@ -1,39 +0,0 @@
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.config;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.User;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
/**
* @author Ben Alex
* @version $Id: DataSourcePopulator.java 2291 2007-12-03 02:56:52Z benalex $
*/
public class CustomUserDetailsService implements UserDetailsService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
if ("rod".equals(username)) {
return new User("rod", "koala", true, true, true, true, new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_FOO")});
}
throw new UsernameNotFoundException("unsupported by stub");
}
}
@@ -1,48 +0,0 @@
package org.springframework.security.config;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.providers.ProviderManager;
import org.springframework.security.providers.dao.DaoAuthenticationProvider;
/**
* @author Ben Alex
* @version $Id$
*/
public class CustomUserDetailsTests {
private static ClassPathXmlApplicationContext appContext;
@BeforeClass
public static void loadContext() {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/custom-user-details.xml");
}
@AfterClass
public static void closeAppContext() {
if (appContext != null) {
appContext.close();
}
}
@Test
public void testUsersFound() {
CustomUserDetailsService mgr = (CustomUserDetailsService) appContext.getBean("myDetails");
assertTrue(mgr.loadUserByUsername("rod") != null);
}
@Test
public void testProviderManagerSetup() {
ProviderManager manager = (ProviderManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER);
List providers = manager.getProviders();
assertTrue(providers.size() == 1);
assertTrue(providers.iterator().next() instanceof DaoAuthenticationProvider);
DaoAuthenticationProvider provider = (DaoAuthenticationProvider) providers.iterator().next();
assertTrue(provider.getUserDetailsService() instanceof CustomUserDetailsService);
}
}
@@ -13,6 +13,7 @@ import org.springframework.security.ui.webapp.DefaultLoginPageGeneratingFilter;
import org.springframework.security.util.FilterChainProxy;
import org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.BeansException;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
@@ -32,7 +33,11 @@ public class HttpSecurityBeanDefinitionParserTests {
@BeforeClass
public static void loadContext() {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/http-security.xml");
try {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/http-security.xml");
} catch (BeansException e) {
e.printStackTrace();
}
}
@AfterClass
@@ -70,7 +75,7 @@ public class HttpSecurityBeanDefinitionParserTests {
assertTrue(filters.next() instanceof AuthenticationProcessingFilter);
assertTrue(filters.next() instanceof DefaultLoginPageGeneratingFilter);
assertTrue(filters.next() instanceof BasicProcessingFilter);
assertTrue(filters.next() instanceof SecurityContextHolderAwareRequestFilter);
assertTrue(filters.next() instanceof SecurityContextHolderAwareRequestFilter);
assertTrue(filters.next() instanceof RememberMeProcessingFilter);
assertTrue(filters.next() instanceof ExceptionTranslationFilter);
assertTrue(filters.next() instanceof FilterSecurityInterceptor);
@@ -14,9 +14,10 @@ import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager;
/**
* @author Ben Alex
* @author Luke Taylor
* @version $Id$
*/
public class JdbcUserDetailsTests {
public class JdbcUserServiceBeanDefinitionParserTests {
private static ClassPathXmlApplicationContext appContext;
@BeforeClass
@@ -32,18 +33,13 @@ public class JdbcUserDetailsTests {
}
@Test
public void testUsersFound() {
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.JDBC_USER_DETAILS_MANAGER);
public void validUsernameIsFound() {
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE);
assertTrue(mgr.loadUserByUsername("rod") != null);
}
@Test
public void testProviderManagerSetup() {
ProviderManager manager = (ProviderManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER);
List providers = manager.getProviders();
assertTrue(providers.size() == 1);
assertTrue(providers.iterator().next() instanceof DaoAuthenticationProvider);
DaoAuthenticationProvider provider = (DaoAuthenticationProvider) providers.iterator().next();
assertTrue(provider.getUserDetailsService() instanceof JdbcUserDetailsManager);
public void beanIdIsParsedCorrectly() {
JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService");
}
}
@@ -8,11 +8,13 @@ import org.springframework.security.userdetails.ldap.LdapUserDetailsImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.beans.BeansException;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
/**
@@ -24,7 +26,13 @@ public class LdapBeanDefinitionParserTests {
@BeforeClass
public static void loadContext() {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/ldap-embedded-default.xml");
try {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/ldap-embedded-default.xml");
} catch (BeansException e) {
e.printStackTrace();
Assert.fail("Exception loading context " + e);
}
}
@AfterClass
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<beans:bean id="myDetails" class="org.springframework.security.config.CustomUserDetailsService"/>
<repository>
<custom-user-service ref="myDetails"/>
</repository>
</beans:beans>
@@ -25,12 +25,12 @@ http://www.springframework.org/schema/security http://www.springframework.org/sc
<remember-me key="doesntmatter" token-repository="tokenRepo"/>
</http>
<repository>
<authentication-provider>
<user-service>
<user name="bob" password="bobspassword" authorities="ROLE_A,ROLE_B" />
<user name="bill" password="billspassword" authorities="ROLE_A,ROLE_B,AUTH_OTHER" />
</user-service>
</repository>
</authentication-provider>
<beans:bean name="tokenRepo" class="org.springframework.security.ui.rememberme.InMemoryTokenRepositoryImpl"/>
@@ -17,8 +17,8 @@ http://www.springframework.org/schema/security http://www.springframework.org/sc
<beans:property name="password" value=""/>
</beans:bean>
<repository>
<jdbc-user-service data-source="dataSource"/>
</repository>
<jdbc-user-service data-source="dataSource"/>
<jdbc-user-service id="customUserService" data-source="dataSource"/>
</beans:beans>
@@ -16,11 +16,11 @@ http://www.springframework.org/schema/security http://www.springframework.org/sc
</intercept-methods>
</b:bean>
<repository>
<authentication-provider>
<user-service>
<user name="bob" password="bobspassword" authorities="ROLE_A,ROLE_B" />
<user name="bill" password="billspassword" authorities="ROLE_A,ROLE_B,AUTH_OTHER" />
</user-service>
</repository>
</authentication-provider>
</b:beans>