SpringSecurityOAuth2(3)自定义token信息

GitHub地址

码云地址

OAuth2默认的token返回最多只携带了5个参数(client_credentials模式只有4个 没有refresh_token)下面是一个返回示例:

1{ 2 "access_token": "1e93bc23-32c8-428f-a126-8206265e17b2", 3 "token_type": "bearer", 4 "refresh_token": "0f083e06-be1b-411f-98b0-72be8f1da8af", 5 "expires_in": 3599, 6 "scope": "auth api" 7}

然后我们需要的token可能需要增加username等自定义参数:

1{ 2 "access_token": "1e93bc23-32c8-428f-a126-8206265e17b2", 3 "token_type": "bearer", 4 "refresh_token": "0f083e06-be1b-411f-98b0-72be8f1da8af", 5 "expires_in": 3599, 6 "scope": "auth api", 7 "username":"username" 8}

具体实现自定义token步骤如下: 新建一个自定义token信息的新建自定义token返回MyTokenEnhancer实现TokenEnhancer接口重写enhance方法:

1/** 2 * @Description 自定义token返回值 3 * @Author wwz 4 * @Date 2019/07/31 5 * @Param 6 * @Return 7 */ 8public class MyTokenEnhancer implements TokenEnhancer { 9 @Override 10 public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { 11 User user = (User) authentication.getPrincipal(); 12 final Map<String, Object> additionalInfo = new HashMap<>(); 13 additionalInfo.put("username", user.getUsername()); 14 ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); 15 return accessToken; 16 } 17}

然后在认证服务配置AuthorizationServerEndpointsConfigurer中加上 MyTokenEnhancer。这里划重点 因为我这里指定了了defaultTokenServices()所以得在这个方法里加上配置

还有,如果已经生成了一次没有自定义的token信息,需要去redis里删除掉该token才能再次测试结果,不然你的结果一直是错误的,因为token还没过期的话,是不会重新生成的。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringSecurityOAuth2(9) feign 微服务间调用 token验证

GitHub地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fweizongwu%2FSpringCloudOAuth2SpringSecurityFrame.git"项目地址")码云地址(https://gitee.com/wujishu/

SpringSecurityOAuth2(5)自定义登录登出

GitHub地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fweizongwu%2FSpringCloudOAuth2SpringSecurityFrame.git"项目地址")码云地址(https://gitee.com/wujishu/

SpringSecurityOAuth2(8) swagger2 集成OAuth2

GitHub地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fweizongwu%2FSpringCloudOAuth2SpringSecurityFrame.git"项目地址")码云地址(https://gitee.com/wujishu/