SEC-271: Added BeanDefitnitionParser for principal-repository, extended security schema and added unit tests
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.acegisecurity.config;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.acegisecurity.userdetails.User;
|
||||
import org.acegisecurity.userdetails.UserDetailsService;
|
||||
import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
|
||||
import org.acegisecurity.userdetails.memory.UserMap;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author vpuri
|
||||
*
|
||||
*/
|
||||
public class PrincipalRepositoryNamespaceTests extends TestCase {
|
||||
|
||||
public void testParserWithUserDefinition() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"org/acegisecurity/config/principal-repository-user-map.xml");
|
||||
|
||||
ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) context
|
||||
.getAutowireCapableBeanFactory();
|
||||
|
||||
String[] names = clbf.getBeanNamesForType(UserDetailsService.class);
|
||||
assertEquals(1, names.length);
|
||||
|
||||
RootBeanDefinition definition = (RootBeanDefinition) clbf.getBeanDefinition(names[0]);
|
||||
assertEquals(InMemoryDaoImpl.class, definition.getBeanClass());
|
||||
|
||||
UserMap map = new UserMap();
|
||||
|
||||
GrantedAuthority[] authotities = { new GrantedAuthorityImpl("ROLE_YO"), new GrantedAuthorityImpl("ROLE_YOYO") };
|
||||
|
||||
User user = new User("vishal", "nottellingya", true, true, true, true, authotities);
|
||||
|
||||
map.addUser(user);
|
||||
|
||||
assertPropertyValues(map, definition, "userMap");
|
||||
|
||||
}
|
||||
|
||||
private void assertPropertyValues(UserMap assertionValue, RootBeanDefinition definition, String property) {
|
||||
PropertyValue propertyValue = definition.getPropertyValues().getPropertyValue(property);
|
||||
assertNotNull(propertyValue);
|
||||
assertTrue(propertyValue.getValue() instanceof UserMap);
|
||||
UserMap users = (UserMap) propertyValue.getValue();
|
||||
assertTrue(assertionValue.getUserCount() == users.getUserCount());
|
||||
assertEquals(assertionValue.getUser("vishal"), users.getUser("vishal"));
|
||||
assertTrue(users.getUser("vishal").isEnabled());
|
||||
assertTrue(users.getUser("vishal").isAccountNonExpired());
|
||||
assertTrue(users.getUser("vishal").isAccountNonLocked());
|
||||
assertTrue(users.getUser("vishal").isCredentialsNonExpired());
|
||||
assertEquals(2, users.getUser("vishal").getAuthorities().length);
|
||||
assertEquals(new GrantedAuthorityImpl("ROLE_YO"), users.getUser("vishal").getAuthorities()[0]);
|
||||
assertEquals(new GrantedAuthorityImpl("ROLE_YOYO"), users.getUser("vishal").getAuthorities()[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util-2.0.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
|
||||
|
||||
<!-- http://www.springframework.org/schema/security file:/Users/vpuri/interface21/acegisecurity/trunk/acegisecurity/core/src/main/resources/org/acegisecurity/config/spring-security-2.0.xsd -->
|
||||
<!-- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" -->
|
||||
|
||||
<!-- If LogoutFilter does not have setHandlers populated, introspect app ctx for LogoutHandlers, using Ordered (if present, otherwise assume Integer.MAX_VALUE) -->
|
||||
<!-- The logoutUrl and redirectAfterLogout are both optional and default to that shown -->
|
||||
<security:logout-support id="logoutFilter"
|
||||
redirectAfterLogoutUrl="/" logoutUrl="/logout" />
|
||||
|
||||
<security:authentication-remember-me-services
|
||||
id="rememberMeServices" key="someValue" />
|
||||
|
||||
<security:principal-repository id="userDetailsService">
|
||||
<security:user-definition username="vishal"
|
||||
password="nottellingya" enabled="true">
|
||||
<security:granted-authority authority="ROLE_YO" />
|
||||
<security:granted-authority authority="ROLE_YOYO" />
|
||||
<!-- TODO: <security:granted-authority-ref authorityBeanRef="fooBarAuthority"/>-->
|
||||
</security:user-definition>
|
||||
</security:principal-repository>
|
||||
</beans>
|
||||
@@ -14,8 +14,11 @@ http://www.springframework.org/schema/security file:/Users/vpuri/interface21/ace
|
||||
<!-- userDetailsService, This is used if they want an out-of-the-bx UserDetailsService; if they write their own, this goes away and they wire a legacy bean definition and then the various
|
||||
beans depending on a UserDetailsService will auto-detect it at runtime OR provide a way of setUserDetailsService(UserDetailsService) if to specified explicitly.
|
||||
If they fail to provide a repository, the security-autodetect will set one up for them with a few basic in-memory users and pwds -->
|
||||
|
||||
<!--<security:security-autoconfig/> -->
|
||||
|
||||
<security:principal-repository id="userDetailsService">
|
||||
<security:jdbc dataSourceBeanRef="dataSource" jdbcTemplateBeanRef="jdbcTemplate" authoritiesByUsernameQuery="" rolePrefix="" usernameBasedPrimaryKey="true" usersByUsernameQuery=""/>
|
||||
<security:jdbc dataSourceBeanRef="dataSource"/>
|
||||
</security:principal-repository>
|
||||
|
||||
<bean id="dataSource"
|
||||
@@ -35,8 +38,7 @@ http://www.springframework.org/schema/security file:/Users/vpuri/interface21/ace
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<property name="dataSource" ref="dataSource"></property>
|
||||
</bean>
|
||||
|
||||
</bean>-->
|
||||
</beans>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util-2.0.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
|
||||
|
||||
<!-- http://www.springframework.org/schema/security file:/Users/vpuri/interface21/acegisecurity/trunk/acegisecurity/core/src/main/resources/org/acegisecurity/config/spring-security-2.0.xsd -->
|
||||
<!-- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" -->
|
||||
|
||||
<!-- userDetailsService, This is used if they want an out-of-the-bx UserDetailsService; if they write their own, this goes away and they wire a legacy bean definition and then the various
|
||||
beans depending on a UserDetailsService will auto-detect it at runtime OR provide a way of setUserDetailsService(UserDetailsService) if to specified explicitly.
|
||||
If they fail to provide a repository, the security-autodetect will set one up for them with a few basic in-memory users and pwds -->
|
||||
|
||||
<security:principal-repository id="userDetailsService">
|
||||
<security:properties resource="classpath:org/acegisecurity/config/user.properties"/>
|
||||
</security:principal-repository>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util-2.0.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
|
||||
|
||||
<!-- http://www.springframework.org/schema/security file:/Users/vpuri/interface21/acegisecurity/trunk/acegisecurity/core/src/main/resources/org/acegisecurity/config/spring-security-2.0.xsd -->
|
||||
<!-- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" -->
|
||||
|
||||
<!-- userDetailsService, This is used if they want an out-of-the-bx UserDetailsService; if they write their own, this goes away and they wire a legacy bean definition and then the various
|
||||
beans depending on a UserDetailsService will auto-detect it at runtime OR provide a way of setUserDetailsService(UserDetailsService) if to specified explicitly.
|
||||
If they fail to provide a repository, the security-autodetect will set one up for them with a few basic in-memory users and pwds -->
|
||||
|
||||
<security:principal-repository id="userDetailsService">
|
||||
<security:user-definition username="vishal" password="nottellingya" enabled="true">
|
||||
<security:granted-authority authority="ROLE_YO"/>
|
||||
<security:granted-authority authority="ROLE_YOYO"/>
|
||||
<!-- TODO: <security:granted-authority-ref authorityBeanRef="fooBarAuthority"/>-->
|
||||
</security:user-definition>
|
||||
</security:principal-repository>
|
||||
|
||||
<!-- TODO: <security:granted-authority id="fooBarAuthority" authority="ROLE_FOOBAR"/> -->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,2 @@
|
||||
vishal=ity,ROLE_ADMIN
|
||||
ity=vishal,ROLE_TELLER
|
||||
Reference in New Issue
Block a user