浏览代码

SEC-1619: Added check in GAE sample for change of Google user while still logged into the app.

Also updated GAE version and build script. Uploading to GAE now works when run from the gradle build file using the command 'gradle gaeDeploy'.
Luke Taylor 15 年之前
父节点
当前提交
37810a19c4

+ 1 - 1
buildSrc/build.gradle

@@ -30,7 +30,7 @@ dependencies {
 
 
 // GAE
 // GAE
 dependencies {
 dependencies {
-    compile 'com.google.appengine:appengine-tools-api:1.3.5'
+    compile 'com.google.appengine:appengine-tools-api:1.3.7'
 }
 }
 
 
 task ide(type: Copy)  {
 task ide(type: Copy)  {

+ 1 - 1
buildSrc/src/main/groovy/gae/GaePlugin.groovy

@@ -20,7 +20,7 @@ class GaePlugin implements Plugin<Project> {
         project.gaeDeploy.dependsOn project.war
         project.gaeDeploy.dependsOn project.war
 
 
         project.war.doLast {
         project.war.doLast {
-          ant.unzip(src: project.war.archivePath, dest: explodedWar)
+            ant.unzip(src: project.war.archivePath, dest: explodedWar)
         }
         }
     }
     }
 }
 }

+ 4 - 3
samples/gae/gae.gradle

@@ -2,7 +2,7 @@ apply plugin: 'war'
 apply plugin: 'jetty'
 apply plugin: 'jetty'
 apply plugin: 'gae'
 apply plugin: 'gae'
 
 
-gaeVersion="1.3.5"
+gaeVersion="1.3.7"
 
 
 repositories {
 repositories {
     // Hibernate Validator
     // Hibernate Validator
@@ -15,8 +15,7 @@ repositories {
 configurations.runtime.exclude(group: 'ch.qos.logback')
 configurations.runtime.exclude(group: 'ch.qos.logback')
 
 
 dependencies {
 dependencies {
-    providedCompile 'javax.servlet:servlet-api:2.5@jar',
-                    "com.google.appengine:appengine-api-1.0-sdk:$gaeVersion"
+    providedCompile 'javax.servlet:servlet-api:2.5@jar'
 
 
     compile project(':spring-security-core'),
     compile project(':spring-security-core'),
             project(':spring-security-web'),
             project(':spring-security-web'),
@@ -25,11 +24,13 @@ dependencies {
             "org.springframework:spring-webmvc:$springVersion",
             "org.springframework:spring-webmvc:$springVersion",
             "org.springframework:spring-context:$springVersion",
             "org.springframework:spring-context:$springVersion",
             "org.springframework:spring-context-support:$springVersion",
             "org.springframework:spring-context-support:$springVersion",
+            "com.google.appengine:appengine-api-1.0-sdk:$gaeVersion",
             'javax.validation:validation-api:1.0.0.GA',
             'javax.validation:validation-api:1.0.0.GA',
             'org.hibernate:hibernate-validator:4.1.0.Final',
             'org.hibernate:hibernate-validator:4.1.0.Final',
             "org.slf4j:slf4j-api:$slf4jVersion"
             "org.slf4j:slf4j-api:$slf4jVersion"
 
 
     runtime project(':spring-security-config'),
     runtime project(':spring-security-config'),
+            project(':spring-security-taglibs'),
             "org.slf4j:jcl-over-slf4j:$slf4jVersion",
             "org.slf4j:jcl-over-slf4j:$slf4jVersion",
             "org.slf4j:slf4j-jdk14:$slf4jVersion"
             "org.slf4j:slf4j-jdk14:$slf4jVersion"
     testCompile "com.google.appengine:appengine-testing:$gaeVersion"
     testCompile "com.google.appengine:appengine-testing:$gaeVersion"

+ 26 - 2
samples/gae/src/main/java/samples/gae/security/GaeAuthenticationFilter.java

@@ -24,6 +24,7 @@ import org.springframework.security.web.authentication.WebAuthenticationDetailsS
 import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
 import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
 import org.springframework.web.filter.GenericFilterBean;
 import org.springframework.web.filter.GenericFilterBean;
+import samples.gae.users.GaeUser;
 
 
 /**
 /**
  * @author Luke Taylor
  * @author Luke Taylor
@@ -39,10 +40,15 @@ public class GaeAuthenticationFilter extends GenericFilterBean {
 
 
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        User googleUser = UserServiceFactory.getUserService().getCurrentUser();
 
 
-        if (authentication == null) {
-            User googleUser = UserServiceFactory.getUserService().getCurrentUser();
+        if (authentication != null && !loggedInUserMatchesGaeUser(authentication, googleUser)) {
+            SecurityContextHolder.clearContext();
+            authentication = null;
+            ((HttpServletRequest)request).getSession().invalidate();
+        }
 
 
+        if (authentication == null) {
             if (googleUser != null) {
             if (googleUser != null) {
                 logger.debug("Currently logged on to GAE as user " + googleUser);
                 logger.debug("Currently logged on to GAE as user " + googleUser);
                 logger.debug("Authenticating to Spring Security");
                 logger.debug("Authenticating to Spring Security");
@@ -72,6 +78,24 @@ public class GaeAuthenticationFilter extends GenericFilterBean {
         chain.doFilter(request, response);
         chain.doFilter(request, response);
     }
     }
 
 
+    private boolean loggedInUserMatchesGaeUser(Authentication authentication, User googleUser) {
+        assert authentication != null;
+
+        if (googleUser == null) {
+            // User has logged out of GAE but is still logged into application
+            return false;
+        }
+
+        GaeUser gaeUser = (GaeUser)authentication.getPrincipal();
+
+        if (!gaeUser.getEmail().equals(googleUser.getEmail())) {
+            return false;
+        }
+
+        return true;
+
+    }
+
     @Override
     @Override
     public void afterPropertiesSet() throws ServletException {
     public void afterPropertiesSet() throws ServletException {
         Assert.notNull(authenticationManager, "AuthenticationManager must be set");
         Assert.notNull(authenticationManager, "AuthenticationManager must be set");