]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms.ui/src/org/argeo/cms/ui/AbstractCmsEntryPoint.java
Improve and clean authentication. Reintroduce anonymous login context.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / AbstractCmsEntryPoint.java
index 9f7811aa4d0d08a5c57919c3eb9a03b11fc29e6e..bea7117a6c6e3737e8fb1aec82797164f0462ba1 100644 (file)
@@ -12,7 +12,6 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.security.auth.Subject;
-import javax.security.auth.login.CredentialNotFoundException;
 import javax.security.auth.login.LoginContext;
 import javax.security.auth.login.LoginException;
 import javax.servlet.http.HttpServletRequest;
@@ -20,6 +19,7 @@ import javax.servlet.http.HttpServletRequest;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
+import org.argeo.cms.auth.CurrentUser;
 import org.argeo.cms.auth.HttpRequestCallbackHandler;
 import org.argeo.eclipse.ui.specific.UiContext;
 import org.argeo.jcr.JcrUtils;
@@ -73,17 +73,15 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint implement
                // Initial login
                try {
                        loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
-                                       new HttpRequestCallbackHandler(UiContext.getHttpRequest()));
+                                       new HttpRequestCallbackHandler(UiContext.getHttpRequest(), UiContext.getHttpResponse()));
                        loginContext.login();
-               } catch (CredentialNotFoundException e) {
+               } catch (LoginException e) {
                        try {
-                               loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER);
+                               loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS);
                                loginContext.login();
                        } catch (LoginException e1) {
                                throw new CmsException("Cannot log in as anonymous", e1);
                        }
-               } catch (LoginException e) {
-                       throw new CmsException("Cannot initialize subject", e);
                }
                authChange(loginContext);
 
@@ -174,12 +172,13 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint implement
                if (loginContext == null)
                        throw new CmsException("Login context should not be null");
                try {
+                       CurrentUser.logoutCmsSession(loginContext.getSubject());
                        loginContext.logout();
-                       LoginContext anonymousLc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER);
+                       LoginContext anonymousLc = new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS);
                        anonymousLc.login();
                        authChange(anonymousLc);
                } catch (LoginException e) {
-                       throw new CmsException("Cannot logout", e);
+                       log.error("Cannot logout", e);
                }
        }