]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/plugins/org.argeo.security.ui.rap/src/main/java/org/argeo/security/ui/rap/SecureEntryPoint.java
Improve RAP security
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.rap / src / main / java / org / argeo / security / ui / rap / SecureEntryPoint.java
index d7dc18ca9c5dacdde74b134c465482763fd776b7..50f74e9c72fe6a06f7227f04b524577e14288b59 100644 (file)
@@ -1,25 +1,45 @@
 package org.argeo.security.ui.rap;
 
 import java.security.PrivilegedAction;
+import java.util.Set;
 
 import javax.security.auth.Subject;
 import javax.security.auth.login.LoginException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.eclipse.ui.dialogs.Error;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.rwt.RWT;
 import org.eclipse.rwt.lifecycle.IEntryPoint;
+import org.eclipse.rwt.service.SessionStoreEvent;
+import org.eclipse.rwt.service.SessionStoreListener;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
 import org.eclipse.ui.application.WorkbenchAdvisor;
 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
+import org.springframework.security.Authentication;
+import org.springframework.security.context.SecurityContextHolder;
 
-public class SecureEntryPoint implements IEntryPoint {
+public class SecureEntryPoint implements IEntryPoint, SessionStoreListener {
+       private Log log = LogFactory.getLog(SecureEntryPoint.class);
+
+       private final static String SECURITY_CONTEXT_ATTRIBUTE = "securityContextAttribute";
 
        @Override
        public int createUI() {
+//             log.debug("THREAD=" + Thread.currentThread().getId()
+//                             + ", RWT.getSessionStore().getId()="
+//                             + RWT.getSessionStore().getId());
+
+               Authentication authen = (Authentication) RWT.getSessionStore()
+                               .getAttribute(SECURITY_CONTEXT_ATTRIBUTE);
+               if (authen != null)
+                       SecurityContextHolder.getContext().setAuthentication(authen);
+
                Integer returnCode = null;
                Display display = PlatformUI.createDisplay();
                try {
@@ -27,9 +47,18 @@ public class SecureEntryPoint implements IEntryPoint {
                        Boolean retry = true;
                        while (retry) {
                                try {
-                                       SecureRapActivator.getLoginContext().login();
-                                       subject = SecureRapActivator.getLoginContext()
-                                                       .getSubject();
+                                       // if (authen == null)
+                                       // SecureRapActivator.getLoginContext().login();
+                                       subject = SecureRapActivator.getLoginContext().getSubject();
+                                       Set<Authentication> auths = subject
+                                                       .getPrincipals(Authentication.class);
+                                       if (auths.size() > 0)
+                                               SecurityContextHolder.getContext().setAuthentication(
+                                                               auths.iterator().next());
+                                       // authen = SecurityContextHolder.getContext()
+                                       // .getAuthentication();
+                                       // RWT.getSessionStore().setAttribute(
+                                       // SECURITY_CONTEXT_ATTRIBUTE, authen);
                                        retry = false;
                                } catch (LoginException e) {
                                        Error.show("Cannot login", e);
@@ -99,4 +128,13 @@ public class SecureEntryPoint implements IEntryPoint {
                };
        }
 
+       @Override
+       public void beforeDestroy(SessionStoreEvent event) {
+               if (log.isDebugEnabled())
+                       log.debug("RWT session " + event.getSessionStore().getId()
+                                       + " about to be destroyed. THREAD="
+                                       + Thread.currentThread().getId());
+
+       }
+
 }