0

我想將Google Plus添加到此Tutorial中包含的代碼中,但是在登錄時,我會從Log4J中收到以下警告消息。spring-social-google身份驗證不會重定向到註冊表單頁面

WARN - RestTemplate - 「https://www.googleapis.com/plus/v1/people/me」的GET請求導致403(Forbidden);調用錯誤處理程序

而不是重定向到登記表像它爲Facebook 和Twitter的味道我得到再次重定向到登錄頁面和。

DEBUG - LoginController - 渲染登錄頁面。

本教程的代碼可以here。這些是我對它做出的改變。

的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> 

資源/ application.properties - 添加以下行

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

在GitHub的倉庫我有forked提供的其餘的改動。

的應用程序需要通過

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

的jetty.port開始當然可以省略,如果你有8080端口自由的約束。

我讀的地方,在谷歌加請求需要POST,但它並沒有給我任何其他的結果,因此在login.jsp代碼看起來像這樣

<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> 

有沒有人面對和解決這個問題呢?藉助Twitter和Facebook,它可以毫無問題地工作。

編輯

我已經發現,經過與Facebook或Twitter的握手我重定向到/singup頁如果帳戶不存在。但是,如果使用Google Plus,我將重定向到/signin,這當然會重定向到/login,因爲數據庫中沒有用戶帳戶。

回答

0

問題在於我的應用程序在Google Developers' Console中未啓用Google+ API。爲什麼它沒有啓用默認我不知道。

此外這project幫助我很多來診斷問題的原因。

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

+0

有谷歌提供多種API,有的甚至需要錢,讓(我認爲的Compute Engine)。我與谷歌地圖API有同樣的問題,它也沒有默認啓用。 我會回答這個問題,但我認爲你已經知道,並有機會獲得第一次賞金xD –

+0

嗨,我現在在Spring社交Twitter上遇到了一些問題。一段時間以來,可以從Twitter用戶帳戶獲取電子郵件的詳細信息。必須做的是通過Twitter特別請求這樣的許可。我已經完成了這項工作,即使'UserProfile'的getEmail方法爲Twitter返回null。我已經在Spring Social Twitter GitHub存儲庫上打開了一個[issue](https://github.com/spring-projects/spring-social-twitter/issues/97)。如果你能夠幫助我,那麼我會延長這個問題,讓這筆獎金落到你的手中。 :)我使用SST版本1.1.2.RELEASE。 – Jagger

+0

好的,我會研究這個。我在Streams上做了一些Spring社交Twitter的實驗,但沒有用電子郵件做實驗。我看我是否可以獲得用戶電子郵件。 :) –

相關問題