搭建一个最简单的 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. 尝试用kotlin做一个app(一)

    1.先添加一下anko库 依赖:implementation "org.jetbrains.anko:anko:$anko_version" 版本:ext.anko_version ...

  2. Multiarmed Bandit Algorithm在股票中的应用

    股票与Bandit Machine看起来相去甚远,但实际上通过限制买入和卖出的行为,股票可以转换为Bandit Machine,比如:规定股票必须在买入一天以后卖出.为什么要大费周折地把股票变成Ban ...

  3. 一名资深架构师规划Java程序员五年职业生涯指南

    每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天我给大家分享的是一篇来自阿里大牛对五年工作经验程序员的职业建议,希望 ...

  4. 进度1_家庭记账本App

    今天完成了昨天的初步构想,详细介绍见上一篇博客,具体项目结构和案例如下: MainActivity.java: package com.example.familybooks; import andr ...

  5. Vue.js 之 过渡动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. xhell ctrl+s 假死

    如题. ctrl+q 即可接触 假死状态. 记录下

  7. UML-架构分析-步骤

    1.识别->因素表 2.解决->技术备忘录 1).可靠性 2).法律问题 3).可适应性

  8. [De1CTF 2019]SSRF Me-MD5长度扩展攻击&CVE-2019-9948

    0x00 打开题目查看源代码,开始审计 这里贴上网上师傅的博客笔记: https://xz.aliyun.com/t/6050 #! /usr/bin/env python #encoding=utf ...

  9. 图论中TSP问题的LINGO求解与应用

    巡回旅行商问题(Traveling Salesman Problem,TSP),也称为货郎担问题.该问题可简单描述为走遍n个城市的最短路.几十年来,出现了很多近似优化算法.如近邻法.贪心算法.最近插入 ...

  10. matlab 高级

    绘图 条形图 x = [1:10]; y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95]; bar(x,y), xlabel('Student'),ylabel( ...