Google Login Using REST Service In Spring Boot

Introduction
Today we are going to discuss a very interesting topic Google Login using REST Service in Spring Boot.
Requirements
- Open the Google API Console.
- Click Select a project, then NEW PROJECT, and enter a name for the project, and optionally, edit the provided Project ID. Click Create.
- On the Credentials page, select Create credentials, then OAuth client ID.
- You may be prompted to set a product name on the Consent screen; if so, click Configure consent screen, supply the requested information, and click Save to return to the Credentials screen.
- Select Other for the Application type, and enter any additional information required.
- Click Create.
- On the page that appears, copy the client ID and client secret to your clipboard, as you will need them when you configure your client library.
If You want to read the full post please click on this click
Let’s Start
- Required Tools:
- Maven
- STS
- JDK 8
Project Structure

Dive In the Code
First, create the main function
package com.oauth; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OauthSetupApplication { public static void main(String[] args) { SpringApplication.run(OauthSetupApplication.class, args); } }
Add Dependencies In Your pom.xml File
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp --> <dependency> <groupId>org.apache.sshd</groupId> <artifactId>sshd-sftp</artifactId> <version>2.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.45</version> </dependency> <!-- Swagger Integration --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client --> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client</artifactId> <version>1.30.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-oauth2 --> <dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-oauth2</artifactId> <version>v2-rev65-1.17.0-rc</version> </dependency> </dependencies>
Create a SwaggerConfig.java Class
Here we config the Swagger for testing the REST API’s.
If you want to learn more about the Swagger Configuration click on the below link.
SwaggerConfig.java Class
package com.oauth.config.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger.web.ApiKeyVehicle; import springfox.documentation.swagger.web.SecurityConfiguration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.oauth.controller")) .paths(PathSelectors.any()) .build(); } }
Create an oauthDTO Class for returning the response.
package com.oauth.DTO; import com.google.api.client.auth.openidconnect.IdToken.Payload; public class OauthDTO { private Payload payload; private String email; private boolean emailVerified; private String name; private String pictureUrl; private String locale; private String familyName; private String givenName; /** * @return the payload */ public Payload getPayload() { return payload; } /** * @param payload the payload to set */ public void setPayload(Payload payload) { this.payload = payload; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } /** * @return the emailVerified */ public boolean isEmailVerified() { return emailVerified; } /** * @param emailVerified the emailVerified to set */ public void setEmailVerified(boolean emailVerified) { this.emailVerified = emailVerified; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the pictureUrl */ public String getPictureUrl() { return pictureUrl; } /** * @param pictureUrl the pictureUrl to set */ public void setPictureUrl(String pictureUrl) { this.pictureUrl = pictureUrl; } /** * @return the locale */ public String getLocale() { return locale; } /** * @param locale the locale to set */ public void setLocale(String locale) { this.locale = locale; } /** * @return the familyName */ public String getFamilyName() { return familyName; } /** * @param familyName the familyName to set */ public void setFamilyName(String familyName) { this.familyName = familyName; } /** * @return the givenName */ public String getGivenName() { return givenName; } /** * @param givenName the givenName to set */ public void setGivenName(String givenName) { this.givenName = givenName; } }
Create an OauthController.java class for implementing the whole logic.
Best Practice, just create the service and implementation separately.
package com.oauth.controller; import java.util.Collections; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; import com.google.api.client.auth.openidconnect.IdToken.Payload; import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.oauth.DTO.OauthDTO; @RestController @RequestMapping("/Oauth") public class OauthController { @PostMapping("/client/login/userId") public OauthDTO getOauthLogin(@RequestParam String googleUserIdToken) throws Throwable { GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new JacksonFactory()) .setAudience(Collections .singletonList("Your client Id which is created by you in developer.google.account")) .build(); GoogleIdToken idToken = verifier.verify(googleUserIdToken); if (idToken != null) { Payload payload = idToken.getPayload(); String email = ((com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) payload).getEmail(); boolean emailVerified = Boolean.valueOf(((com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) payload).getEmailVerified()); String name = (String) payload.get("name"); String pictureUrl = (String) payload.get("picture"); String locale = (String) payload.get("locale"); String familyName = (String) payload.get("family_name"); String givenName = (String) payload.get("given_name"); OauthDTO oauthDTO=new OauthDTO(); oauthDTO.setEmail(email); oauthDTO.setEmailVerified(emailVerified); oauthDTO.setName(name); oauthDTO.setFamilyName(familyName); oauthDTO.setGivenName(givenName); oauthDTO.setPayload(payload); oauthDTO.setPictureUrl(pictureUrl); oauthDTO.setLocale(locale); return oauthDTO; } throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"Something not right"); } }