使用oauth2时,如果令牌失效了,可以使用刷新令牌通过refresh_token的授权模式再次获取access_token。只需修改认证服务器的配置,添加refresh_token的授权模式即可。
修改授权服务器配置,增加refresh_token配置
@Autowired
private UserService userService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean)
.reuseRefreshTokens(false)
.userDetailsService(userService)
.allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST);
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
*授权码模式
*http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://www.baidu.com&scope=all
*http://localhost:8080/oauth/authorize?response_type=code&client_id=client
* password模式
* http://localhost:8080/oauth/token?username=fox&password=123456&grant_type=password&client_id=client&client_secret=123123&scope=all
* 客户端模式
* http://localhost:8080/oauth/token?grant_type=client_credentials&scope=all&client_id=client&client_secret=123123
clients.inMemory()
.withClient("client")
.secret(passwordEncoder.encode("123123"))
.accessTokenValiditySeconds(3600)
.refreshTokenValiditySeconds(864000)
.redirectUris("http://www.baidu.com")
.scopes("all")
* 配置grant_type,表示授权类型
* authorization_code: 授权码
* password: 密码
* client_credentials: 客户端
* refresh_token: 更新令牌
.authorizedGrantTypes("authorization_code","password","client_credentials","refresh_token");
https://gitee.com/zjj19941/ZJJ_Neaten5.10/tree/master/ZJJ_SpringCloud_Oauth2/demo03
get请求:
http://localhost:8080/oauth/token?username=fox&password=123456&grant_type=password&client_id=client&client_secret=123123&scope=all
结果:
可以发现access_token的有效时间只有59秒
"access_token": "f2eec987-bc8a-404d-9d77-3eecfe2c5fb0",
"token_type": "bearer",
"refresh_token": "162b1497-8880-4cc2-afe1-2c8bba82026c",
"expires_in": 59,
"scope": "all"
当token过期之后用过期的token去访问我们的目标服务的时候会报下面的错误

get请求: http://localhost:8080/oauth/token?grant_type=refresh_token&client_id=client&client_secret=123123&refresh_token=162b1497-8880-4cc2-afe1-2c8bba82026c
"access_token": "982e6883-9f03-4840-9869-cbaba5414339",
"token_type": "bearer",
"refresh_token": "6bf21179-5e36-4306-9cdb-979db69612f1",
"expires_in": 59,
"scope": "all"
http://localhost:8080/oauth/token?grant_type=refresh_token&client_id=client&client_secret=123123&refresh_token=162b1497-8880-4cc2-afe1-2c8bba82026c
如果refresh_token已经被使用了一次了,第二次使用的时候会返回下面的结果:
"error": "invalid_grant",
"error_description": "Invalid refresh token: 162b1497-8880-4cc2-afe1-2c8bba82026c"
设置refresh_token是否重复使用 reuseRefreshTokens(false) false就是不可以重用,
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean)
.tokenStore(tokenStore)
.reuseRefreshTokens(false)
.userDetailsService(userService)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
概述使用oauth2时,如果令牌失效了,可以使用刷新令牌通过refresh_token的授权模式再次获取access_token。只需修改认证服务器的配置,添加refresh_token的授权模式即可。修改授权服务器配置,增加refresh_token配置@Autowiredprivate UserService userService;@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoint
refreshToken刷新token后会变化的原因以及refreshToken的复用
两次使用refreshToken刷新token返回的refreshToken如下:
将两次返回的refreshToken解密后可以看到。处理ati不一样,其它参数都是一样的
ati(access token):新的访问令牌的id
jti : refreshToken 的id
既然刷出了新的token,那么ati肯定变了,那么由这些信息加密成的refreshToken自然也是一直变化的
那么如果想无限制刷新refres
文章目录1. OAuth2介绍2. OAuth2的四种授权模式①:授权码模式②:密码模式③:简化(隐式)模式④:客户端模式3. 令牌token的携带方式
1. OAuth2介绍
OAuth2并不是一个框架,而是一个关于授权(authorization)的开放网络标准,是一个授权协议!OAuth在全世界得到广泛应用,目
Spring Cloud Security Oauth2.0
Spring 提供的 Spring Security oAuth2 能搭建一套验证授权及资源访问服务,帮助大家在实现企业微服务架构时能够有效的控制多个服务的统一登录、授权及资源保护工作。
这里只是一个简单的案例基础,我自己是参照 千峰李哥的博客 上面的笔记来学习的,通过自己的整理与案例学习,综合出了一个自己风格的项目
李哥视频教程
Sp...
在学习oauth2.0协议的时候,对于刷新令牌refresh token感觉很困惑。主要是为啥需要刷新令牌,以及刷新令牌是如何工作的,技术细节是啥?比如通过refresh token可以让access token永久不过期吗?
下面就针对这两个问题进行分析。
为什么需要刷新令牌
如果access token超时时间很长,比如14条,由于第三方软件获取受保护资源都要带着access token,这样access token的攻击面就比较大。
如果access token超时时间很短,比如1个小时,那其超时之后
最近看人人网的OAuth认证,发现他是OAuth2.0,之前一直看的是新浪的OAuth,是OAuth1.0.
二者还是有很多不同的,主要的不同点在access token的获取方式.
OAuth1.0的access token获取过来之后,就可以存到数据库里,然后长期使用,因为它有效期很长,通常有效期
之前写过一篇“Oauth2.0实现token刷新功能”,发现大家阅读的还是特别多,那个是基于Spring Cloud实现的刷新功能,现在将它改为基于普通过滤器实现token刷新。
直接上代码
基本思路还是跟之前一样,过滤器拦截请求验证token,token过期后请求单点登录服务器换取新的access_token和refresh_token,将两个token放到过滤器返回体的头部。从而不影响本次请求,同时还能在本次请求的过程中做到无痕刷新token。
@Override
access token 是应用方访问资源服务器的接口时,需要提供的一个令牌。拥有这个令牌代表着得到用户的授权。然而,这个授权应该是临时的,有一定有效期。这是因为,access token 在使用的过程中可能会泄露。给 access token 限定一个较短的有效期可以降低因 access token 泄露而带来的风险。
然而引入了有效期之后,应用方使用起来就不那么方便了。每当 ...
oauth2如何
refreshToken?
post请求,跟请求
token一样的url地址:
http://localhost:9098/oauth/
token
post需要的参数:
grant_type :
refresh_
token”
client_id:分配的客户端id
client_secret:分配的客户端密码
refresh_
token:值为上一次请求返回值中"
refresh_
token
实例如下所示
oauth2配置文件需要设置支持
refreshToken
@Override
作为一个小白,在做微信第三方登录的时候遇到的一些问题总结了一下:
问题:access_token与refresh_token之为什么要用refresh_token刷新不重新获取access_token?
网上挺多回答的,但是我都觉得不是很满意,所以我就自己总结了一下;
原因是access_token只保存2个小时,而refresh_token保存30天;
当access_tok
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.maxIdle' in stri
19118