KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > security > Ejb3AuthenticationInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.security;
23
24 import java.security.GeneralSecurityException JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 import javax.security.auth.Subject JavaDoc;
30
31 import javax.ejb.EJBAccessException JavaDoc;
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 /**
49  * Comment
50  *
51  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
52  * @version $Revision: 55011 $
53  */

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 JavaDoc gse)
69    {
70       throw new EJBAccessException JavaDoc("Authentication failure", gse);
71    }
72
73    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
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 JavaDoc principal = (Principal JavaDoc)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 JavaDoc 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