SpringBoot Security的oauth2四种授权模式使用

oauth2四种使用方式

密码模式

localhost:8080/oauth/token?client_id=client_id_1&client_secret=123456&grant_type=password&username=admin&password=123456
{
	access_token: "b909b1e6-bf10-4c13-bef7-bfca64211feb",
	token_type: "bearer",
	refresh_token: "521c491d-ef73-45af-aefb-36c5c66cbe89",
	expires_in: 3599,
	scope: "read write"
}

简化模式

1、http://localhost:8080/login 输入账号密码

2、再次输入如下地址,获取token
http://localhost:8080/oauth/authorize?response_type=token&client_id=client_id_1&redirect_uri=http://example.com&scope=write 
地址栏重定向如下
http://example.com/#access_token=2820363f-2e2f-4485-8892-64683895675e&token_type=bearer&expires_in=3512

授权码模式

1、http://localhost:8080/login 输入账号密码

2、再次输入如下地址,获取code
http://localhost:8080/oauth/authorize?response_type=code&client_id=client_id_1&redirect_uri=http://example.com&scope=write
地址栏重定向如下
http://example.com/?code=n9WlYp 

3、http://localhost:8080/oauth/token?grant_type=authorization_code&client_id=client_id_1&client_secret=123456&redirect_uri=http://example.com&code=n9WlYp
{
    "access_token": "812403de-548d-4939-b0fa-57b1ec8abb11",
    "token_type": "bearer",
    "refresh_token": "8a8a7ebb-6f73-452d-9582-2fbf4ff80815",
    "expires_in": 99,
    "scope": "write"
}

客户端模式

localhost:8080/oauth/token?client_id=client_id_1&client_secret=123456&grant_type=client_credentials
{
	access_token: "097db1fd-20a1-4a8f-a7a9-8d500309b12e",
	token_type: "bearer",
	expires_in: 3590,
	scope: "read write"
}

refresh token

$ curl -X GET "localhost:8080/oauth/token?client_id=client_id_1&client_secret=123456&grant_type=refresh_token&refresh_token=fca82077-720e-46da-9c88-c2f221b0cb46"

{"access_token":"0274a51f-f61b-4bb0-9ae4-b99ef508c91e","token_type":"bearer","refresh_token":"fca82077-720e-46da-9c88-c2f221b0cb46","expires_in":99,"scope":"read write"}

使用token获取资源

$ curl  -H "Authorization:bearer 284a5718-0a80-4eab-9d04-1bda3b6ceb62" -X GET  http://localhost:8080/user/get
{"error":"invalid_token","error_description":"Invalid access token: 284a5718-0a80-4eab-9d04-1bda3b6ceb62"}
或者
$ curl -X GET  http://localhost:8080/user/get?access_token=0f22ff90-1834-4654-9576-8737ec84d61e

代码地址

https://github.com/mozovw/delicacy-grape/tree/master/grape-oauth

oauth2使用场景

授权码方式的使用场景

进入csdn首页登陆,我们这里使用微博登陆
在这里插入图片描述
点击微博图标,会重定向到如下页面,看到吗?这就是我们熟悉的/oauth/authorize接口地址。如果我们通过手机中的微博扫码登陆后,也就是相当于账号密码登陆,成功后也就会重定向到redirect_url所对应的地址,格式:地址?code=xxxxxx,下面自然就是通过授权码登陆的方式获取access_token。

在这里插入图片描述
然后在进一步,请求获取微博中的账户信息,下面就是看看微博中的账户信息和自己系统的账户信息绑定,已绑定,则表示你登陆成功。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Security OAuth2 是 Spring Security 的子项目,用于支持 OAuth2 协议。其源码包括了授权服务器和资源服务器两部分,下面分别介绍。 ## 授权服务器源码 Spring Security OAuth2 的授权服务器源码主要包括以下几个模块: 1. oauth2-core:包括 OAuth2 协议的核心实现,如授权码、令牌等的生成和验证。 2. oauth2-jwt:包括 JWT 令牌的生成和解析。 3. oauth2-authorization-server:包括授权服务器的实现,包括授权模式、密码模式、客户端模式等。 其中,授权服务器的实现是基于 Spring MVC 的,主要包括以下几个核心类: 1. AuthorizationEndpoint:处理授权端点,包括授权模式、密码模式、客户端模式等。 2. TokenEndpoint:处理令牌端点,包括颁发访问令牌、刷新令牌等。 3. WhitelabelApprovalEndpoint:处理用户授权页面,提供用户授权功能。 ## 资源服务器源码 Spring Security OAuth2 的资源服务器源码主要包括以下几个模块: 1. oauth2-resource:包括资源服务器的核心实现,如令牌解析和访问控制等。 2. oauth2-jwt:包括 JWT 令牌的生成和解析。 其中,资源服务器的实现是基于 Spring Security 的,主要包括以下几个核心类: 1. ResourceServerConfigurerAdapter:用于配置资源服务器,包括资源的访问控制、令牌的解析等。 2. JwtAccessTokenConverter:用于将 JWT 令牌转换为 OAuth2 令牌。 以上是 Spring Security OAuth2 的授权服务器和资源服务器的主要源码模块和类,具体实现方式还需要根据实际需求进行具体的配置和实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值