Initial commit.
This commit is contained in:
4
samples/attributes/.cvsignore
Normal file
4
samples/attributes/.cvsignore
Normal file
@@ -0,0 +1,4 @@
|
||||
classes
|
||||
generated
|
||||
reports
|
||||
|
||||
119
samples/attributes/build.xml
Normal file
119
samples/attributes/build.xml
Normal file
@@ -0,0 +1,119 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<!--
|
||||
Build file for the "attributes" sample application.
|
||||
|
||||
Demonstrates how to compile an application that has the security
|
||||
configuration defined by Commons Attributes in its Java source code.
|
||||
|
||||
$Id$
|
||||
-->
|
||||
|
||||
<project name="attributes" default="all" basedir=".">
|
||||
|
||||
<property file="build.properties"/>
|
||||
<property file="project.properties"/>
|
||||
|
||||
<path id="attribute-compiler-classpath">
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="**/commons-attributes-compiler-SNAPSHOT.jar"/>
|
||||
<include name="**/commons-collections.jar"/>
|
||||
<include name="**/xjavadoc-1.0.jar"/>
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
<path id="qa-portalpath">
|
||||
<pathelement location="classes"/>
|
||||
<fileset dir="${dist.lib.dir}">
|
||||
<include name="acegi-security.jar"/>
|
||||
</fileset>
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="**/spring.jar"/>
|
||||
<include name="**/aopalliance.jar"/>
|
||||
<include name="**/commons-logging.jar"/>
|
||||
<include name="**/commons-attributes-api-SNAPSHOT.jar"/>
|
||||
<include name="**/commons-collections.jar"/>
|
||||
<include name="**/xml-apis.jar"/>
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
<path id="jalopy-classpath">
|
||||
<fileset dir="${lib.dir}/jalopy">
|
||||
<include name="**/*.jar"/>
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
<target name="attribute-compiler" description="Generate Commons Attributes sources from original sources">
|
||||
<taskdef name='attribute-compiler'
|
||||
classname="org.apache.commons.attributes.compiler.AttributeCompiler"
|
||||
classpathref="attribute-compiler-classpath"/>
|
||||
<attribute-compiler
|
||||
destdir="${src.generated.dir}"
|
||||
attributePackages="net.sf.acegisecurity">
|
||||
<fileset dir="${src.dir}"/>
|
||||
</attribute-compiler>
|
||||
</target>
|
||||
|
||||
<target name="compile-classes" description="Compile generated and original sources">
|
||||
<mkdir dir="${build.dir}"/>
|
||||
<javac destdir="${build.dir}" classpathref="qa-portalpath">
|
||||
<src path="${src.dir}"/>
|
||||
<src path="${src.generated.dir}"/>
|
||||
</javac>
|
||||
<copy todir="${build.dir}">
|
||||
<fileset dir="${src.dir}">
|
||||
<include name="*.xml"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
</target>
|
||||
|
||||
<target name="format" description="Formats all project source code">
|
||||
<taskdef name="jalopy" classname="de.hunsicker.jalopy.plugin.ant.AntPlugin">
|
||||
<classpath refid="jalopy-classpath"/>
|
||||
</taskdef>
|
||||
|
||||
<jalopy fileformat="unix"
|
||||
convention="${jalopy.xml}"
|
||||
history="file"
|
||||
historymethod="adler32"
|
||||
loglevel="error"
|
||||
threads="2"
|
||||
classpathref="qa-portalpath">
|
||||
<fileset dir="${src.dir}">
|
||||
<include name="**/*.java"/>
|
||||
</fileset>
|
||||
</jalopy>
|
||||
</target>
|
||||
|
||||
<target name="tests" depends="compile-classes" description="Run tests">
|
||||
|
||||
<delete dir="${reports.dir}"/>
|
||||
<mkdir dir="${reports.dir}"/>
|
||||
|
||||
<junit printsummary="yes" haltonfailure="yes">
|
||||
<classpath location="${build.dir}"/>
|
||||
<classpath refid="qa-portalpath"/>
|
||||
<formatter type="plain"/>
|
||||
|
||||
<batchtest fork="yes" todir="${reports.dir}">
|
||||
<fileset dir="${build.dir}" includes="${test.includes}" excludes="${test.excludes}"/>
|
||||
</batchtest>
|
||||
</junit>
|
||||
|
||||
</target>
|
||||
|
||||
<target name="clean" description="Clean output dirs (generated, classes, reports)">
|
||||
<delete dir="${src.generated.dir}"/>
|
||||
<delete dir="${build.dir}"/>
|
||||
<delete dir="${reports.dir}"/>
|
||||
</target>
|
||||
|
||||
<target name="all" depends="clean, attribute-compiler, compile-classes, tests" description="Builds from scratch and runs tests"/>
|
||||
|
||||
<target name="execute" description="Runs application (assumes has been built)">
|
||||
<java fork="true" qa-portalpathref="qa-portalpath" classname="sample.attributes.Main"/>
|
||||
</target>
|
||||
|
||||
<target name="release" depends="all" description="Builds a clean release file"/>
|
||||
|
||||
</project>
|
||||
13
samples/attributes/project.properties
Normal file
13
samples/attributes/project.properties
Normal file
@@ -0,0 +1,13 @@
|
||||
# Ant properties for building the Attributes sample application.
|
||||
# $Id$
|
||||
|
||||
name=attributes
|
||||
src.dir=src
|
||||
src.generated.dir=generated
|
||||
lib.dir=${basedir}/../../lib
|
||||
dist.lib.dir=${basedir}/../../dist
|
||||
build.dir=classes
|
||||
jalopy.xml=${basedir}/../../jalopy.xml
|
||||
reports.dir=reports
|
||||
test.includes=**/*TestSuite.class **/*Tests.class
|
||||
test.excludes=**/Abstract*
|
||||
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