无涯教程-Spring Boot - Google OAuth2

77 阅读2分钟

在本章中,无涯教程将了解如何在Gradle版本中使用Spring Boot应用程序添加Google OAuth2登录。

首先,在构建配置文件中添加Spring Boot OAuth2安全依赖项,构建配置文件如下所示。

buildscript {
   ext {
      springBootVersion=1.5.8.RELEASE
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: java apply plugin: eclipse apply plugin: org.springframework.boot

group=com.learnfk.projects version=0.0.1-SNAPSHOT sourceCompatibility=1.8

repositories { mavenCentral() } dependencies { compile(org.springframework.boot:spring-boot-starter) testCompile(org.springframework.boot:spring-boot-starter-test) compile(org.springframework.security.oauth:spring-security-oauth2) compile(org.springframework.boot:spring-boot-starter-web) testCompile(org.springframework.boot:spring-boot-starter-test) }

现在,在主Spring Boot应用程序类文件中通过Spring Boot进行身份验证后,添加HTTP端点以从Google读取用户主体,如下所示-

package com.learnfk.projects.Learnfkservice;

import java.security.Principal;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication @RestController public class GoogleserviceApplication { public static void main(String[] args) { SpringApplication.run(GoogleserviceApplication.class, args); } @RequestMapping(value="/user") public Principal user(Principal principal) { return principal; } }

现在,编写一个配置文件来为Web安全启用OAuth2 SSO,并删除index.html文件的身份验证,如-所示

package com.learnfk.projects.Learnfkservice;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration @EnableOAuth2Sso public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf() .disable() .antMatcher("/**") .authorizeRequests() .antMatchers("/", "/index.html") .permitAll() .anyRequest() .authenticated(); } }

接下来,在静态资源下添加index.html文件,并添加重定向到user HTTP Endpoint的链接,以读取Google user Principal,如下所示-

<!DOCTYPE html>
<html>
   <head>
      <meta charset="ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <a href="user">Click here to Google Login</a>
   </body>
</html> 

注意Google Cloud Console中的--启用Gmail服务、分析服务和Google+服务API。

然后,转到Credentials部分并创建一个Credentials并选择OAuth client ID。

Credentials Section

接下来,在OAuth2同意屏幕中提供产品名称。

Product Name in OAuth2 Consent Screen

接下来,选择Application Type为"Web application",提供授权的JavaScript源和授权的重定向URI。

Authorized Redirect URIs

现在,您的OAuth2客户端ID和客户端密码已经创建。

OAuth2 Client Id Created

接下来,在应用程序属性文件中添加客户端ID和客户端机密。

security.oauth2.client.clientId=<CLIENT_ID>
security.oauth2.client.clientSecret=<CLIENT_SECRET>
security.oauth2.client.accessTokenUri = https://www.Learnfkapis.com/oauth2/v3/token
security.oauth2.client.userAuthorizationUri = https://accounts.Learnfk.com/o/oauth2/auth
security.oauth2.client.tokenName=oauth_token
security.oauth2.client.authenticationScheme=query
security.oauth2.client.clientAuthenticationScheme=form
security.oauth2.client.scope=profile email

security.oauth2.resource.userInfoUri = https://www.Learnfkapis.com/userinfo/v2… security.oauth2.resource.preferTokenInfo=false

现在,您可以创建一个可执行的JAR文件,并使用以下Gradle命令运行Spring Boot应用程序。

对于Gradle,您可以使用如下所示的命令-

gradle clean build

在"构建成功"之后,您可以在build/libs目录下找到JAR文件。

使用命令java-jar <JARFILE>运行JAR文件,然后在Tomcat端口8080上启动应用程序。

现在点击URLhttp://localhost:8080/并单击Google Login链接。

Google Login link

它将重定向到Google登录屏幕,并提供Gmail登录详细信息。

Google Login Screen

如果登录成功,无涯教程将收到Gmail用户的主体对象。

Principal Object of The Gmail User

参考链接

www.learnfk.com/spring-boot…