title: Login using Google with Spring Security and Spring Social
body: <p>Various Java classes which i used for this purpose are</p>
  
  <pre><code>public class GoogleConnectInterceptor extends SpringSocialSecurityConnectInterceptor&lt;Google&gt;{
  
  }
  </code></pre>
  
  <p>This class provides the connector interface:</p>
  
  <pre><code>public class GoogleProviderConfig extends AbstractProviderConfig&lt;Google&gt;{
  
      @Autowired
      private ConnectInterceptor&lt;Google&gt; googleConnectInterceptor;
  
      @Override
      protected ConnectInterceptor&lt;Google&gt; getConnectInterceptor() {
          return googleConnectInterceptor;
      }
  
  }
  </code></pre>
  
  <p>and this is my controller class method</p>
  
  <pre><code>@RequestMapping("/protected/google")
      public String helloGoogleProtectedWorld(Map model) {
          model.put("userName", getUserName());
  
          // Display on the jsp which security level the page is intended for
          model.put("securityLevel", "LinkedIn Protected");
  
          return "helloWorld";
      }
  </code></pre>
  
  <p>and in the following class i am giving the all the configuration needed for the connection between facebook twitter and linkedin. here i this app i am able tp login using facebook, twitter and linkedin but failed in case of google</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableSocial
  @PropertySource("classpath:/environment.properties")
  public class SocialConfig implements SocialConfigurer {
  
  
      // Handle to users connection repository - allows us to set connection sign up in post construct
      private JdbcUsersConnectionRepository usersConnectionRepository;
  
      @Autowired(required=false)
      private SpringSocialSecurityConnectionSignUp springSocialSecurityConnnectionSignUp;
  
  
  
      @Bean
      public DataSource dataSource() {
          EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
          return builder.setType(EmbeddedDatabaseType.H2).build();
      }
  
  
      @Bean 
      public DataSourceTransactionManager transactionManager(DataSource dataSource)
      {
          return new DataSourceTransactionManager(dataSource);
      }
  
      @Override
      public void addConnectionFactories(
              ConnectionFactoryConfigurer cfConfig,
              Environment env) {
           cfConfig.addConnectionFactory(new TwitterConnectionFactory(
                      env.getProperty("twitter.consumerKey"),
                      env.getProperty("twitter.consumerSecret")
              ));
              cfConfig.addConnectionFactory(new FacebookConnectionFactory(
                      env.getProperty("facebook.clientId"),
                      env.getProperty("facebook.clientSecret")
              ));
              cfConfig.addConnectionFactory(new LinkedInConnectionFactory(
                      env.getProperty("linkedin.consumerKey"),
                      env.getProperty("linkedin.consumerSecret")
              ));
              cfConfig.addConnectionFactory(new GoogleConnectionFactory(
                      env.getProperty("google.clientId"),
                      env.getProperty("google.clientSecret")));
              cfConfig.addConnectionFactory(new SpringSocialSecurityConnectionFactory());
      }
  
      /**
       * This is only needed because the official spring-social-security from SpringSocial is on the classpath
       * @return
       */
      @Override
      public UserIdSource getUserIdSource() {
           return new AuthenticationNameUserIdSource();
      }
  
  
      @Override
      public UsersConnectionRepository getUsersConnectionRepository(
              ConnectionFactoryLocator connectionFactoryLocator) {
          usersConnectionRepository  = new JdbcUsersConnectionRepository(
                  dataSource(),
                  connectionFactoryLocator,
                 Encryptors.noOpText());
          return usersConnectionRepository;
  
      }
  
  
  
      @PostConstruct
      // Registers a mechanism for implicit sign up if user id available from provider
      // Remove if explicit user name selection is required
      public void registerConnectionSignUp()
      {
          usersConnectionRepository.setConnectionSignUp(springSocialSecurityConnnectionSignUp);
      }
  
  
  }
  </code></pre>
  
  <p>and the below given file is my spring config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:security="http://www.springframework.org/schema/security"
  
  
      xsi:schemaLocation="  http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-4.0.xsd              
                  http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                  http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.2.xsd"&gt;
  
      &lt;bean
          class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'&gt;
          &lt;property name='location'&gt;
              &lt;value&gt;classpath:environment.properties&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Scan classpath for components, including our Social Security Configuration 
          class --&gt;
  
      &lt;context:component-scan base-package="org.socialsignin.springsocial.security.config.social" &gt;
      &lt;/context:component-scan&gt;
  
      &lt;context:component-scan base-package="org.socialsignin.springsocial.security" &gt;
      &lt;/context:component-scan&gt;
  
  
      &lt;context:component-scan base-package="org.socialsignin.provider" /&gt;
  
  
      &lt;!-- End Spring Social Security --&gt;
  
      &lt;!-- Start Spring Security --&gt;
  
      &lt;security:global-method-security
          proxy-target-class="false" &gt;
      &lt;/security:global-method-security&gt;
  
  
      &lt;security:authentication-manager alias="authenticationManager"&gt;
      &lt;/security:authentication-manager&gt;
  
  
      &lt;bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
          xmlns="http://www.springframework.org/schema/beans"&gt;
          &lt;constructor-arg&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.security.access.vote.RoleVoter" /&gt;
                  &lt;bean class="org.springframework.security.access.vote.AuthenticatedVoter" /&gt;
              &lt;/list&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;
  
      &lt;security:http use-expressions="true"
          entry-point-ref="springSocialSecurityEntryPoint" xmlns="http://www.springframework.org/schema/security"&gt;
  
          &lt;intercept-url pattern="/protected/twitter" access="hasRole('ROLE_USER_TWITTER')" /&gt;
          &lt;intercept-url pattern="/protected/facebook" access="hasRole('ROLE_USER_FACEBOOK')" /&gt;
          &lt;intercept-url pattern="/protected/linkedin" access="hasRole('ROLE_USER_LINKEDIN')"/&gt;
          &lt;intercept-url pattern="/protected/google" access="hasRole('ROLE_USER_GOOGLE')"/&gt;
          &lt;intercept-url pattern="/protected/facebookTwitter" access="hasRole('ROLE_USER_FACEBOOK') and hasRole('ROLE_USER_TWITTER')" /&gt;
          &lt;intercept-url pattern="/protected/**" access="hasRole('ROLE_USER')" /&gt;
  
          &lt;intercept-url pattern="/oauthconnect.jsp" access="hasRole('ROLE_USER')" /&gt;
  
          &lt;access-denied-handler ref="springSocialSecurityAccessDeniedHandler"/&gt;
  
  
          &lt;security:logout logout-url="/logout" /&gt;
  
          &lt;anonymous /&gt;
          &lt;security:custom-filter position="FORM_LOGIN_FILTER"
              ref="springSocialSecurityAuthenticationFilter" /&gt;
  
          &lt;remember-me services-ref="springSocialSecurityRememberMeServices"
              key="springSocialSecurity" /&gt;
  
      &lt;/security:http&gt;
  
  
      &lt;bean id="springSocialSecurityEntryPoint"
          class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"&gt;
          &lt;property name="loginFormUrl" value="/oauthlogin.jsp"/&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="springSocialSecurityRememberMeServices"
          class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices"&gt;
          &lt;property name="userDetailsService" ref="springSocialSecurityUserDetailsService" /&gt;
          &lt;property name="tokenRepository" ref="tokenRepository" /&gt;
          &lt;property name="key" value="springSocialSecurity" /&gt;
          &lt;property name="alwaysRemember" value="true" /&gt;
          &lt;property name="tokenValiditySeconds" value="3600" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="tokenRepository"
          class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl"&gt;
          &lt;property name="createTableOnStartup" value="true" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- End Spring Security --&gt;
  
  
      &lt;!-- Start Spring Social --&gt;
  
      &lt;!-- Transaction manager for a single JDBC DataSource --&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource"&gt;&lt;ref bean="dataSource"/&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean class="org.springframework.social.connect.web.ProviderSignInController"&gt;
          &lt;constructor-arg value="${application.secureUrl}" /&gt;
          &lt;property name="signUpUrl" value="/signup" /&gt;
          &lt;property name="postSignInUrl" value="/authenticate" /&gt;
  
          &lt;!-- relies on by-type autowiring for the other constructor-args --&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.social.connect.web.ConnectController"&gt;
          &lt;!-- relies on by-type autowiring for the constructor-args --&gt;
          &lt;property name="interceptors" ref="connectInterceptorList"&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!--  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName"&gt;
              &lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;jdbc:hsqldb:mem:socialsignin&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="username"&gt;
              &lt;value&gt;sa&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;value&gt;&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="defaultAutoCommit"&gt;
              &lt;value&gt;true&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      --&gt;
      &lt;bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors"
          factory-method="noOpText" /&gt;
  
      &lt;!-- End Spring Social --&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And here is my JSp page from where i am redirecting to the Facebook, LinkedIn and Twitter</p>
  
  <pre><code>&lt;html&gt;
   &lt;head&gt;
   &lt;/head&gt;
   &lt;body&gt;
  
  
  &lt;%@ taglib prefix="authz" uri="http://www.springframework.org/security/tags" %&gt;
  
   &lt;authz:authorize access="!hasRole('ROLE_USER')"&gt;
  
  
  &lt;p&gt;Please log in with a third party provider&lt;/p&gt;
  
  
    &lt;form class="login"action="http://localhost:8080/spring-social-security-demo-webapp/signin/twitter" method="POST"&gt;
      &lt;p&gt;&lt;input type="submit" value="Login with Twitter" /&gt;&lt;/p&gt;
  &lt;/form&gt; 
  
    &lt;form class="login"action="http://localhost:8080/spring-social-security-demo-webapp/signin/facebook" method="POST"&gt;
      &lt;p&gt;&lt;input type="submit" value="Login with Facebook" /&gt;&lt;/p&gt;
  &lt;/form&gt; 
  
  &lt;form class="login"action="http://localhost:8080/spring-social-security-demo-webapp/signin/linkedin" method="POST"&gt;
      &lt;p&gt;&lt;input type="submit" value="Login with LinkedIn" /&gt;&lt;/p&gt;
  &lt;/form&gt; 
  
  &lt;form class="login"action="http://localhost:8080/spring-social-security-demo-webapp/signin/google" method="POST"&gt;
      &lt;p&gt;&lt;input type="submit" value="Login with Google" /&gt;&lt;/p&gt;
  &lt;/form&gt; 
  &lt;/authz:authorize&gt;
  
   &lt;authz:authorize access="hasRole('ROLE_USER')"&gt;
  
      You are already logged in
  
   &lt;/authz:authorize&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/kTw0m.png" alt="enter image description here"></p>