Initial commit.
This commit is contained in:
81
samples/attributes/src/applicationContext.xml
Normal file
81
samples/attributes/src/applicationContext.xml
Normal file
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||
<!--
|
||||
* The Acegi Security System for Spring is published under the terms
|
||||
* of the Apache Software License.
|
||||
* $Id$
|
||||
-->
|
||||
|
||||
<beans>
|
||||
|
||||
<!-- =================== SECURITY SYSTEM DEFINITIONS ================== -->
|
||||
|
||||
<!-- RunAsManager -->
|
||||
<bean id="runAsManager" class="net.sf.acegisecurity.runas.RunAsManagerImpl">
|
||||
<property name="key"><value>my_run_as_password</value></property>
|
||||
</bean>
|
||||
|
||||
<!-- ~~~~~~~~~~~~~~~~~~~~ AUTHENTICATION DEFINITIONS ~~~~~~~~~~~~~~~~~~ -->
|
||||
|
||||
<!-- This authentication provider accepts any presented TestingAuthenticationToken -->
|
||||
<bean id="testingAuthenticationProvider" class="net.sf.acegisecurity.providers.TestingAuthenticationProvider"/>
|
||||
|
||||
<!-- The authentication manager that iterates through our only authentication provider -->
|
||||
<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
|
||||
<property name="providers">
|
||||
<list>
|
||||
<ref bean="testingAuthenticationProvider"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- ~~~~~~~~~~~~~~~~~~~~ AUTHORIZATION DEFINITIONS ~~~~~~~~~~~~~~~~~~~ -->
|
||||
|
||||
<!-- An access decision voter that reads ROLE_* configuaration settings -->
|
||||
<bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
|
||||
|
||||
<!-- A unanimous access decision manager -->
|
||||
<bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.UnanimousBased">
|
||||
<property name="allowIfAllAbstainDecisions"><value>false</value></property>
|
||||
<property name="decisionVoters">
|
||||
<list>
|
||||
<ref bean="roleVoter"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- ===================== SECURITY DEFINITIONS ======================= -->
|
||||
|
||||
<bean id="attributes" class="org.springframework.metadata.commons.CommonsAttributes"/>
|
||||
<bean id="methodDefinitionSource" class="net.sf.acegisecurity.MethodDefinitionAttributes">
|
||||
<property name="attributes"><ref local="attributes"/></property>
|
||||
</bean>
|
||||
|
||||
<!-- We don't validate config attributes, as it's unsupported by MethodDefinitionAttributes -->
|
||||
<bean id="securityInterceptor" class="net.sf.acegisecurity.SecurityInterceptor">
|
||||
<property name="validateConfigAttributes"><value>false</value></property>
|
||||
<property name="authenticationManager"><ref bean="authenticationManager"/></property>
|
||||
<property name="accessDecisionManager"><ref bean="accessDecisionManager"/></property>
|
||||
<property name="runAsManager"><ref bean="runAsManager"/></property>
|
||||
<property name="methodDefinitionSource"><ref bean="methodDefinitionSource"/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="bankService" class="sample.attributes.BankServiceImpl"/>
|
||||
|
||||
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
|
||||
<!-- names of the interceptors that will be applied by the proxy -->
|
||||
<property name="interceptorNames">
|
||||
<list>
|
||||
<value>securityInterceptor</value>
|
||||
</list>
|
||||
</property>
|
||||
|
||||
<!-- the bean names to automatically generate proxies for -->
|
||||
<property name="beanNames">
|
||||
<list>
|
||||
<value>bankService</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* The Acegi Security System for Spring is published under the terms
|
||||
* of the Apache Software License.
|
||||
*
|
||||
* Visit http://acegisecurity.sourceforge.net for further details.
|
||||
*/
|
||||
|
||||
package sample.attributes;
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Cameron Braid
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*
|
||||
* @@SecurityConfig("ROLE_TELLER")
|
||||
*/
|
||||
public interface BankService {
|
||||
//~ Methods ================================================================
|
||||
|
||||
/**
|
||||
* The SecurityConfig below will be merged with the interface-level
|
||||
* SecurityConfig above by Commons Attributes. ie: this is equivalent to
|
||||
* defining BankService=ROLE_TELLER,ROLE_PERMISSION_BALANACE in the bean
|
||||
* context.
|
||||
*
|
||||
* @return DOCUMENT ME!
|
||||
*
|
||||
* @@SecurityConfig("ROLE_PERMISSION_BALANCE")
|
||||
*/
|
||||
public float balance(String accountNumber);
|
||||
|
||||
/**
|
||||
* The SecurityConfig below will be merged with the interface-level
|
||||
* SecurityConfig above by Commons Attributes. ie: this is equivalent to
|
||||
* defining BankService=ROLE_TELLER,ROLE_PERMISSION_LIST in the bean
|
||||
* context.
|
||||
*
|
||||
* @return DOCUMENT ME!
|
||||
*
|
||||
* @@SecurityConfig("ROLE_PERMISSION_LIST")
|
||||
*/
|
||||
public String[] listAccounts();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* The Acegi Security System for Spring is published under the terms
|
||||
* of the Apache Software License.
|
||||
*
|
||||
* Visit http://acegisecurity.sourceforge.net for further details.
|
||||
*/
|
||||
|
||||
package sample.attributes;
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Cameron Braid
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class BankServiceImpl implements BankService {
|
||||
//~ Methods ================================================================
|
||||
|
||||
public float balance(String accountNumber) {
|
||||
return 42000000;
|
||||
}
|
||||
|
||||
public String[] listAccounts() {
|
||||
return new String[] {"1", "2", "3"};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* The Acegi Security System for Spring is published under the terms
|
||||
* of the Apache Software License.
|
||||
*
|
||||
* Visit http://acegisecurity.sourceforge.net for further details.
|
||||
*/
|
||||
|
||||
package sample.attributes;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.AccessDeniedException;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* Tests security objects.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class BankTests extends TestCase {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private BankService service;
|
||||
private ClassPathXmlApplicationContext ctx;
|
||||
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public BankTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BankTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
service = (BankService) ctx.getBean("bankService");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(BankTests.class);
|
||||
}
|
||||
|
||||
public void testDeniedAccess() throws Exception {
|
||||
createSecureContext();
|
||||
|
||||
try {
|
||||
service.balance("1");
|
||||
fail("Should have thrown AccessDeniedException");
|
||||
} catch (AccessDeniedException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
destroySecureContext();
|
||||
}
|
||||
|
||||
public void testListAccounts() throws Exception {
|
||||
createSecureContext();
|
||||
service.listAccounts();
|
||||
destroySecureContext();
|
||||
}
|
||||
|
||||
private static void createSecureContext() {
|
||||
TestingAuthenticationToken auth = new TestingAuthenticationToken("test",
|
||||
"test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"), new GrantedAuthorityImpl("ROLE_PERMISSION_LIST")});
|
||||
|
||||
SecureContextImpl secureContext = new SecureContextImpl();
|
||||
secureContext.setAuthentication(auth);
|
||||
ContextHolder.setContext(secureContext);
|
||||
}
|
||||
|
||||
private static void destroySecureContext() {
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
}
|
||||
67
samples/attributes/src/main/java/sample/attributes/Main.java
Normal file
67
samples/attributes/src/main/java/sample/attributes/Main.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* The Acegi Security System for Spring is published under the terms
|
||||
* of the Apache Software License.
|
||||
*
|
||||
* Visit http://acegisecurity.sourceforge.net for further details.
|
||||
*/
|
||||
|
||||
package sample.attributes;
|
||||
|
||||
import net.sf.acegisecurity.AccessDeniedException;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author Cameron Braid
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class Main {
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
createSecureContext();
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
BankService service = (BankService) context.getBean("bankService");
|
||||
|
||||
// will succeed
|
||||
service.listAccounts();
|
||||
|
||||
// will fail
|
||||
try {
|
||||
System.out.println("We expect an AccessDeniedException now, as we do not hold the ROLE_PERMISSION_BALANCE granted authority, and we're using a unanimous access decision manager... ");
|
||||
service.balance("1");
|
||||
} catch (AccessDeniedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
destroySecureContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* This can be done in a web app by using a filter or
|
||||
* <code>SpringMvcIntegrationInterceptor</code>.
|
||||
*/
|
||||
private static void createSecureContext() {
|
||||
TestingAuthenticationToken auth = new TestingAuthenticationToken("test",
|
||||
"test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"), new GrantedAuthorityImpl("ROLE_PERMISSION_LIST")});
|
||||
|
||||
SecureContextImpl secureContext = new SecureContextImpl();
|
||||
secureContext.setAuthentication(auth);
|
||||
ContextHolder.setContext(secureContext);
|
||||
}
|
||||
|
||||
private static void destroySecureContext() {
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user