搭建一个最简单的 Oauth2 认证服务

基于 Springboot2,在 pom.xml 中引入 Oauth2:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.3.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>

在 xxxApplication 上添加 @EnableAuthorizationServer 注解:

1
2
3
4
5
6
7
8
9
@SpringBootApplication
public class { public static void main(String[] args) {
SpringApplication.run(SecurityOauth2Application.class, args);
} }

启动项目,会在控制台打印:

1
2
security.oauth2.client.client-id = 3c7748b1-ff89-4643-8e16-56fc2ae77a3c
security.oauth2.client.client-secret = 08ed0119-8e8e-4a24-83c7-ac249f8ba1a3

先使用 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 初步的更多相关文章

  1. OAuth2授权原理

    最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAuth2的授权方式做了些了解. 我还记得一两年前,跟一位同事聊起互联网时,当时我说过一个想法: 目前不少较为稀有的资源,很多都是论坛 ...

  2. 使用Owin中间件搭建OAuth2.0认证授权服务器

    前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...

  3. [2014-11-11]使用Owin中间件搭建OAuth2.0认证授权服务器

    前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...

  4. 使用SpringSecurity体验OAuth2 (入门2)

    本文继续使用SpringSecurity从实战角度对OAuth2进行体验,上一篇 搭建了项目环境,并对配置做了初步分析,分析发现会有两套配置可能在影响OAuth,一个是由授权服务的启动类上的注解@En ...

  5. OAuth2.0配置

    一:授权服务器相关代码 AuthorizationServer.java import org.springframework.beans.factory.annotation.Autowired; ...

  6. oauth2 Spring Security

    oauth2四种授权方式小结 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html 密码模式(resource owner pas ...

  7. Spring Security 实战干货:客户端OAuth2授权请求的入口

    1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...

  8. 移动端之Android开发的几种方式的初步体验

    目前越来越多的移动端混合开发方式,下面列举的大多数我都略微的尝试过,就初步的认识写个简单的心得: 开发方式 开发环境 是否需要AndroidSDK 支持跨平台 开发语言&技能 MUI Win+ ...

  9. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

随机推荐

  1. CountDownLatch和CyclicBarrier和Semaphore最通俗形象解释

    应该还有好多同学对这三个的区别比较模糊,网络上其他文章说的也比较专业化.所以我在这里举个例子说明这三个的区别. 我们假定有一场百米比赛,比赛包括十个运动员和一个裁判,每个运动员和每个裁判都是一个线程, ...

  2. HTML-基础标记

    HTML, 一种超文本标记语言,顾名思义,要比文本的样式多,而且是由标记组成,还是一门语言. 标记写法 <标记名> <a></a>双标记 超链接 <br /& ...

  3. 工程日记之HelloSlide(3):如何使用Core Data数据库,以及和sqlite之间的对应关系

    Core Data 和 SQLite 是什么关系 core data是对sqlite的封装,因为sqlite是c语言的api,然而有人也需要obj-c的api,所以有了core data ,另外,co ...

  4. Delphi生成即调用带窗体的Dll

    library frmDll; { Important note about DLL memory management: ShareMem must be the first unit in you ...

  5. 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL

    在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...

  6. GBDT入门

    GBDT(MART)迭代决策树入门教程 GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree) ...

  7. Linux相关笔记

    vim下 r /etc/hosts  会把这个文件读进来 r! df -Th  会把执行的内容读取进来 查找 /  ? 替换:s/old/new/g 2到9行替换2,9s/old/new/g 全部替换 ...

  8. vue安装插件指定版本

    安装插件指定版本 npm install 插件名称@2.9.6 --save 查看需要安装插件的版本记录 npm view 插件名称 versions --json

  9. 选择排序&冒泡排序

    //直接选择排序 #include<stdio.h> void SelectionSort(int arr[],int len) { int i,j; int k,min; int tem ...

  10. tomcat添加ssl证书

    Tomcat支持JKS格式证书,从Tomcat7开始也支持PFX格式证书,两种证书格式任选其一. 文件说明: 1. 证书文件xxx.pem,包含两段内容,请不要删除任何一段内容. 2. 如果是证书系统 ...