ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OAuth2.0用refresh_token更新access_token令牌

2022-02-07 19:31:17  阅读:484  来源: 互联网

标签:http refresh access token client type id


概述

使用oauth2时,如果令牌失效了,可以使用刷新令牌通过refresh_token的授权模式再次获取access_token。只需修改认证服务器的配置,添加refresh_token的授权模式即可。
修改授权服务器配置,增加refresh_token配置

@Autowired
private UserService userService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManagerBean) //使用密码模式需要配置
        // .tokenStore(tokenStore)  //指定token存储到redis
        .reuseRefreshTokens(false)  //refresh_token是否重复使用
        .userDetailsService(userService) //刷新令牌授权包含对用户信息的检查
        .allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST); //支持GET,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()
        //配置client_id
        .withClient("client")
        //配置client-secret
        .secret(passwordEncoder.encode("123123"))
        //配置访问token的有效期
        .accessTokenValiditySeconds(3600)
        //配置刷新token的有效期
        .refreshTokenValiditySeconds(864000)
        //配置redirect_uri,用于授权成功后跳转
        .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

测试

获取token

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去访问user服务

当token过期之后用过期的token去访问我们的目标服务的时候会报下面的错误
image.png

刷新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"
}

用新的token就可以正常的访问user服务了

image.png

注意,refresh_token可以设置是否重用

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 {
        // 认证管理器去SpringSecurity做用户名密码认证.
        endpoints.authenticationManager(authenticationManagerBean) //使用密码模式需要配置
                // token存储策略 , 指定token存储到redis
                .tokenStore(tokenStore)
                .reuseRefreshTokens(false)  //refresh_token是否重复使用
                .userDetailsService(userService) //刷新令牌授权包含对用户信息的检查
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST); //支持GET,POST请求
    }

标签:http,refresh,access,token,client,type,id
来源: https://blog.csdn.net/qq_41489540/article/details/122813497

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有