01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package sample.annotations;
17:
18: import org.acegisecurity.AccessDeniedException;
19: import org.acegisecurity.GrantedAuthority;
20: import org.acegisecurity.GrantedAuthorityImpl;
21:
22: import org.acegisecurity.context.SecurityContextHolder;
23: import org.acegisecurity.context.SecurityContextImpl;
24:
25: import org.acegisecurity.providers.TestingAuthenticationToken;
26:
27: import org.springframework.context.support.ClassPathXmlApplicationContext;
28:
29: /**
30: *
31: DOCUMENT ME!
32: *
33: * @author Mark St.Godard
34: * @version $Id: Main.java 1496 2006-05-23 13:38:33Z benalex $
35: */
36: public class Main {
37: //~ Methods ========================================================================================================
38:
39: /**
40: * This can be done in a web app by using a filter or <code>SpringMvcIntegrationInterceptor</code>.
41: */
42: private static void createSecureContext() {
43: TestingAuthenticationToken auth = new TestingAuthenticationToken(
44: "test",
45: "test",
46: new GrantedAuthority[] {
47: new GrantedAuthorityImpl("ROLE_TELLER"),
48: new GrantedAuthorityImpl("ROLE_PERMISSION_LIST") });
49:
50: SecurityContextHolder.getContext().setAuthentication(auth);
51: }
52:
53: private static void destroySecureContext() {
54: SecurityContextHolder.setContext(new SecurityContextImpl());
55: }
56:
57: public static void main(String[] args) throws Exception {
58: createSecureContext();
59:
60: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
61: "applicationContext-annotations.xml");
62: BankService service = (BankService) context
63: .getBean("bankService");
64:
65: // will succeed
66: service.listAccounts();
67:
68: // will fail
69: try {
70: System.out
71: .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... ");
72: service.balance("1");
73: } catch (AccessDeniedException e) {
74: e.printStackTrace();
75: }
76:
77: destroySecureContext();
78: }
79: }
|