1 22 package org.jboss.ejb.plugins; 23 24 import java.lang.reflect.Method ; 25 import java.security.CodeSource ; 26 import java.security.Policy ; 27 import java.security.Principal ; 28 import java.security.ProtectionDomain ; 29 import java.util.Set ; 30 import javax.security.auth.Subject ; 31 import javax.security.jacc.EJBMethodPermission ; 32 33 import org.jboss.ejb.Container; 34 import org.jboss.invocation.Invocation; 35 import org.jboss.metadata.BeanMetaData; 36 37 43 public class JaccAuthorizationInterceptor extends AbstractInterceptor 44 { 45 private Policy policy; 46 private String ejbName; 47 private CodeSource ejbCS; 48 49 52 public void setContainer(Container container) 53 { 54 super.setContainer(container); 55 if (container != null) 56 { 57 BeanMetaData beanMetaData = container.getBeanMetaData(); 58 ejbName = beanMetaData.getEjbName(); 59 ejbCS = container.getBeanClass().getProtectionDomain().getCodeSource(); 60 container.setJaccEnabled(true); 62 } 63 policy = Policy.getPolicy(); 64 } 65 66 public void start() throws Exception 68 { 69 super.start(); 70 } 71 72 public Object invokeHome(Invocation mi) throws Exception 73 { 74 checkSecurityAssociation(mi); 76 Object returnValue = getNext().invokeHome(mi); 77 return returnValue; 78 } 79 80 public Object invoke(Invocation mi) throws Exception 81 { 82 checkSecurityAssociation(mi); 84 Object returnValue = getNext().invoke(mi); 85 return returnValue; 86 } 87 88 90 private void checkSecurityAssociation(Invocation mi) 91 throws Exception 92 { 93 Method m = mi.getMethod(); 94 if( m == null ) 96 return; 97 98 String iface = mi.getType().toInterfaceString(); 99 EJBMethodPermission methodPerm = new EJBMethodPermission (ejbName, iface, m); 100 Subject caller = SecurityActions.getContextSubject(); 102 Principal [] principals = null; 103 if( caller != null ) 104 { 105 Set principalsSet = caller.getPrincipals(); 107 principals = new Principal [principalsSet.size()]; 108 principalsSet.toArray(principals); 109 } 110 ProtectionDomain pd = new ProtectionDomain (ejbCS, null, null, principals); 111 if( policy.implies(pd, methodPerm) == false ) 112 { 113 String msg = "Denied: "+methodPerm+", caller=" + caller; 114 SecurityException e = new SecurityException (msg); 115 throw e; 116 } 117 } 118 } 119 | Popular Tags |