gateway+springsecurity+oauth2 整合google github 等第三方登录

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

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

接下来 配置 gateway 网关模块

1、添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>

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();
     }
  }

启动网关,并访问
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
即可实现正常登录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值