From b7fd6f22cd58401024391c53ff1887e4eb2710b1 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 28 Apr 2011 04:50:59 +0000 Subject: [PATCH] Improve authenticated application context initialization ASSIGNED - bug 17: Generalize agent management and registration beyond JMS https://bugzilla.argeo.org/show_bug.cgi?id=17 git-svn-id: https://svn.argeo.org/commons/trunk@4481 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../core/AbstractSystemExecution.java | 9 ++++++- ...atedApplicationContextInitialization.java} | 26 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) rename security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/{SystemExecutionBeanPostProcessor.java => AuthenticatedApplicationContextInitialization.java} (65%) diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AbstractSystemExecution.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AbstractSystemExecution.java index 23a111b94..856ceee60 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AbstractSystemExecution.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AbstractSystemExecution.java @@ -68,11 +68,18 @@ public abstract class AbstractSystemExecution { if (securityContext.getAuthentication() != null) { securityContext.setAuthentication(null); authenticatedBySelf.set(false); - if (log.isTraceEnabled()) + if (log.isTraceEnabled()) { log.trace("System deauthenticated"); + // Thread.dumpStack(); + } } } + /** Whether the current thread was authenticated by this component. */ + protected Boolean isAuthenticatedBySelf() { + return authenticatedBySelf.get(); + } + public void setAuthenticationManager( AuthenticationManager authenticationManager) { this.authenticationManager = authenticationManager; diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/SystemExecutionBeanPostProcessor.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AuthenticatedApplicationContextInitialization.java similarity index 65% rename from security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/SystemExecutionBeanPostProcessor.java rename to security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AuthenticatedApplicationContextInitialization.java index a2086bb9c..51d4b1929 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/SystemExecutionBeanPostProcessor.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/AuthenticatedApplicationContextInitialization.java @@ -2,6 +2,8 @@ package org.argeo.security.core; import java.beans.PropertyDescriptor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; @@ -13,13 +15,24 @@ import org.springframework.context.event.ContextRefreshedEvent; * Executes with a system authentication the instantiation and initialization * methods of the application context where it has been defined. */ -public class SystemExecutionBeanPostProcessor extends AbstractSystemExecution - implements InstantiationAwareBeanPostProcessor, ApplicationListener { +public class AuthenticatedApplicationContextInitialization extends + AbstractSystemExecution implements InstantiationAwareBeanPostProcessor, + ApplicationListener { + private Log log = LogFactory + .getLog(AuthenticatedApplicationContextInitialization.class); @SuppressWarnings("rawtypes") public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { - authenticateAsSystem(); + // we authenticate when any beans is instantiated + // we will deauthenticate only when the application context has been + // refreshed in order to be able to deal with factory beans has well + if (!isAuthenticatedBySelf()) { + authenticateAsSystem(); + if (log.isDebugEnabled()) + log.debug("Application context initialization authenticated for thread " + + Thread.currentThread().getName()); + } return null; } @@ -36,7 +49,7 @@ public class SystemExecutionBeanPostProcessor extends AbstractSystemExecution public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - authenticateAsSystem(); + // authenticateAsSystem(); return bean; } @@ -46,7 +59,7 @@ public class SystemExecutionBeanPostProcessor extends AbstractSystemExecution // we expect the underlying thread to die and thus the system // authentication to be lost. We have currently no way to catch the // exception and perform the deauthentication by ourselves. - deauthenticateAsSystem(); + // deauthenticateAsSystem(); return bean; } @@ -55,6 +68,9 @@ public class SystemExecutionBeanPostProcessor extends AbstractSystemExecution // make sure that we have deauthenticated after the application // context was initialized/refreshed deauthenticateAsSystem(); + if (log.isDebugEnabled()) + log.debug("Application context initialization deauthenticated for thread " + + Thread.currentThread().getName()); } } -- 2.30.2