Oauth2 初步
搭建一个最简单的 Oauth2 认证服务
基于 Springboot2,在 pom.xml
中引入 Oauth2:
1 |
<dependency> |
在 xxxApplication 上添加 @EnableAuthorizationServer
注解:
1 |
@SpringBootApplication |
启动项目,会在控制台打印:
1 |
security.oauth2.client.client-id = 3c7748b1-ff89-4643-8e16-56fc2ae77a3c |
先使用 Git Bash 进行 token 获取测试:
1 |
$ curl 3c7748b1-ff89-4643-8e16-56fc2ae77a3c:08ed0119-8e8e-4a24-83c7-ac249f8ba1a3@localhost:8888/oauth/token -dgrant_type=client_credentials -dscope=any |
然后使用 postman 进行 token 获取:
这里 Username 相当于 client_id,Password 相当于 client_secret
爬坑:对于 postman 进行测试,困扰了半天,之前都是把 client_id、client_secret、grant_type、scope 字段全部填到 Params 中,然后进行请求返回的数据全部都是:
1
2
3
4
5
6
7
8 >{
> "timestamp": "2019-06-18T06:44:16.443+0000",
> "status": 401,
> "error": "Unauthorized",
> "message": "Unauthorized",
> "path": "/oauth/token"
>}
>大专栏 Oauth2 初步ote>
经过不断尝试,找到需要将 client_id 和 client_secret 整合在一起,然后通过 Base64 加密后连接到 Authorization 后放到 Header 里面进行传输,对应到 postman 中就是在 Authorization 中选择 Basic Auth,填写 Username 和 Password。但是在 Git Bash 中使用 curl 就能够直接进行请求连接,目前还不清楚 curl 请求的时候是如何处理的。
参数设置完成后,发起请求:
根据 Git Bash 和 postman 的请求结果可以看出两次使用相同client_id 和 client_secret 在不同时间发起请求,得到的
access_token
值是一样的,而expires_in
过期时间的值在对应减小。上面使用的是 Oauth2 自动生成的 client_id 与 client_secret,这里我们可以自己定义 client_id 与 client_secret,直接在 application.yml 中添加如下配置:
1
2
3
4
5 security:
oauth2:
client:
client-id: test
client-secret: test启动项目,控制台中打印:
1
2 security.oauth2.client.client-id = test
security.oauth2.client.client-secret = ****使用 postman 做测试,修改其中 Username 和 Password 为 test,发起请求,得到结果:
1
2
3
4
5
6 {
"access_token": "10093ccd-80ec-44c7-84fa-cd75c96a5309",
"token_type": "bearer",
"expires_in": 43199,
"scope": "any"
}到这里就完成了最基本的 Oauth2 授权获取 token,使用的都是 Oauth2 的客户端模式。
后记:
在项目启动的时候总是会出现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 > 2019-06-18 19:08:46.530 INFO 12208 --- [nio-8888-exec-1] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header
> Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
>
> java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
> at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:414) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
> at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
> at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.16.jar:9.0.16]
> at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.16.jar:9.0.16]
> at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.16.jar:9.0.16]
> at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.16.jar:9.0.16]
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_161]
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_161]
> at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.16.jar:9.0.16]
> at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
>这个异常,对于项目的运行目前没有看出有什么影响。
找了很久都未能找到解决方案,后面再继续看看吧。
Oauth2 初步的更多相关文章
- OAuth2授权原理
最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAuth2的授权方式做了些了解. 我还记得一两年前,跟一位同事聊起互联网时,当时我说过一个想法: 目前不少较为稀有的资源,很多都是论坛 ...
- 使用Owin中间件搭建OAuth2.0认证授权服务器
前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...
- [2014-11-11]使用Owin中间件搭建OAuth2.0认证授权服务器
前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...
- 使用SpringSecurity体验OAuth2 (入门2)
本文继续使用SpringSecurity从实战角度对OAuth2进行体验,上一篇 搭建了项目环境,并对配置做了初步分析,分析发现会有两套配置可能在影响OAuth,一个是由授权服务的启动类上的注解@En ...
- OAuth2.0配置
一:授权服务器相关代码 AuthorizationServer.java import org.springframework.beans.factory.annotation.Autowired; ...
- oauth2 Spring Security
oauth2四种授权方式小结 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html 密码模式(resource owner pas ...
- Spring Security 实战干货:客户端OAuth2授权请求的入口
1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...
- 移动端之Android开发的几种方式的初步体验
目前越来越多的移动端混合开发方式,下面列举的大多数我都略微的尝试过,就初步的认识写个简单的心得: 开发方式 开发环境 是否需要AndroidSDK 支持跨平台 开发语言&技能 MUI Win+ ...
- Spring Security OAuth2 开发指南
官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...
随机推荐
- mysql第四篇:数据操作之单表查询
单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- WEB一周总结(1)待补充
1.网页设计作业--小组介绍 图片来自https://weibo.com/hxLMo?sudaref=www.baidu.com&display=0&retcode=6102 2.WE ...
- ORACLE自增函数,一般函数
1.UNIX时间与普通时间互相转换 1.ORACLE先创建函数方法,再直接使用,MySQL直接使用方法UNIX_TIMESTAMP,FROM_UNIXTIME oracle_to_unix(creat ...
- 19 01 04 CSS3 圆角 grba(带通明的) tansition动画 transform变换 animation动画
CSS3圆角 设置某一个角的圆角,比如设置左上角的圆角:border-top-left-radius:30px 60px; 同时分别设置四个角: border-radius:30px 60px 120 ...
- PHP 的变量类型,变量检测
1.PHP的变量类型: 整型 浮点型 字符串 布尔型 数组 对象 null 资源类型 一个变量就是一个盒子,类型可以看做盒子的标签,变量的值就是盒子里的内容 null 是没有类型的空盒子, ...
- Scheduled定时任务器在Springboot中的使用
Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...
- 《C Primer Plus》- 第二章 C语言概述
本笔记写于2020年1月27日. 本系列文章参考的是<C Primer Plus>(第六版),其中里面会有笔者自己的相关补充. 以下示例均运行于macOS Catalina 10.15.2 ...
- Api_hook 拦截 messageBox 等函数
library hookdll; uses SysUtils, Windows, Classes, unitHook in 'unitHook.pas'; {$R *.res} const HOOK_ ...
- servlet-api api文档获取请求参数
1.假如有个get请求后面带有的参数如下: a=b&a2=b2&a3=b3&a4=b4. 如果想获取所有的key,value.这个时候可以根据request的getQueryS ...