1 22 package org.jboss.ejb3.security; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import javax.annotation.security.DenyAll; 28 import javax.annotation.security.PermitAll; 29 import javax.annotation.security.RolesAllowed; 30 import javax.ejb.EJBAccessException ; 31 import org.jboss.aop.joinpoint.Invocation; 32 import org.jboss.aop.joinpoint.MethodInvocation; 33 import org.jboss.ejb3.Container; 34 import org.jboss.ejb3.EJBContainer; 35 import org.jboss.logging.Logger; 36 import org.jboss.security.AnybodyPrincipal; 37 import org.jboss.security.AuthenticationManager; 38 import org.jboss.security.NobodyPrincipal; 39 import org.jboss.security.RealmMapping; 40 import org.jboss.security.SimplePrincipal; 41 42 50 public final class RoleBasedAuthorizationInterceptor extends org.jboss.aspects.security.RoleBasedAuthorizationInterceptor 51 { 52 private static final Logger log = Logger.getLogger(RoleBasedAuthorizationInterceptor.class); 53 54 private EJBContainer container; 55 56 public RoleBasedAuthorizationInterceptor(AuthenticationManager manager, RealmMapping realmMapping, Container container) 57 { 58 super(manager, realmMapping); 59 this.container = (EJBContainer)container; 60 } 61 62 protected Set getRoleSet(Invocation invocation) 63 { 64 Method method = ((MethodInvocation)invocation).getActualMethod(); 65 66 Class [] classes = new Class []{DenyAll.class, PermitAll.class, RolesAllowed.class}; 67 68 Object annotation = container.resolveAnnotation(method, classes); 69 70 int classIndex = 0; 71 while (annotation == null && classIndex < 3) 72 { 73 annotation = container.resolveAnnotation(classes[classIndex++]); 74 } 75 76 HashSet set = new HashSet (); 77 if (annotation != null) 78 { 79 if (annotation instanceof DenyAll) 80 { 81 set.add(NobodyPrincipal.NOBODY_PRINCIPAL); 82 } 83 else if (annotation instanceof PermitAll) 84 { 85 set.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 86 } 87 else if (annotation instanceof RolesAllowed) 88 { 89 RolesAllowed permissions = (RolesAllowed) annotation; 90 for (int i = 0; i < permissions.value().length; i++) 91 { 92 set.add(new SimplePrincipal(permissions.value()[i])); 93 } 94 } 95 else 96 set.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 97 } 98 else 99 set.add(AnybodyPrincipal.ANYBODY_PRINCIPAL); 100 101 return set; 102 } 103 104 public Object invoke(Invocation invocation) throws Throwable 105 { 106 try 107 { 108 return super.invoke(invocation); 109 } 110 catch (SecurityException throwable) 111 { 112 throw new EJBAccessException ("Authorization failure", throwable); 113 } finally { 114 } 115 } 116 117 } 118 | Popular Tags |