前言:一般整合第三方登录会使用授权码模式,那么整合谷歌账号登录有两种方式,其一是利用官方提供SDK按授权码流程来实现,其二可以使用SpringSecurity,框架中集成了Google、Github等方式,只需简单配置就可以实现第三方登录。

以谷歌为例: 首先 注册Google Cloud Platform (GCP) 进入控制台 先配置好 oauth同意屏幕 添加一个 ClientID,选择WEB应用

接下来 配置 gateway 网关模块

1、添加依赖

org.springframework.boot

spring-boot-starter-oauth2-client

2.配置application.yml

spring:

security:

oauth2:

client:

registration:

google:

client-id: 214317645080-5o28tv5gfi4uhcgitn67f3a74969v33g.apps.googleusercontent.com

client-secret: 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

redirect-uri: https://top.hbin.tk/login/oauth2/code/google # 控制台中的回调地址

github:

client-id: b7c17279fe5eb3198bf9

client-secret: f9e04cd90d649c8d674447cacc3ad7ee72d72cea

redirect-uri: https://top.hbin.tk/login/oauth2/code/github # 控制台中的回调地址

3.添加一个 Security 配置类

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;

import org.springframework.security.config.web.server.ServerHttpSecurity;

import org.springframework.security.web.server.SecurityWebFilterChain;

/**

* @author lhb

* @date 2022/9/4 21:28

*/

@Configuration

@EnableWebFluxSecurity

public class SecurityConfig {

@Bean

public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {

return http.authorizeExchange()

.pathMatchers("/login", "/error")

.permitAll()

.anyExchange()

.authenticated()

.and()

.oauth2Login()

.and()

.build();

}

}

启动网关,并访问 即可实现正常登录

推荐链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。