Thursday, March 10, 2016

spring-social-google authentication does not redirect to register form page

Leave a Comment

I want to add Google Plus flavor to the code included in this Tutorial, however while signing in I get the following warning message from Log4J.

WARN - RestTemplate - GET request for "https://www.googleapis.com/plus/v1/people/me" resulted in 403 (Forbidden); invoking error handler

and instead of redirecting to the registration form like it is for Facebook and Twitter flavors I get redirected once again to the login page.

DEBUG - LoginController - Rendering login page.

The code for the tutorial is available here. These are the changes that I've made to it.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>net.petrikainulainen.spring.social.signin</groupId>     <artifactId>spring-mvc-normal</artifactId>     <packaging>war</packaging>     <version>0.1</version>     <name>Spring Social Sign In MVC Normal</name>     <description>         This example demonstrates how you can integration Spring Social 1.1.0,         Spring Security 3.2.0, and a "normal" Spring MVC application.          You can create an user account by using either "normal" form registration or         sign in by using a social media service.     </description>      <properties>         <hibernate.version>4.2.4.Final</hibernate.version>         <liquibase.version>3.1.1</liquibase.version>         <mysql.connector.version>5.1.26</mysql.connector.version>         <slf4j.version>1.7.5</slf4j.version>         <spring.version>4.0.0.RELEASE</spring.version>         <spring.data.jpa.version>1.4.3.RELEASE</spring.data.jpa.version>         <spring.security.version>3.2.0.RELEASE</spring.security.version>         <spring.social.version>1.1.2.RELEASE</spring.social.version>         <spring.social.facebook.version>2.0.3.RELEASE</spring.social.facebook.version>         <spring.social.twitter.version>1.1.2.RELEASE</spring.social.twitter.version>          <build.profile.id>dev</build.profile.id>         <jdk.version>1.7</jdk.version>         <skip.integration.tests>true</skip.integration.tests>         <skip.unit.tests>false</skip.unit.tests>          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     </properties>     <profiles>         <profile>             <id>dev</id>             <properties>                 <build.profile.id>dev</build.profile.id>             </properties>         </profile>         <profile>             <id>integration-test</id>             <properties>                 <build.profile.id>integration-test</build.profile.id>                 <skip.integration.tests>false</skip.integration.tests>                 <skip.unit.tests>true</skip.unit.tests>             </properties>         </profile>     </profiles>      <build>         <finalName>spring-social-normal-mvc</finalName>         <filters>             <filter>profiles/${build.profile.id}/config.properties</filter>             <filter>profiles/${build.profile.id}/socialConfig.properties</filter>         </filters>         <resources>             <resource>                 <filtering>true</filtering>                 <directory>src/main/resources</directory>                 <excludes>                     <exclude>**/messages*.properties</exclude>                 </excludes>             </resource>             <resource>                 <filtering>false</filtering>                 <directory>src/main/resources</directory>                 <includes>                     <include>**/messages*.properties</include>                 </includes>             </resource>         </resources>          <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-dependency-plugin</artifactId>                 <version>2.8</version>                 <executions>                     <execution>                         <phase>package</phase>                         <goals>                             <goal>copy</goal>                         </goals>                         <configuration>                             <artifactItems>                                 <artifactItem>                                     <groupId>com.github.jsimone</groupId>                                     <artifactId>webapp-runner</artifactId>                                     <version>8.0.24.1</version>                                     <destFileName>webapp-runner.jar</destFileName>                                 </artifactItem>                             </artifactItems>                         </configuration>                     </execution>                 </executions>             </plugin>              <plugin>                 <groupId>org.codehaus.mojo</groupId>                 <artifactId>build-helper-maven-plugin</artifactId>                 <version>1.7</version>                 <executions>                     <execution>                         <id>add-integration-test-sources</id>                         <phase>generate-test-sources</phase>                         <goals>                             <goal>add-test-source</goal>                         </goals>                         <configuration>                             <sources>                                 <source>src/integration-test/java</source>                             </sources>                         </configuration>                     </execution>                     <execution>                         <id>add-integration-test-resources</id>                         <phase>generate-test-resources</phase>                         <goals>                             <goal>add-test-resource</goal>                         </goals>                         <configuration>                             <resources>                                 <resource>                                     <directory>src/integration-test/resources</directory>                                 </resource>                                 <resource>                                     <directory>etc/db</directory>                                 </resource>                             </resources>                         </configuration>                     </execution>                 </executions>             </plugin>             <!-- Used for unit tests -->             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <version>2.16</version>                 <configuration>                     <argLine>${surefireArgLine}</argLine>                     <!-- Skips unit tests if the value of skip.unit.tests property is true -->                     <skipTests>${skip.unit.tests}</skipTests>                     <excludes>                         <exclude>**/IT*.java</exclude>                     </excludes>                 </configuration>             </plugin>             <!-- Used for integration tests -->             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-failsafe-plugin</artifactId>                 <version>2.16</version>                 <configuration>                     <argLine>${failsafeArgLine}</argLine>                 </configuration>                 <executions>                     <!-- States that both integration-test and verify goals of the Failsafe Maven plugin are executed. -->                     <execution>                         <id>integration-tests</id>                         <goals>                             <goal>integration-test</goal>                             <goal>verify</goal>                         </goals>                         <configuration>                             <!-- Skips integration tests if the value of skip.integration.tests property is true -->                             <skipTests>${skip.integration.tests}</skipTests>                         </configuration>                     </execution>                 </executions>             </plugin>             <plugin>                 <groupId>org.jacoco</groupId>                 <artifactId>jacoco-maven-plugin</artifactId>                 <version>0.7.5.201505241946</version>                 <executions>                     <!--                        Prepares the property pointing to the JaCoCo runtime agent which                        is passed as VM argument when Maven the Surefire plugin is executed.                    -->                     <execution>                         <id>pre-unit-test</id>                         <goals>                             <goal>prepare-agent</goal>                         </goals>                         <configuration>                             <!-- Sets the path to the file which contains the execution data. -->                             <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>                             <!--                                Sets the name of the property containing the settings                                for JaCoCo runtime agent.                            -->                             <propertyName>surefireArgLine</propertyName>                         </configuration>                     </execution>                     <!--                        Ensures that the code coverage report for unit tests is created after                        unit tests have been run.                    -->                     <execution>                         <id>post-unit-test</id>                         <phase>test</phase>                         <goals>                             <goal>report</goal>                         </goals>                         <configuration>                             <!-- Sets the path to the file which contains the execution data. -->                             <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>                             <excludes>                                 <exclude>net/petrikainulainen/spring/social/signinmvc/config/**</exclude>                             </excludes>                             <!-- Sets the output directory for the code coverage report. -->                             <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>                         </configuration>                     </execution>                 </executions>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.1</version>                 <configuration>                     <source>${jdk.version}</source>                     <target>${jdk.version}</target>                     <encoding>${project.build.sourceEncoding}</encoding>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-resources-plugin</artifactId>                 <version>2.6</version>                 <configuration>                     <encoding>${project.build.sourceEncoding}</encoding>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-war-plugin</artifactId>                 <version>2.4</version>                 <configuration>                     <failOnMissingWebXml>false</failOnMissingWebXml>                 </configuration>             </plugin>             <plugin>                 <groupId>org.liquibase</groupId>                 <artifactId>liquibase-maven-plugin</artifactId>                 <version>${liquibase.version}</version>                 <dependencies>                     <dependency>                         <groupId>mysql</groupId>                         <artifactId>mysql-connector-java</artifactId>                         <version>${mysql.connector.version}</version>                     </dependency>                 </dependencies>                 <configuration>                     <propertyFile>target/classes/liquibase.properties</propertyFile>                 </configuration>             </plugin>             <plugin>                 <groupId>org.mortbay.jetty</groupId>                 <artifactId>jetty-maven-plugin</artifactId>                 <version>8.1.12.v20130726</version>                 <configuration>                     <stopKey>todostop</stopKey>                     <stopPort>9999</stopPort>                 </configuration>             </plugin>         </plugins>     </build>     <dependencies>         <!-- Spring Framework -->         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-aop</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-beans</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-core</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context-support</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-jdbc</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-orm</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-tx</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-web</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-webmvc</artifactId>             <version>${spring.version}</version>         </dependency>         <!-- Spring Security -->         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-core</artifactId>             <version>${spring.security.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-config</artifactId>             <version>${spring.security.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-taglibs</artifactId>             <version>${spring.security.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-web</artifactId>             <version>${spring.security.version}</version>         </dependency>          <!-- Use Apache HttpClient as HTTP Client -->         <dependency>             <groupId>org.apache.httpcomponents</groupId>             <artifactId>httpclient</artifactId>             <version>4.3.2</version>         </dependency>          <!-- Spring Social -->         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-config</artifactId>             <version>${spring.social.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-core</artifactId>             <version>${spring.social.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-security</artifactId>             <version>${spring.social.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-web</artifactId>             <version>${spring.social.version}</version>         </dependency>         <!-- Spring Social Facebook -->         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-facebook</artifactId>             <version>${spring.social.facebook.version}</version>             <exclusions>                 <exclusion>                     <artifactId>spring-social-core</artifactId>                     <groupId>org.springframework.social</groupId>                 </exclusion>                 <exclusion>                     <artifactId>spring-social-config</artifactId>                     <groupId>org.springframework.social</groupId>                 </exclusion>             </exclusions>         </dependency>         <!-- Spring Social Twitter -->         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-twitter</artifactId>             <version>${spring.social.twitter.version}</version>         </dependency>         <!-- Spring Social Google -->         <dependency>             <groupId>org.springframework.social</groupId>             <artifactId>spring-social-google</artifactId>             <version>1.0.0.RELEASE</version>         </dependency>         <!-- CGLIB -->         <dependency>             <groupId>cglib</groupId>             <artifactId>cglib</artifactId>             <version>2.2.2</version>         </dependency>          <!-- Hibernate Validator -->         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-validator</artifactId>             <version>4.2.0.Final</version>         </dependency>          <!-- Sitemesh -->         <dependency>             <groupId>org.sitemesh</groupId>             <artifactId>sitemesh</artifactId>             <version>3.0-alpha-2</version>         </dependency>          <!-- Servlet API 3.0 -->         <dependency>             <groupId>org.glassfish</groupId>             <artifactId>javax.servlet</artifactId>             <version>3.0</version>             <scope>provided</scope>         </dependency>          <!-- JSTL -->         <dependency>             <groupId>javax.servlet.jsp</groupId>             <artifactId>jsp-api</artifactId>             <version>2.1</version>             <scope>provided</scope>         </dependency>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>jstl</artifactId>             <version>1.1.2</version>             <scope>compile</scope>         </dependency>         <dependency>             <groupId>taglibs</groupId>             <artifactId>standard</artifactId>             <version>1.1.2</version>             <scope>compile</scope>         </dependency>          <!-- Spring Data JPA -->         <dependency>             <groupId>org.springframework.data</groupId>             <artifactId>spring-data-jpa</artifactId>             <version>${spring.data.jpa.version}</version>         </dependency>         <!-- Hibernate -->         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-core</artifactId>             <version>${hibernate.version}</version>         </dependency>         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-entitymanager</artifactId>             <version>${hibernate.version}</version>         </dependency>          <!-- MySQL -->         <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>             <version>${mysql.connector.version}</version>         </dependency>          <!-- PostgreSQL -->         <dependency>             <groupId>org.postgresql</groupId>             <artifactId>postgresql</artifactId>             <version>9.3-1102-jdbc41</version>         </dependency>          <!-- H2 Database -->         <dependency>             <groupId>com.h2database</groupId>             <artifactId>h2</artifactId>             <version>1.3.173</version>         </dependency>          <!-- BoneCP -->         <dependency>             <groupId>com.jolbox</groupId>             <artifactId>bonecp</artifactId>             <version>0.7.1.RELEASE</version>         </dependency>          <dependency>             <groupId>org.apache.commons</groupId>             <artifactId>commons-lang3</artifactId>             <version>3.1</version>         </dependency>          <!-- Logging -->         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-api</artifactId>             <version>${slf4j.version}</version>         </dependency>         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-log4j12</artifactId>             <version>${slf4j.version}</version>         </dependency>         <dependency>             <groupId>log4j</groupId>             <artifactId>log4j</artifactId>             <version>1.2.17</version>         </dependency>          <!-- Joda -->         <dependency>             <groupId>joda-time</groupId>             <artifactId>joda-time</artifactId>             <version>2.3</version>         </dependency>          <dependency>             <groupId>org.jadira.usertype</groupId>             <artifactId>usertype.spi</artifactId>             <version>3.0.0.GA</version>         </dependency>          <dependency>             <groupId>org.jadira.usertype</groupId>             <artifactId>usertype.core</artifactId>             <version>3.0.0.GA</version>         </dependency>          <!-- TESTING DEPENDENCIES -->         <dependency>             <groupId>org.assertj</groupId>             <artifactId>assertj-core</artifactId>             <version>1.6.0</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.hamcrest</groupId>             <artifactId>hamcrest-all</artifactId>             <version>1.3</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>4.11</version>             <scope>test</scope>             <exclusions>                 <exclusion>                     <artifactId>hamcrest-core</artifactId>                     <groupId>org.hamcrest</groupId>                 </exclusion>             </exclusions>         </dependency>         <dependency>             <groupId>org.mockito</groupId>             <artifactId>mockito-all</artifactId>             <version>1.9.5</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>com.googlecode.catch-exception</groupId>             <artifactId>catch-exception</artifactId>             <version>1.2.0</version>             <exclusions>                 <exclusion>                     <groupId>org.mockito</groupId>                     <artifactId>mockito-core</artifactId>                 </exclusion>             </exclusions>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-test</artifactId>             <version>${spring.version}</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>com.github.springtestdbunit</groupId>             <artifactId>spring-test-dbunit</artifactId>             <version>1.1.0</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.dbunit</groupId>             <artifactId>dbunit</artifactId>             <version>2.4.9</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.liquibase</groupId>             <artifactId>liquibase-core</artifactId>             <version>${liquibase.version}</version>             <scope>test</scope>         </dependency>     </dependencies> </project> 

resources/application.properties - added following lines

#Google spring.social.google.appId=[here my appId] spring.social.google.appSecret=[here my appSecret] 

The rest of the changes are provided in the GitHub repository I have forked.

The app needs to be started by

mvn jetty:run -P dev -Djetty.port=8090 

the jetty.port can be of course omitted if you have the port 8080 free to be bound.

I have read somewhere that the Google Plus request needs to be POST but it did not give me any other result, therefore the code in login.jsp looks like this

<div class="row social-button-row">     <div class="col-lg-4">         <!--form method="POST" action="${pageContext.request.contextPath}/auth/google?scope=email">             <button type="submit" class="btn btn-google-plus"><i class="icon-google-plus"></i> | <spring:message code="label.google.sign.in.button"/></button>         </form-->         <a href="${pageContext.request.contextPath}/auth/google?scope=email"><button type="submit" class="btn btn-google-plus"><i class="icon-google-plus"></i> | <spring:message code="label.google.sign.in.button"/></button></a>     </div> </div> 

Has anyone faced and solved this problem? With Twitter and Facebook it works without any problems.

EDIT

I have spotted that after a handshake with Facebook or Twitter I get redirected to /singup page if the account does not exist yet. However with Google Plus I get redirected to /signin which of course redirects to /login because there is no user account in the database yet.

2 Answers

Answers 1

You can find here another article with source code and demo. It may help you to implement spring-social-google authentication. https://samerabdelkafi.wordpress.com/2016/02/29/social-login-with-spring-social/

Answers 2

The problem was that the Google+ API was not enabled for my application in the Google Developers' Console. Why it is not enabled by default I have no idea.

Also this project helped me a lot to diagnose the reason of the issue.

WARNING:       Response body: {  "error": {   "errors": [    {     "domain": "usageLimits",     "reason": "accessNotConfigured",     "message": "Access Not Configured. The API (Google+ API) is not enabled for your project. Please use the Google Developers Console to update your configuration.",     "extendedHelp": "https://console.developers.google.com"    }   ],   "code": 403,   "message": "Access Not Configured. The API (Google+ API) is not enabled for your project. Please use the Google Developers Console to update your configuration."  } } 

Google+ API enable in GDC

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment