SpringBoot整合justauth实现多种方式的第三方登陆

目录

0.准备工作

1.引入依赖

2.yml文件

3. Controller代码

 4.效果

参考


0.准备工作

你需要获取三方登陆的client-idclient-secret

github为例

申请地址:Sign in to GitHub · GitHub

1.引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.zhengqing</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <justauth-spring-boot.version>1.1.0</justauth-spring-boot.version>
  </properties>

  <dependencies>
    <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>

    <!-- 添加适用于生产环境的功能,如性能指标和监测等功能 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- ====================================================== -->

    <!-- reids -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- 对象池,使用redis时必须引入 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

    <!-- oauth工具类 -->
    <dependency>
      <groupId>com.xkcoding</groupId>
      <artifactId>justauth-spring-boot-starter</artifactId>
      <version>${justauth-spring-boot.version}</version>
    </dependency>

    <!-- lombok插件 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

    <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.3.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>29.0-jre</version>
    </dependency>


  </dependencies>

  <build>
    <!-- 注:maven默认是不编译,因此加上如下resources才会生产对应的xml文件 目的:解决mybatis映射关系不对应问题  start =============== -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </testResource>
    </testResources>
    <!-- 注:maven默认是不编译,因此加上如下resources才会生产对应的xml文件 目的:解决mybatis映射关系不对应问题  end =============== -->

    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

2.yml文件

server:
  port: 90
  servlet:
    context-path: /demo

spring:
  application:
    name: demo

  # ======================== ↓↓↓↓↓↓ redis相关配置 ↓↓↓↓↓↓ ===============================
  redis:
    # Redis服务器地址
    host: 127.0.0.1
    # Redis服务器连接端口
    port: 6379
    # 连接超时时间(毫秒
    timeout: 10000ms
    # Redis服务器连接密码(默认为空)
    password:
    # Redis数据库索引(默认为0)
    database: 1
    lettuce:
      pool:
        # 连接池最大连接数(使用负值表示没有限制) 默认 8
        max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
        max-wait: -1ms
        # 连接池中的最大空闲连接 默认 8
        max-idle: 8
        # 连接池中的最小空闲连接 默认 0
        min-idle: 0
      cache:
        # 一般来说是不用配置的,Spring Cache 会根据依赖的包自行装配
        type: redis

justauth:
  enabled: true
  type:
    github:
      client-id: Ov23l*********T6zyhg
      client-secret: fd82534****************59aefac
      redirect-uri: http://127.0.0.1:90/demo/oauth/github/callback
#    qq:
#      client-id: 10******85
#      client-secret: 1f7d************************d629e
#      redirect-uri: http://127.0.0.1/demo/oauth/qq/callback
#    wechat:
#      client-id: wxdcb******4ff4
#      client-secret: b4e9dc************************a08ed6d
#      redirect-uri: http://127.0.0.1/demo/oauth/wechat/callback
#    google:
#      client-id: 716******17-6db******vh******ttj320i******userco******t.com
#      client-secret: 9IBorn************7-E
#      redirect-uri: http://127.0.0.1/demo/oauth/google/callback
#    microsoft:
#      client-id: 7bdce8******************e194ad76c1b
#      client-secret: Iu0zZ4************************tl9PWan_.
#      redirect-uri: https://127.0.0.1/demo/oauth/microsoft/callback
#    mi:
#      client-id: 288************2994
#      client-secret: nFeTt89************************==
#      redirect-uri: http://127.0.0.1/demo/oauth/mi/callback
#    wechat_enterprise:
#      client-id: ww58******f3************fbc
#      client-secret: 8G6PCr00j************************rgk************AyzaPc78
#      redirect-uri: http://127.0.0.1/demo/oauth/wechat_enterprise/callback
#      agent-id: 1*******2
  cache:
    type: redis
    prefix: 'SOCIAL::STATE::'
    timeout: 1h

3. Controller代码

package com.zhengqing.demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.xkcoding.justauth.AuthRequestFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p> 第三方登录 Controller </p>
 * @description : 可参考[Spring Boot 快速集成第三方登录功能](https://xkcoding.com/2019/05/22/spring-boot-login-with-oauth.html)
 */
@Slf4j
@RestController
@RequestMapping("/oauth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OauthController {

  private final AuthRequestFactory factory;

  /**
   * 登录类型
   */
  @GetMapping
  public Map<String, String> loginType() {
    List<String> oauthList = factory.oauthList();
    return oauthList.stream().collect(Collectors
        .toMap(oauth -> oauth.toLowerCase() + "登录",
            oauth -> "http://127.0.0.1:90/demo/oauth/login/" + oauth.toLowerCase()));
  }

  /**
   * 登录
   *
   * @param oauthType 第三方登录类型
   * @param response  response
   * @throws IOException
   */
  @RequestMapping("/login/{oauthType}")
  public void renderAuth(@PathVariable String oauthType, HttpServletResponse response)
      throws IOException {
    AuthRequest authRequest = factory.get(getAuthSource(oauthType));
    response.sendRedirect(authRequest.authorize(oauthType + "::" + AuthStateUtils.createState()));
  }

  /**
   * 登录成功后的回调
   *
   * @param oauthType 第三方登录类型
   * @param callback  携带返回的信息
   * @return 登录成功后的信息
   */
  @RequestMapping("/{oauthType}/callback")
  public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
    AuthRequest authRequest = factory.get(getAuthSource(oauthType));
    AuthResponse response = authRequest.login(callback);
    log.info("【response】= {}", JSONUtil.toJsonStr(response));
    return response;
  }

  private AuthSource getAuthSource(String type) {
    if (StrUtil.isNotBlank(type)) {
      return AuthSource.valueOf(type.toUpperCase());
    } else {
      throw new RuntimeException("不支持的类型");
    }
  }

}

 4.效果

 

参考

  1. JustAuth 项目地址:https://github.com/justauth/JustAuth
  2. justauth-spring-boot-starter 地址:https://github.com/justauth/justauth-spring-boot-starter
  3. frp 内网穿透项目地址:https://github.com/fatedier/frp
  4. frp 内网穿透官方中文文档:https://github.com/fatedier/frp/blob/master/README_zh.md
  5. Frp 实现内网穿透:https://zhuanlan.zhihu.com/p/45445979
  6. QQ 互联文档:http://wiki.connect.qq.com / 准备工作_oauth2-0
  7. 微信开放平台文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
  8. GitHub 第三方登录文档:https://developer.github.com/apps/building-oauth-apps/
  9. 谷歌 Oauth2 文档:https://developers.google.com/identity/protocols/OpenIDConnect
  10. 微软 Oauth2 文档:https://docs.microsoft.com/zh-cn/graph/auth-v2-user
  11. 小米开放平台账号服务文档:https://dev.mi.com/console/doc/detail?pId=707

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/729428.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

NAT Easyip实验

我们这篇博客将重点讲述easy ip的配置&#xff1a; 以下面的一个简单的实验拓扑图为例&#xff1a; 本实验使用的网络地址&#xff1a; 1. 我们先来完成基础配置&#xff1a; 1.1AR1的基础配置&#xff1a; 1.2AR2上的基础配置 1.3完成AR1和AR2的基础配置后&#xff0c;我们…

【SPIE出版】第六届无线通信与智能电网国际会议(ICWCSG 2024,7月26-28)

随着科技的飞速发展和能源需求的日益增长&#xff0c;智能电网技术逐渐成为电力行业的重要发展方向。与此同时&#xff0c;无线通信技术在近年来也取得了显著的进步&#xff0c;为智能电网的发展提供了强有力的支持。为了进一步推动无线通信与智能电网的结合与发展&#xff0c;…

Socket编程之多进程模型

一、多进程模型概述 基于最初的阻塞网络 I/O &#xff0c;若服务器要为多个客户端提供支持&#xff0c;在较为传统的手段中&#xff0c;多进程模型是常用的选择&#xff0c;即为每个客户端都分配一个进程来处理其请求。 服务器的主进程主要负责对客户连接的监听&#xff0c;一旦…

视频云沉浸式音视频技术能力探索与建设

概述 随着传输技术、显示技术与算力的持续提升&#xff0c;用户对于音视频体验的需求在提高&#xff0c;各家设备厂商也在探索和推出对应的技术与产品。打造空间感的空间视频与空间音频是其中最为关键的2项技术&#xff0c;bilibili视频云在这两项技术领域也进行了相关代探索与…

redis.conf 参数详解,方便进行性能优化配置

以下是redis.conf中一些常见参数的详细说明&#xff1a; daemonize&#xff1a;是否以后台进程运行&#xff0c;默认为no&#xff1b; pidfile&#xff1a;如以后台进程运行&#xff0c;则需指定一个pid&#xff0c;默认为/var/run/redis.pid&#xff1b;bind&#xff1a;绑定主…

WPF——属性

一、属性 类最初只有字段与函数&#xff0c;字段为一个变量&#xff0c;访问权限可以是private&#xff0c;protected&#xff0c;public。而将字段设为private&#xff0c;不方便外界对类数据的操作&#xff0c;但是将字段设为public又怕外界对数据进行非法操作&#xff0c;于…

二叉树-二叉搜索树的最近公共祖先

目录 一、问题描述 二、解题思路 三、代码实现 四、刷题链接 一、问题描述 二、解题思路 这个问题和之前做过的问题很相似&#xff1a; 深度优先遍历-在二叉树中找到两个节点的最近公共祖先-CSDN博客文章浏览阅读80次。java刷题&#xff1a;在二叉树中找到两个结点的最近公…

用于快速充电站的 AC/DC 转换器概述

电动汽车构成了未来实现可持续交通部门的有前途技术的主要部分。AC/DC 转换器是扩展和改进 EV 功能的骨干组件。本文概述了 AC/DC 转换器、充电站类型、传统两电平 (2L) AC/DC 转换器面临的问题以及使用多电平转换器 (MLC) 的重要性。 AC/DC 充电器示意图&#xff08;&#xff…

2024广东省职业技能大赛云计算赛项实战——Minio服务搭建

Minio服务搭建 前言 这道题是比赛时考到的&#xff0c;没找到具体题目&#xff0c;但在公布的样题中找到了&#xff0c;虽然很短~ 使用提供的 OpenStack 云平台&#xff0c;申请一台云主机&#xff0c;使用提供的软件包安装部署 MINIO 服务并使用 systemctl 管理 Minio是一个…

关于接口测试——自动化框架的设计与实现

一、自动化测试框架 在大部分测试人员眼中只要沾上“框架”&#xff0c;就感觉非常神秘&#xff0c;非常遥远。大家之所以觉得复杂&#xff0c;是因为落地运用起来很复杂&#xff1b;每个公司&#xff0c;每个业务及产品线的业务流程都不一样&#xff0c;所以就导致了“自动化…

Linux_理解进程地址空间和页表

目录 1、进程地址空间示意图 2、验证进程地址空间的结构 3、验证进程地址空间是虚拟地址 4、页表-虚拟地址与物理地址 5、什么是进程地址空间 6、进程地址空间和页表的存在意义 6.1 原因一&#xff08;效率性&#xff09; 6.2 原因二&#xff08;安全性&#xff09; …

MVC模式中控制器、视图和模型之间的关系如何?

mvc模式将应用程序逻辑与表示层分离&#xff0c;包括控制器、视图和模型三个组件&#xff1a;控制器&#xff1a;协调用户输入&#xff0c;获取模型数据&#xff0c;验证输入&#xff0c;执行业务规则。视图&#xff1a;显示模型数据&#xff0c;不包含业务逻辑。模型&#xff…

如何使用AI解决所有EXCEL公式问题

有个假设前提&#xff0c;你略懂EXCEL公式 知道单元格“ $C1” 和 ”C1”的区别&#xff0c;当然你也可以自行度娘或问AI。 AI使用文心一言免费版方便容易获取。 第一步也是唯一的一步&#xff0c;向AI准确描述你的需求 示例&#xff1a;学生的成绩分布在0-100分之间&#x…

echarts+vue2实战(一)

目录 一、项目准备 二、(横向分页)柱状图 2.1、动态刷新 2.2、UI调整 2.3、分辨率适配 三、(竖向平移)柱状图 3.1、平移动画 3.2、不同数值显示不同颜色 四、(下拉切换)折线图 4.1、切换图表和分辨率适配 4.2、UI调整 五、(三级分类)饼图 5.1、数据切换 六、圆环…

dial tcp 10.96.0.1:443: connect: no route to host

1、创建Pod一直不成功&#xff0c;执行kubectl describe pod runtime-java-c8b465b98-47m82 查看报错 Warning FailedCreatePodSandBox 2m17s kubelet Failed to create pod sandbox: rpc error: code Unknown desc failed to setup network for…

java8 将对象list中的某一个属性取出组成一个list

实体类 public class Sp {String spdm;String spmc;public Sp() {}public Sp(String spdm, String spmc) {this.spdm spdm;this.spmc spmc;}public String getSpdm() {return spdm;}public void setSpdm(String spdm) {this.spdm spdm;}public String getSpmc() {return sp…

太爱这种数据可视化效果,零售行业的都看过来

在当今数字化浪潮下&#xff0c;数据可视化已成为零售行业洞察市场趋势、优化运营决策的关键技术。奥威BI零售数据分析方案凭借其卓越的数据可视化效果&#xff0c;成为零售企业的得力助手。接下来就通过BI节假日分析报表来简单地感受一下。 注&#xff1a;该BI节假日分析报表…

反激开关电源输出电解电容选型及计算

电容高频模型&#xff1a;ESRESLC的串联 1、耐压&#xff1a;根据输出的电压来取&#xff0c;需留一定余量&#xff0c;比如5V输出可以选6.3V或者10V的电解电容 2、容量 纹波电压 电容充放电引起的纹波电压&#xff08;与电容容量存在着直接因果关系&#xff09; ESR引起的纹…

校园任务平台系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;管理员管理&#xff0c;论坛管理&#xff0c;任务咨询管理&#xff0c;用户管理&#xff0c;基础数据管理 前台账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;任务资讯公告&#…

Springboot 实体类赋默认值 @Value 失效? 那怎么搞?

这是最近一个小伙找上来问的问题&#xff0c; 我初一看还没看出来啥猫腻&#xff0c;后面认真一想&#xff0c;决定也写下来记录下&#xff0c;给其他初学者也知道下。 原先思路错误代码&#xff1a; 这个小伙想利用 Value 注解&#xff0c; 给这个属性 赋值&#xff0c;defaul…