搭建一个最简单的 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. ETL优化

    ETL优化 Extract.Transform.Load,对异构数据源进行数据处理. 设立基线标准,根据硬盘.网络传输速度,多测测量得到数据量(m)/时间(s)的比值,找线性关系.建立基线作为调试和优 ...

  2. c++的符号表的肤浅认识

    符号表是编译期产生的一个hash列表,随着可执行文件在一起 示例程序 int a = 10; int b; void foo(){ static int c=100; } int main(){ in ...

  3. Java 14 都要来了,为什么还有这么多人固守Java8?

    作者:刘欣 从Java 9开始,Java版本的发布就让人眼花缭乱了. 每隔6个月,都会冒出一个新版本出来,Java 10 , Java 11, Java 12, Java 13, 到2020年3月份, ...

  4. POJ 1840:Eqs 哈希求解五元方程

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14169   Accepted: 6972 Description ...

  5. Mysql--主库不停机搭建备库

    参考:http://blog.csdn.net/luozuolincool/article/details/38494817 mysqldump --skip-lock-tables --single ...

  6. Hexo博客NexT主题美化之评论系统

    前言 更多效果展示,请访问我的博客 https://kangmingxian.github.io/ 效果图:   image Valine 诞生于2017年8月7日,是一款基于Leancloud的快速 ...

  7. 每天一杯C_Visual Studio各个版本的区别和总结

    细致区别如上图所示 企业版点满图中技能树,能够提供点对点的解决方案,充分满足正规企业的要求. PS:技能最多,肯定也就价格最贵 专业版中提供的专业开发者工具.服务和订阅就成了最佳选择. PS:技能多, ...

  8. u-boot中filesize环境变量【转载】

    转载地址:https://blog.csdn.net/fzs333/article/details/48518559 U-Boot中的环境命令可以使用$(filesize)来确定刚下载(传输)得到的文 ...

  9. CentOS8上用Docker部署开源项目Tcloud

    一.安装Docker 1.我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上) root账户登录,查 ...

  10. PAT Basic 1043 输出PATest (20分)[Hash散列]

    题目 给定⼀个⻓度不超过10000的.仅由英⽂字⺟构成的字符串.请将字符重新调整顺序,按"PATestPATest-."这样的顺序输出,并忽略其它字符.当然,六种字符的个数不⼀定是 ...