001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.security;
023:
024: import java.security.GeneralSecurityException;
025: import java.security.Principal;
026:
027: import javax.ejb.EJBAccessException;
028:
029: import org.jboss.ejb3.Container;
030: import org.jboss.ejb3.EJBContainer;
031: import org.jboss.logging.Logger;
032:
033: import org.jboss.annotation.security.SecurityDomain;
034: import org.jboss.aop.joinpoint.MethodInvocation;
035:
036: import org.jboss.aspects.security.AuthenticationInterceptor;
037: import org.jboss.aspects.security.SecurityContext;
038: import org.jboss.security.AuthenticationManager;
039: import org.jboss.security.RealmMapping;
040: import org.jboss.security.SecurityAssociation;
041: import org.jboss.security.SecurityRolesAssociation;
042: import org.jboss.security.SimplePrincipal;
043:
044: /**
045: * Authentication Interceptor
046: *
047: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
048: * @author Anil.Saldhana@jboss.org
049: * @version $Revision: 62545 $
050: */
051: public class Ejb3AuthenticationInterceptor extends
052: AuthenticationInterceptor {
053: private static final Logger log = Logger
054: .getLogger(Ejb3AuthenticationInterceptor.class);
055:
056: private EJBContainer container;
057: protected RealmMapping realmMapping;
058:
059: public Ejb3AuthenticationInterceptor(AuthenticationManager manager,
060: Container container) {
061: super (manager);
062: this .container = (EJBContainer) container;
063: this .realmMapping = (RealmMapping) manager;
064: }
065:
066: protected void handleGeneralSecurityException(
067: GeneralSecurityException gse) {
068: log.debug("Authentication failure", gse);
069: throw new EJBAccessException("Authentication failure");
070: }
071:
072: public Object invoke(org.jboss.aop.joinpoint.Invocation invocation)
073: throws Throwable {
074: MethodInvocation mi = (MethodInvocation) invocation;
075: SecurityDomain domain = (SecurityDomain) container
076: .resolveAnnotation(SecurityDomain.class);
077:
078: if (domain != null && domain.unauthenticatedPrincipal() != null
079: && domain.unauthenticatedPrincipal().length() != 0) {
080: Principal principal = (Principal) invocation.getMetaData(
081: "security", "principal");
082: if (principal == null)
083: principal = SecurityAssociation.getPrincipal();
084:
085: if (principal == null) {
086: invocation.getMetaData().addMetaData(
087: "security",
088: "principal",
089: new SimplePrincipal(domain
090: .unauthenticatedPrincipal()));
091:
092: Object oldDomain = SecurityContext.getCurrentDomain()
093: .get();
094:
095: try {
096: SecurityContext.getCurrentDomain().set(
097: authenticationManager);
098: return invocation.invokeNext();
099: } finally {
100: SecurityContext.getCurrentDomain().set(oldDomain);
101: }
102: }
103: }
104: try {
105: //Set a map of principal-roles that may be configured at deployment level
106: if (container.getAssemblyDescriptor() != null) {
107: SecurityRolesAssociation.setSecurityRoles(container
108: .getAssemblyDescriptor()
109: .getPrincipalVersusRolesMap());
110: }
111: return super.invoke(invocation);
112: } finally {
113: SecurityRolesAssociation.setSecurityRoles(null);
114: }
115: }
116: }
|