diff --git a/.classpath b/.classpath index 0c750a7693..5719d5cda4 100644 --- a/.classpath +++ b/.classpath @@ -59,5 +59,8 @@ + + + diff --git a/samples/annotations/.cvsignore b/samples/annotations/.cvsignore new file mode 100644 index 0000000000..7b85662386 --- /dev/null +++ b/samples/annotations/.cvsignore @@ -0,0 +1,4 @@ +classes +generated +reports +target diff --git a/samples/annotations/maven.xml b/samples/annotations/maven.xml new file mode 100644 index 0000000000..4ca6064af1 --- /dev/null +++ b/samples/annotations/maven.xml @@ -0,0 +1,6 @@ + + + + diff --git a/samples/annotations/project.properties b/samples/annotations/project.properties new file mode 100644 index 0000000000..cfae2ce8ea --- /dev/null +++ b/samples/annotations/project.properties @@ -0,0 +1,9 @@ +# $Id$ + +# Compile settings +# +# Java 1.5 is required due to the use of annotations for metadata. +# (main Acegi Security project / parent) is Java 1.3 compatible +# +maven.compile.target=1.5 +maven.compile.source=1.5 \ No newline at end of file diff --git a/samples/annotations/project.xml b/samples/annotations/project.xml new file mode 100644 index 0000000000..bad1e04533 --- /dev/null +++ b/samples/annotations/project.xml @@ -0,0 +1,23 @@ + + + ${basedir}/../project.xml + 3 + acegi-security-sample-annotations + Acegi Security System for Spring - Annotations sample + acegisecurity + /home/groups/a/ac/acegisecurity/htdocs/multiproject/acegi-security-sample-annotations + + scm:cvs:pserver:anonymous@cvs.sourceforge.net:/cvsroot/acegisecurity:acegisecurity + scm:cvs:ext:${maven.username}@cvs.sourceforge.net:/cvsroot/acegisecurity:acegisecurity + http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/acegisecurity/acegisecurity/samples/annotations/ + + + + acegisecurity + acegi-security-tiger + 0.9.0-SNAPSHOT + jar + + + + diff --git a/samples/annotations/src/main/java/sample/annotations/BankService.java b/samples/annotations/src/main/java/sample/annotations/BankService.java new file mode 100644 index 0000000000..7d8b4905bb --- /dev/null +++ b/samples/annotations/src/main/java/sample/annotations/BankService.java @@ -0,0 +1,52 @@ +/* Copyright 2004, 2005 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 sample.annotations; + +import net.sf.acegisecurity.annotation.Secured; + +/** + * BankService sample using Java 5 Annotations. + * + * @author Mark St.Godard + * @version $Id$ + * + * @see net.sf.acegisecurity.annotation.Secured + */ + +@Secured({"ROLE_TELLER" }) +public interface BankService { + //~ Methods ================================================================ + + /** + * Get the account balance. + * + * @param accountNumber The account number + * + * @return The balance + */ + + @Secured({"ROLE_PERMISSION_BALANCE" }) + public float balance(String accountNumber); + + /** + * List accounts + * + * @return The list of accounts + */ + + @Secured({"ROLE_PERMISSION_LIST" }) + public String[] listAccounts(); +} diff --git a/samples/annotations/src/main/java/sample/annotations/BankServiceImpl.java b/samples/annotations/src/main/java/sample/annotations/BankServiceImpl.java new file mode 100644 index 0000000000..2582bc5528 --- /dev/null +++ b/samples/annotations/src/main/java/sample/annotations/BankServiceImpl.java @@ -0,0 +1,34 @@ +/* Copyright 2004, 2005 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 sample.annotations; + +/** + * BankService sample implementation. + * + * @author Mark St.Godard + * @version $Id$ + */ +public class BankServiceImpl implements BankService { + //~ Methods ================================================================ + + public float balance(String accountNumber) { + return 42000000; + } + + public String[] listAccounts() { + return new String[] {"1", "2", "3"}; + } +} diff --git a/samples/annotations/src/main/java/sample/annotations/Main.java b/samples/annotations/src/main/java/sample/annotations/Main.java new file mode 100644 index 0000000000..3d40de9bf7 --- /dev/null +++ b/samples/annotations/src/main/java/sample/annotations/Main.java @@ -0,0 +1,60 @@ +package sample.annotations; + + +import net.sf.acegisecurity.AccessDeniedException; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.context.SecurityContextHolder; +import net.sf.acegisecurity.context.SecurityContextImpl; +import net.sf.acegisecurity.providers.TestingAuthenticationToken; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + * @author Mark St.Godard + * @version $Id$ + */ +public class Main { + //~ Methods ================================================================ + + public static void main(String[] args) throws Exception { + createSecureContext(); + + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "applicationContext-annotations.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 + * SpringMvcIntegrationInterceptor. + */ + private static void createSecureContext() { + TestingAuthenticationToken auth = new TestingAuthenticationToken("test", + "test", + new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"), new GrantedAuthorityImpl( + "ROLE_PERMISSION_LIST")}); + + SecurityContextHolder.getContext().setAuthentication(auth); + } + + private static void destroySecureContext() { + SecurityContextHolder.setContext(new SecurityContextImpl()); + } +} diff --git a/samples/annotations/src/main/resources/applicationContext-annotations.xml b/samples/annotations/src/main/resources/applicationContext-annotations.xml new file mode 100644 index 0000000000..186f26d03e --- /dev/null +++ b/samples/annotations/src/main/resources/applicationContext-annotations.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + my_run_as_password + + + + + + + + + + + + + + + + + + + + + + + + false + + + + + + + + + + + + + + + + + + false + + + + + + + + + + + + + + + + + + diff --git a/samples/annotations/src/test/java/samples/annotations/BankTests.java b/samples/annotations/src/test/java/samples/annotations/BankTests.java new file mode 100644 index 0000000000..2ba91ce54e --- /dev/null +++ b/samples/annotations/src/test/java/samples/annotations/BankTests.java @@ -0,0 +1,82 @@ +package samples.annotations; + +import junit.framework.TestCase; +import net.sf.acegisecurity.AccessDeniedException; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.context.SecurityContextHolder; +import net.sf.acegisecurity.context.SecurityContextImpl; +import net.sf.acegisecurity.providers.TestingAuthenticationToken; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import sample.annotations.BankService; + + +/** +* 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-annotations.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")}); + + SecurityContextHolder.getContext().setAuthentication(auth); + } + + private static void destroySecureContext() { + SecurityContextHolder.setContext(new SecurityContextImpl()); + } +} +