OAuth2 JustAuth实战
1.创建第三方平台应用,申请配置信息
基本上按照这篇博客的教程完成。
以github为例,在developer settings–>OAuth App上申请第三方应用,获得配置信息,以下是注册好应用后的浏览信息页面:
2.加入JustAuth工具类
2.1创建spring boot项目,加入pring-boot-starter-web和lombok依赖
2.2加入JustAuth依赖
// ...<dependency><groupId>me.zhyd.oauth</groupId><artifactId>JustAuth</artifactId><version>1.15.1</version></dependency>// ...
最终获得pom.xml如下
<?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.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>me.zhyd.oauth</groupId><artifactId>JustAuth</artifactId><version>1.15.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.简洁的三部曲
添加一个Controller类,用于处理页面和后端之间的请求,
3.1 申请授权页面
由于集成的是github,所以请求链接的动态参数{source}传入的是github,所以我们会在 http://localhost:8080/oauth/render/github上请求授权。
@RequestMapping("/render/{source}")public void renderAuth(HttpServletResponse response) throws IOException {AuthRequest authRequest = getAuthRequest();String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());response.sendRedirect(authorizeUrl);}
3.2 获取AuthRequest,生成授权url
因为是向Github请求授权,所以直接实例化AuthGithubRequest( ),创建授权申请时填入之前在Github上获得的客户端Id和Secret,以及授权完毕后的跳转url。
3.3认证登录
在授权页面同意授权后,会跳转到redirect_url也就是http://localhost:8080/oauth/callback/github,并携带code、state等参数,callback是页面request的入参。
@RequestMapping("/callback/{resource}")public Object login(AuthCallback callback) {AuthRequest authRequest = getAuthRequest();return authRequest.login(callback);}
回调时会报告Http实现类未指定,这个在之前的教程中也给出了原因和解决方案,加入依赖即可。
会返回如图页面,这表示github授权成功啦。url中包含了code和state,response会返回一个JSON,accesstoken就是OAuth的通行令牌,可以通过它有限定期限地访问github中授权用户的信息。
上面的教程还提出了state的自定义redis缓存和集成gitlab私服,在yt的项目中都有运用到。
OAuth2 JustAuth实战相关推荐
- OAuth2简易实战(四)-Github社交联合登录
1. OAuth2简易实战(四)-Github社交联合登录 1.1. 用到的第三方插件 https://github.com/spring-projects/spring-social-github ...
- java github关联登录_没错,用三方Github做授权登录就是这么简单!(OAuth2.0实战)...
全2册git版本控制管理(第2版)+ 99.8元 包邮 (需用券) 去购买 > 本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种 ...
- 没错,用三方 Github 做授权登录就是这么简单!(OAuth2.0实战)
本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种授权方式>文末说过,后续要来一波OAuth2.0实战,耽误了几天今儿终于补上了. ...
- OAuth2.0实战
微信开发三大坑: 1.微信OAuth2.0授权 2.微信jssdk签名 3.微信支付签名 本篇先搞定微信OAuth2.0授权吧! 实际一旦了解微信的OAuth2.0,其他的也掌握了. 以简书的登陆页面 ...
- OAuth2学习(二)——OAuth2实战
在上一篇文章中我们讲解了OAuth2的一些基本概念,对OAuth2有了基本的认识.这一节内容我们就讲解一下OAuth2实战,围绕OAuth2的基本类型进行实战讲解. 授权码模式(authorizati ...
- Spring Cloud微服务网关Zuul过滤链和整合OAuth2+JWT入门实战
一.Spring Cloud Zuul 过滤链 1.1 工作原理 Zuul的核心逻辑是由一系列的Filter来实现的,他们能够在进行HTTP请求或者相应的时候执行相关操作.Zuul Filter的主要 ...
- OAuth2之授权服务
趁着五一假期学习了OAuth2 传送门 下载源码 OAuth2简易实战(一)-四种模式 https://www.cnblogs.com/sky-chen/archive/2019/03/13/1052 ...
- Spring security 集成 JustAuth 实现第三方授权登录
Spring security 集成 JustAuth 实现第三方授权登录脚手架: 一.特性 spring security 集成 JustAuth 实现第三方授权登录 : 此项目从 用户管理脚手架( ...
- 详细介绍OAuth2.0及实现和SpringSecurity的整合应用
一.OAuth2.0介绍 源码获取请加V :boge_java 1.概念说明 先说OAuth,OAuth是Open Authorization的简写. OAuth协议为用户资源的授权提供了一个 ...
最新文章
- python怎么变白-python – 在热图中使反向对角线变白
- 为何红黄蓝一边在被做空,一边在逆势上涨?
- ubuntu 配置minicom 进行串口开发
- Ubuntu出现没有正确安装GNOME电源管理器的默认配置
- mvn 命令向本地仓库上传 jar
- 蓝桥杯 ADV-103 算法提高 逆序排列
- 概率论与数理统计(二)选择题
- VUE项目 高德地图使用热力图--实例
- mhdd硬盘测试软件,mhdd硬盘检测工具使用攻略 mhdd硬盘检测工具如何使用
- 高一计算机课程教案,高一信息技术《信息及其特征》教案
- jmail邮件服务器,asp+JMAIL实现发送邮件
- php模拟post提交 在线,curl post请求 , postman 模拟请求 , 在线测试工具模拟请求...
- cmd 实用快捷键。。
- Warshall传递闭包算法
- 脖子黑色素沉淀怎么去除,有效方法
- 百家号室内设计行业粉丝数量统计(2019.12.24)
- java导出excel报表_java生成excel报表文件示例
- 焦虑症和植物神经紊乱是同一种疾病吗
- linux eclipse某些项目,某些项目无法导入,因为它们已存在于Eclipse中的工作空间错误中...
- 将文字定位到浮动图片上|CSS