当前位置: 首页 > news >正文

Spring Security Oauth2 JWT 添加额外信息

目录

一、问题描述

二、实现步骤

1、自定义TokenEnhancer

2、配置授权服务器

3、自定义UserDetails的User类

三、参考文档


一、问题描述

Oauth2里默认生成的JWT信息并没有用户信息,在认证授权后一般会返回这一部分信息,我对此进行了改造。

Oauth 2.0 JWT 默认返回 OAuth2AccessToken 接口的实现类,默认实现类是 DefaultOAuth2AccessToken,返回字段有 5 个:

access_token:表示访问令牌,必选项
token_type:表示令牌类型,该值大小写不敏感,必选项,默认是 bearer 类型
expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。
refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。
scope:表示权限范围,如果与客户端申请的范围一致,此处可省

改造后,最终实现效果:

可以看到额外信息已添加上。

二、实现步骤

1、自定义TokenEnhancer

public TokenEnhancer customTokenEnhancer() {return (accessToken, authentication) -> {Map<String, Object> additionalInfo = new HashMap<>();Object principal = authentication.getPrincipal();try {String s = objectMapper.writeValueAsString(principal);Map<?, ?> map = objectMapper.readValue(s, Map.class);/* 移除不需要的属性 */map.remove("accountNonExpired");map.remove("accountNonLocked");map.remove("credentialsNonExpired");map.remove("enabled");additionalInfo.put("data", map);} catch (JsonProcessingException e) {log.error("", e);}((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);return accessToken;};}

2、配置授权服务器

    @Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {TokenEnhancerChain enhancerChain = new TokenEnhancerChain();enhancerChain.setTokenEnhancers(Arrays.asList(customTokenEnhancer(), jwtAccessTokenConverter())); //token转换器DefaultTokenServices tokenServices = new DefaultTokenServices();tokenServices.setTokenEnhancer(enhancerChain); //token拓展链tokenServices.setTokenStore(jwtTokenStore()); //采用JWT存储token/* 开启refresh_token */tokenServices.setReuseRefreshToken(true);tokenServices.setSupportRefreshToken(true);endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService).tokenStore(jwtTokenStore()) //采用JWT存储token.tokenServices(tokenServices);super.configure(endpoints);}

3、自定义UserDetails的User类

@Getter
@Setter
@ApiModel("登录用户对象")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class JwtUser<T> extends User {@ApiModelProperty("账号信息")private T info;@ApiModelProperty("菜单")private List<TreeNode<SysMenu>> menus;public JwtUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {super(username, password, authorities);}public JwtUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);}
}

并自定义UserDetailsService并在返回时返回自定义对象即可。

三、参考文档

https://www.cnblogs.com/kuangdaoyizhimei/p/14279979.html

Spring Security JWT 添加额外信息_jwts增加参数-CSDN博客

http://www.lryc.cn/news/344241.html

相关文章:

  • 蜜蜂收卡系统 加油卡充值卡礼品卡自定义回收系统源码 前后端开源uniapp可打包app
  • 三星硬盘好还是西数硬盘好?硬盘数据丢失怎么找回
  • 企业微信hook接口协议,ipad协议http,设置是否自动同意
  • 自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
  • h5页面用js判断机型是安卓还是ios,判断有app安装没app跳转应用商店app stroe或者安卓应用商店
  • 算法人生(17):从“课程学习”到“逐步暴露心理疗法”
  • C++仿函数周边及包装器
  • 改进灰狼算法优化随机森林回归预测
  • Hadoop生态系统的核心组件探索
  • 命令行方式将mysql数据库迁移到达梦数据库(全步骤)
  • 旅游系列之:庐山美景
  • 杭州恒生面试,社招,3年经验
  • python virtualenv 创建虚拟环境指定python版本,pip 从指定地址下载某个包
  • open feign支持调用form-data的接口
  • ESD静电问题 | TypeC接口整改
  • 基于springboot+mybatis+vue的项目实战之前端
  • 开源软件托管平台gogs操作注意事项
  • Linux cmake 初窥【3】
  • centos学习- ps命令详解-进程监控的利器
  • C++贪心算法
  • 访问网络附加存储:nfs
  • jsp 实验12 servlet
  • 「 网络安全常用术语解读 」通用配置枚举CCE详解
  • 一机游领航旅游智慧化浪潮:借助前沿智能设备,革新旅游服务效率,构建高效便捷、生态友好的旅游服务新纪元,开启智慧旅游新时代
  • 设计模式学习笔记 - 项目实战三:设计实现一个支持自定义规则的灰度发布组件(实现)
  • BJFUOJ-C++程序设计-实验2-类与对象
  • 数据库语法复习
  • Tomcat、MySQL、Redis最大支持说明
  • MATLAB数值计算工具箱介绍
  • 2023 广东省大学生程序设计竞赛(部分题解)