1 22 package org.jboss.ejb3.security; 23 24 import java.security.GeneralSecurityException ; 25 import java.security.Principal ; 26 import java.util.Set ; 27 import java.util.HashSet ; 28 29 import javax.security.auth.Subject ; 30 31 import javax.ejb.EJBAccessException ; 32 33 import org.jboss.ejb3.Container; 34 import org.jboss.ejb3.EJBContainer; 35 import org.jboss.logging.Logger; 36 37 import org.jboss.annotation.security.SecurityDomain; 38 import org.jboss.aop.joinpoint.MethodInvocation; 39 40 import org.jboss.aspects.security.AuthenticationInterceptor; 41 import org.jboss.aspects.security.SecurityContext; 42 import org.jboss.security.AuthenticationManager; 43 import org.jboss.security.RealmMapping; 44 import org.jboss.security.RunAsIdentity; 45 import org.jboss.security.SecurityAssociation; 46 import org.jboss.security.SimplePrincipal; 47 48 54 public class Ejb3AuthenticationInterceptor extends AuthenticationInterceptor 55 { 56 private static final Logger log = Logger.getLogger(Ejb3AuthenticationInterceptor.class); 57 58 private EJBContainer container; 59 protected RealmMapping realmMapping; 60 61 public Ejb3AuthenticationInterceptor(AuthenticationManager manager, Container container) 62 { 63 super(manager); 64 this.container = (EJBContainer)container; 65 this.realmMapping = (RealmMapping)manager; 66 } 67 68 protected void handleGeneralSecurityException(GeneralSecurityException gse) 69 { 70 throw new EJBAccessException ("Authentication failure", gse); 71 } 72 73 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable 74 { 75 MethodInvocation mi = (MethodInvocation)invocation; 76 SecurityDomain domain = (SecurityDomain)container.resolveAnnotation(SecurityDomain.class); 77 78 if (domain != null && domain.unauthenticatedPrincipal() != null && domain.unauthenticatedPrincipal().length() != 0) 79 { 80 Principal principal = (Principal )invocation.getMetaData("security", "principal"); 81 if (principal == null) 82 principal = SecurityAssociation.getPrincipal(); 83 84 if (principal == null) 85 { 86 invocation.getMetaData().addMetaData("security", "principal", new SimplePrincipal(domain.unauthenticatedPrincipal())); 87 88 Object oldDomain = SecurityContext.getCurrentDomain().get(); 89 90 try 91 { 92 SecurityContext.getCurrentDomain().set(authenticationManager); 93 return invocation.invokeNext(); 94 } 95 finally 96 { 97 SecurityContext.getCurrentDomain().set(oldDomain); 98 } 99 } 100 } 101 102 return super.invoke(invocation); 103 } 104 } 105 | Popular Tags |