当前位置: 首页 > news >正文

SpringSecurity6-oauth2-三方gitee授权-授权码模式

文章目录

      • 1.登录gitee,创建一个第三方登录的应用
      • 2.引入pom
      • 3.application.yml
      • 4.配置文件
      • 5.测试

1.登录gitee,创建一个第三方登录的应用

首先找到设置中的第三方应用:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.引入pom

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

3.application.yml

spring:security:oauth2:client:registration:gitee:#应用idclient-id: 创建的应用中获取xxxxxxxxxxxxx#应用密钥client-secret: 创建应用中获取xxxxxxxxx#应用名称client-name: MyLinging#授权后重定向urlredirect-uri: http://127.0.0.1:8080/login/oauth2/code/gitee#授权码模式authorization-grant-type: authorization_codeprovider:gitee:# Gitee 的授权 URLauthorization-uri: https://gitee.com/oauth/authorize# token获取urltoken-uri: https://gitee.com/oauth/token# 用户信息获取urluser-info-uri: https://gitee.com/api/v5/user# 码云用户信息中的用户名字段 这里就取id作为唯一标志user-name-attribute: id

4.配置文件

httpSecurity.oauth2Login(cus -> cus//登录认证入口(前端发起请求的路径)//例如:http://127.0.0.1:8080/oauth2/authorization/gitee.authorizationEndpoint(authorization -> authorization.baseUri("/oauth2/authorization"))//用户确认后,第三方服务器回调的路径.redirectionEndpoint(redirection -> redirection.baseUri("/login/oauth2/code/*"))// 处理第三方返回的用户信息.userInfoEndpoint(userInfo -> userInfo.userService(new DefaultOAuth2UserService()))// 三方认证成功后重定向的地址,此处本机测试,搭建前后端分离项目,授权后重定向前端首页地址,仅后端测试可注释掉不配置.defaultSuccessUrl("http://127.0.0.1:8081")
)

5.测试

在这里插入图片描述

  1. 点击gitee登录,访问登录授权的入口:http://127.0.0.1:8080/oauth2/authorization/gitee

  2. 访问后会重定向到gitee授权页面:https://gitee.com/oauth/authorize?response_type=code&client_id=xxxxxx&state=oihEUItlxWQEEsnz2in1VSo9w3Za7WUcwcShzQ1_IRg%3D&redirect_uri=http://127.0.0.1:8080/login/oauth2/code/gitee
    在这里插入图片描述

  3. 当用户点击同意授权时,会向gitee授权服务器请求授权获取code:https://gitee.com/oauth/authorize
    在这里插入图片描述

  4. 请求授权完后,回调到:http://127.0.0.1:8080/login/oauth2/code/gitee?code=xxxxxxxxxxx&state=oihEUItlxWQEEsnz2in1VSo9w3Za7WUcwcShzQ1_IRg%3D
    然后由springsecurity的OAuth2LoginAuthenticationFilter进行拦截,由OAuth2LoginAuthenticationProviderOAuth2AuthorizationCodeAuthenticationProvider根据code向gitee进行认证拿到token,然后根据token由DefaultOAuth2UserService的loadUser向gitee获取用户的信息,并封装DefaultOAuth2User对象保存在SecurityContextHolder中,然后认证完就重定向到首页。这里设置了重定向地址是http://127.0.0.1:8081/,如下:
    在这里插入图片描述

http://www.lqws.cn/news/572977.html

相关文章:

  • JavaScript中的回调函数详解
  • 【鸿蒙中级】
  • 微信小程序接入腾讯云短信验证码流程
  • window11 本地安装 MySQL8.0
  • 【QT】第一个QT程序 || 对象树 || 编码时的注意事项
  • 运维基础-MYSQL数据库-笔记
  • stm32 USART串口协议与外设(程序)——江协教程踩坑经验分享
  • Java面试宝典:基础六
  • MCU、LIN收发器与LIN总线是如何进行交互的?
  • stm32之测量周期
  • WPF学习笔记(13)列表框控件ListBox与数据模板
  • 开发中bs,cs都是什么意思
  • 视频跳帧播放器设计与实现
  • [Linux] PXE
  • HttpServletRequest
  • OpenCV 4.10.0 移植
  • Spring Cloud:服务监控与追踪的高级实践
  • C++ 第四阶段 STL 容器 - 第一讲:详解 std::vector
  • 5 c++核心——文件操作
  • restful规范
  • Oauth2 自定义设置token过期时间
  • HarmonyOS 公共事件机制介绍以及多进程之间的通信实现(9000字详解)
  • 【网络】:DNS协议、ICMP协议、NAT技术
  • MongoDB06 - MongoDB 地理空间
  • vllm部署私有智谱大模型
  • 疏通经脉: Bridge 联通逻辑层和渲染层
  • 模拟多维物理过程与基于云的数值分析-AI云计算数值分析和代码验证
  • 生物实验室安全、化学品安全
  • 【notes2】并发,IO,内存
  • 30套精品论文答辩开题报告PPT模版