]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/auth/AbstractLoginModule.java
- Improve CMS login (HTTP session now supported)
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / auth / AbstractLoginModule.java
index f464ebad9c37669f16c41c28c078f36b5441fe0b..baf6b63175eb3b952bedee3cca530e2d00103196 100644 (file)
@@ -77,17 +77,25 @@ public abstract class AbstractLoginModule implements LoginModule {
                        Authentication currentAuth = SecurityContextHolder.getContext()
                                        .getAuthentication();
 
-                       if (currentAuth == null && Display.getCurrent() != null) {
-                               // try to load authentication from session
-                               HttpServletRequest httpRequest = RWT.getRequest();
-                               HttpSession httpSession = httpRequest.getSession();
-                               // log.debug(httpSession.getId());
-                               Object contextFromSessionObject = httpSession
-                                               .getAttribute(SPRING_SECURITY_CONTEXT_KEY);
-                               if (contextFromSessionObject != null) {
-                                       currentAuth = (Authentication) contextFromSessionObject;
-                                       SecurityContextHolder.getContext().setAuthentication(
-                                                       currentAuth);
+                       if (currentAuth == null) {
+                               // Pre-auth
+                               // TODO Do it at Spring Security level?
+                               try {
+                                       // try to load authentication from session
+                                       HttpServletRequest httpRequest = RWT.getRequest();
+                                       HttpSession httpSession = httpRequest.getSession();
+                                       // log.debug(httpSession.getId());
+                                       Object contextFromSessionObject = httpSession
+                                                       .getAttribute(SPRING_SECURITY_CONTEXT_KEY);
+                                       if (contextFromSessionObject != null) {
+                                               currentAuth = (Authentication) contextFromSessionObject;
+                                               SecurityContextHolder.getContext().setAuthentication(
+                                                               currentAuth);
+                                       }
+                               } catch (Exception e) {
+                                       if (log.isTraceEnabled())
+                                               log.trace("Could not get session", e);
+                                       // silent
                                }
                        }
 
@@ -118,12 +126,16 @@ public abstract class AbstractLoginModule implements LoginModule {
                                SecurityContext securityContext = SecurityContextHolder
                                                .getContext();
                                securityContext.setAuthentication(authentication);
-                               if (Display.getCurrent() != null) {
+                               try {
                                        HttpServletRequest httpRequest = RWT.getRequest();
                                        HttpSession httpSession = httpRequest.getSession();
                                        if (httpSession.getAttribute(SPRING_SECURITY_CONTEXT_KEY) == null)
                                                httpSession.setAttribute(SPRING_SECURITY_CONTEXT_KEY,
                                                                authentication);
+                               } catch (Exception e) {
+                                       if (log.isTraceEnabled())
+                                               log.trace("Could not add security context to session",
+                                                               e);
                                }
                                return true;
                        } else {
@@ -152,6 +164,8 @@ public abstract class AbstractLoginModule implements LoginModule {
                        HttpSession httpSession = httpRequest.getSession();
                        if (httpSession.getAttribute(SPRING_SECURITY_CONTEXT_KEY) != null)
                                httpSession.setAttribute(SPRING_SECURITY_CONTEXT_KEY, null);
+                       // expire session
+                       httpSession.setMaxInactiveInterval(0);
                }
                return true;
        }