1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.intercept.method;
17
18 import org.acegisecurity.AccessDeniedException;
19 import org.acegisecurity.Authentication;
20 import org.acegisecurity.ConfigAttributeDefinition;
21
22 import org.acegisecurity.intercept.AbstractSecurityInterceptor;
23
24 import org.aopalliance.intercept.MethodInvocation;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.beans.factory.InitializingBean;
30
31 import org.springframework.util.Assert;
32
33
34
35
36
37
38
39
40
41
42
43
44 public class MethodInvocationPrivilegeEvaluator implements InitializingBean {
45
46
47 protected static final Log logger = LogFactory.getLog(MethodInvocationPrivilegeEvaluator.class);
48
49
50
51 private AbstractSecurityInterceptor securityInterceptor;
52
53
54
55 public void afterPropertiesSet() throws Exception {
56 Assert.notNull(securityInterceptor, "SecurityInterceptor required");
57 }
58
59 public boolean isAllowed(MethodInvocation mi, Authentication authentication) {
60 Assert.notNull(mi, "MethodInvocation required");
61 Assert.notNull(mi.getMethod(), "MethodInvocation must provide a non-null getMethod()");
62
63 ConfigAttributeDefinition attrs = securityInterceptor.obtainObjectDefinitionSource().getAttributes(mi);
64
65 if (attrs == null) {
66 if (securityInterceptor.isRejectPublicInvocations()) {
67 return false;
68 }
69
70 return true;
71 }
72
73 if ((authentication == null) || (authentication.getAuthorities() == null)
74 || (authentication.getAuthorities().length == 0)) {
75 return false;
76 }
77
78 try {
79 securityInterceptor.getAccessDecisionManager().decide(authentication, mi, attrs);
80 } catch (AccessDeniedException unauthorized) {
81 if (logger.isDebugEnabled()) {
82 logger.debug(mi.toString() + " denied for " + authentication.toString(), unauthorized);
83 }
84
85 return false;
86 }
87
88 return true;
89 }
90
91 public void setSecurityInterceptor(AbstractSecurityInterceptor securityInterceptor) {
92 Assert.notNull(securityInterceptor, "AbstractSecurityInterceptor cannot be null");
93 Assert.isTrue(MethodInvocation.class.equals(securityInterceptor.getSecureObjectClass()),
94 "AbstractSecurityInterceptor does not support MethodInvocations");
95 Assert.notNull(securityInterceptor.getAccessDecisionManager(),
96 "AbstractSecurityInterceptor must provide a non-null AccessDecisionManager");
97 this.securityInterceptor = securityInterceptor;
98 }
99 }