为什么80%的码农都做不了架构师?>>>   

个性化Token 目的

  • 默认通过调用 /oauth/token 返回的报文格式包含以下参数
{"access_token": "e6669cdf-b6cd-43fe-af5c-f91a65041382","token_type": "bearer","refresh_token": "da91294d-446c-4a89-bdcf-88aee15a75e8","expires_in": 43199, "scope": "server"
}

并没包含用户的业务信息比如用户信息、租户信息等。

  • 扩展生成包含业务信息(如下),避免系统多次调用,直接可以通过认证接口获取到用户信息等,大大提高系统性能
{"access_token":"a6f3b6d6-93e6-4eb8-a97d-3ae72240a7b0","token_type":"bearer","refresh_token":"710ab162-a482-41cd-8bad-26456af38e4f","expires_in":42396,"scope":"server","tenant_id":1,"license":"made by pigx","dept_id":1,"user_id":1,"username":"admin"
}

密码模式生成Token 源码解析

​ 主页参考红框部分

  • ResourceOwnerPasswordTokenGranter (密码模式)根据用户的请求信息,进行认证得到当前用户上下文信息

    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());String username = parameters.get("username");String password = parameters.get("password");// Protect from downstream leaks of passwordparameters.remove("password");Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password);((AbstractAuthenticationToken) userAuth).setDetails(parameters);userAuth = authenticationManager.authenticate(userAuth);OAuth2Request storedOAuth2Request =  getRequestFactory().createOAuth2Request(client, tokenRequest);       return new OAuth2Authentication(storedOAuth2Request, userAuth);
    }
    
  • 然后调用AbstractTokenGranter.getAccessToken() 获取OAuth2AccessToken

    protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest) {return tokenServices.createAccessToken(getOAuth2Authentication(client, tokenRequest));
    }
    
  • 默认使用DefaultTokenServices来获取token

    public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {... 一系列判断 ,合法性、是否过期等判断    OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);tokenStore.storeAccessToken(accessToken, authentication);// In case it was modifiedrefreshToken = accessToken.getRefreshToken();if (refreshToken != null) {tokenStore.storeRefreshToken(refreshToken, authentication);}return accessToken;
    }
  • createAccessToken 核心逻辑

    // 默认刷新token 的有效期
    private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; // default 30 days.
    // 默认token 的有效期
    private int accessTokenValiditySeconds = 60 * 60 * 12; // default 12 hours.private OAuth2AccessToken createAccessToken(OAuth2Authentication authentication, OAuth2RefreshToken refreshToken) {DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(uuid);token.setExpiration(Date)token.setRefreshToken(refreshToken);token.setScope(authentication.getOAuth2Request().getScope());return accessTokenEnhancer != null ? accessTokenEnhancer.enhance(token, authentication) : token;
    }
    

    如上代码,在拼装好token对象后会调用认证服务器配置TokenEnhancer( 增强器) 来对默认的token进行增强。

  • TokenEnhancer.enhance 通过上下文中的用户信息来个性化Token

    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {final Map<String, Object> additionalInfo = new HashMap<>(8);PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();additionalInfo.put("user_id", pigxUser.getId());additionalInfo.put("username", pigxUser.getUsername());additionalInfo.put("dept_id", pigxUser.getDeptId());additionalInfo.put("tenant_id", pigxUser.getTenantId());additionalInfo.put("license", SecurityConstants.PIGX_LICENSE);((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);return accessToken;
    }
    

基于pig 看下最终的实现效果

Pig 基于Spring Cloud、oAuth2.0开发基于Vue前后分离的开发平台,支持账号、短信、SSO等多种登录,提供配套视频开发教程。
https://gitee.com/log4j/pig

转载于:https://my.oschina.net/giegie/blog/3011343

Spring Security OAuth 个性化token 1相关推荐

  1. Spring Security OAuth 个性化token

    个性化Token 目的 默认通过调用 /oauth/token 返回的报文格式包含以下参数 {"access_token": "e6669cdf-b6cd-43fe-af ...

  2. Spring Security OAuth 2开发者指南译

    Spring Security OAuth 2开发者指南译 介绍 这是用户指南的支持OAuth 2.0.对于OAuth 1.0,一切都是不同的,所以看到它的用户指南. 本用户指南分为两部分,第一部分为 ...

  3. spring security oauth rce (cve-2016-4977) 漏洞分析

    0x00 漏洞概述 1.漏洞简介 Spring Security OAuth是为Spring框架提供安全认证支持的一个模块,在7月5日其维护者发布了这样一个升级公告,主要说明在用户使用Whitelab ...

  4. Spring Security OAuth笔记

    因为工作需要,系统权限安全方面可能要用到Spring Security OAuth2.0,所以,近几天了解了一下OAuth相关的东西.目前好像还没有系统的学习资料,学习主要是通过博客,内容都是大同小异 ...

  5. 源码分析 - Spring Security OAuth2 生成 token 的执行流程

    说明 本文内容全部基于 Spring Security OAuth2(2.3.5.RELEASE). OAuth2.0 有四种授权模式, 本文会以 密码模式 来举例讲解源码. 阅读前, 需要对 OAu ...

  6. Spring Boot+Spring Security+JWT 实现token验证

    Spring Boot+Spring Security+JWT 实现token验证 什么是JWT? JWT的工作流程 JWT的主要应用场景 JWT的结构 SpringBoot+Spring Secur ...

  7. 单点登录(shiro与Spring Security OAuth 2.0的集成)

    单点登录(shiro与Spring Security OAuth 2.0的集成) shiro项目采用ruoyi,OAuth采用pig 若依:https://gitee.com/y_project/Ru ...

  8. Spring Security OAuth2.0 token生成与刷新机制源码阅读

    一.介绍 Spring Security Oauth2是目前市面上非常流行的实现了OAuth2.0协议的权限框架.本文会介绍其是如何获取token以及刷新token的. 二.AbstractEndPo ...

  9. 后端架构token授权认证机制:spring security JSON Web Token(JWT)简例

    后端架构token授权认证机制:spring security JSON Web Token(JWT)简例 在基于token的客户端-服务器端认证授权以前,前端到服务器端的认证-授权通常是基于sess ...

  10. Spring Security——OAuth 2.0 Client自动配置源代码分析

    基本概念 OAuth2.0:OAuth2.0是OAuth协议的延续版本,但不向前兼容OAuth 1.0(即完全废止了OAuth1.0). OAuth 2.0关注客户端开发者的简易性.要么通过组织在资源 ...

最新文章

  1. python调用js库中的函数_Python 调用JS文件中的函数
  2. python删除列表中的重复元素并保持相对顺序不变
  3. RunTime技术总结
  4. MATLAB从入门到精通-最速下降算法、牛顿算法、BFGS拟牛顿算法、共轭梯度算法无约束极值问题
  5. 160个Crackme028之对抗花指令
  6. linux visudo配置sudoers:免密码使用root权限
  7. 类视图ListView和DetailView比较
  8. css3 的 calc()函数在布局中的使用----头部高度固定,页面正好占满一屏
  9. container 的背后
  10. KD Tree的原理及Python实现
  11. magento smtp设置
  12. 教你一个小技巧给latex表格添加脚注 (非footnote)
  13. java-se-包装类
  14. 贯头山酒——中华酒文化的源头之一
  15. Python检验多重共线性
  16. iOS 制作圆形头像图片
  17. WireShark教程 – 黑客发现之旅(5) – (nmap)扫描探测
  18. 用VBA实现对一维数组的排序(7)堆排序
  19. 增长黑客:如何低成本实现爆发式增长
  20. centos下查看服务器型号命令

热门文章

  1. Pod install 慢, pod update 慢, Cocoapods setup下载缓慢,手动解决方案
  2. matlab 风机 功率曲线,风力发电机功率曲线统计MATLAB代码实现.docx
  3. STL Container
  4. 433.最小基因变化
  5. sqlite 表与表之间的关系_第33章 Django多表关系之一对一
  6. hdu acm1157
  7. 线性代数第八章 λ 矩阵 定理8 多项式最大公约数的性质
  8. Linux微信群shell,linux shell基础
  9. LeetCode-两数之和(Java) 记录下刷题的第一天以及近期迷茫感受
  10. Codeforce 1800Difficulty Graphs 20 questions