个性化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

Spring Security OAuth 个性化token相关推荐

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

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

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

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

  3. Spring Security OAuth笔记

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

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. 女程序员也有 35 岁危机焦虑吗?
  2. linux shell 把一个文件的前n行 拷贝到另一个文件中
  3. 洲明科技与意法半导体合作开发新一代LED显示屏
  4. electron 改变窗体 大小_「Science子刊」约翰·霍普金斯大学创造灰尘大小设备,可协助药物在胃肠道停留24小时之久...
  5. linux机器的物理内存监控,Linux内存监控工具
  6. 2022考研计算机-数据库原理教程1-7章
  7. Struts action-mapping 元素讲解
  8. 201521123033《Java程序设计》第5周学习总结
  9. Linux-lsof
  10. Logit模型拟合实战案例(Biogeme)
  11. 2010年6月16日最新NOD32激活码和升级ID(含ESS/EAV)
  12. Python 房贷计算器小工具
  13. 5个聚合导航网站,资源、工具、素材应有尽有
  14. 研究生师兄谈SCI论文写作心得
  15. Quartz 使用(二) — quartz.properties配置
  16. window 使用rabbitMQ 报错Applying plugin configuration to rabbit@... failed.
  17. 【机器学习】简述多种降维算法
  18. 上班人员必读:“五险一金”详解!
  19. Redis --- 超级详细
  20. **组播PIM-SM详解****

热门文章

  1. linux修改文件权限属性,如何修改Linux文件的属性与权限
  2. 网络编程--connect()、listen()、accept()
  3. OVS ovs-vsctl(二十五)
  4. RESTful 规范
  5. 阻止默认行为是配合passive使用
  6. CodeSalt | Python数据结构的实现 — 链表
  7. 新口令范筹(Token Scope)- viewables:read
  8. 瓜子二手车CEO杨浩涌:创业要建立势能,瓜子的技术能力是护城河,“瓜子大脑”能预测成交概率...
  9. 如何用敏捷消除项目风险?
  10. 项目问题思考之策略模式