1 22 package org.jboss.aspects.security; 23 24 import org.jboss.aop.joinpoint.Invocation; 25 import org.jboss.logging.Logger; 26 import org.jboss.security.AnybodyPrincipal; 27 import org.jboss.security.AuthenticationManager; 28 import org.jboss.security.NobodyPrincipal; 29 import org.jboss.security.RealmMapping; 30 import org.jboss.security.RunAsIdentity; 31 import org.jboss.security.SimplePrincipal; 32 33 import java.security.Principal ; 34 import java.util.HashSet ; 35 import java.util.Set ; 36 37 48 public class RoleBasedAuthorizationInterceptor implements org.jboss.aop.advice.Interceptor 49 { 50 protected Logger log = Logger.getLogger(this.getClass()); 51 protected AuthenticationManager securityManager; 52 protected RealmMapping realmMapping; 53 54 public RoleBasedAuthorizationInterceptor(AuthenticationManager manager, RealmMapping realmMapping) 55 { 56 this.securityManager = manager; 57 this.realmMapping = realmMapping; 58 } 59 60 public String getName() 61 { 62 return "RoleBasedAuthorizationInterceptor"; 63 } 64 65 protected Set getRoleSet(Invocation invocation) 66 { 67 Set roles = (Set ) invocation.getMetaData("security", "roles"); 68 if (roles == null) roles = getAnnotationRoleSet(invocation); 69 return roles; 70 71 } 72 73 protected Set getAnnotationRoleSet(Invocation invocation) 74 { 75 HashSet set = new HashSet (); 76 Exclude exclude = (Exclude) invocation.resolveAnnotation(Exclude.class); 77 if (exclude != null) 78 { 79 set.add(NobodyPrincipal.NOBODY_PRINCIPAL); 80 return set; 81 } 82 Unchecked unchecked = (Unchecked) invocation.resolveAnnotation(Unchecked.class); 83 if (unchecked != null) 84 { 85 set.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 86 return set; 87 } 88 Permissions permissions = (Permissions) invocation.resolveAnnotation(Permissions.class); 89 if (permissions == null) 90 { 91 set.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 93 return set; 94 } 95 for (int i = 0; i < permissions.value().length; i++) 96 { 97 set.add(new SimplePrincipal(permissions.value()[i])); 98 } 99 return set; 100 } 101 102 106 public Object invoke(Invocation invocation) throws Throwable 107 { 108 if (securityManager == null) 111 { 112 return invocation.invokeNext(); 113 } 114 115 if (realmMapping == null) 116 { 117 throw new SecurityException ("Role mapping manager has not been set"); 118 } 119 120 Set roles = getRoleSet(invocation); 121 if (roles == null) 122 { 123 129 String message = "No method permissions assigned."; 130 log.error(message); 131 throw new SecurityException (message); 132 } 133 134 RunAsIdentity callerRunAsIdentity = SecurityActions.peekRunAsIdentity(); 136 if (roles.contains(AnybodyPrincipal.ANYBODY_PRINCIPAL) == false) 137 { 138 if (callerRunAsIdentity == null) 140 { 141 Principal principal = SecurityActions.getPrincipal(); 142 if (realmMapping.doesUserHaveRole(principal, roles) == false) 144 { 145 Set userRoles = realmMapping.getUserRoles(principal); 146 String msg = "Insufficient permissions, principal=" + principal 147 + ", requiredRoles=" + roles + ", principalRoles=" + userRoles; 148 log.error(msg); 149 throw new SecurityException (msg); 150 } 151 } 152 153 else 155 { 156 if (callerRunAsIdentity.doesUserHaveRole(roles) == false) 158 { 159 String msg = "Insufficient permissions, runAsPrincipal=" + callerRunAsIdentity.getName() 160 + ", requiredRoles=" + roles + ", runAsRoles=" + callerRunAsIdentity.getRunAsRoles(); 161 log.error(msg); 162 throw new SecurityException (msg); 163 } 164 } 165 } 166 return invocation.invokeNext(); 167 } 168 169 } 170 | Popular Tags |