zoukankan      html  css  js  c++  java
  • jwt扩展

    1、新建扩展类

    package com.ireciting.uaaservice.config;
    
    import com.ireciting.uaaservice.pojo.TUser;
    import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
    import org.springframework.security.oauth2.common.OAuth2AccessToken;
    import org.springframework.security.oauth2.provider.OAuth2Authentication;
    import org.springframework.security.oauth2.provider.token.TokenEnhancer;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class CustomTokenEnhancer implements TokenEnhancer {
    
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            TUser user = (TUser) authentication.getPrincipal();
            final Map<String, Object> additionalInfo = new HashMap<>();
            additionalInfo.put("user_id", user.getUserId());
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    
            return accessToken;
        }
    }

    2、资源服务器获取扩展信息

    新建类转换类

    package com.ireciting.noteservice.config;
    
    import org.springframework.security.oauth2.provider.OAuth2Authentication;
    import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
    import org.springframework.stereotype.Component;
    
    import java.util.Map;
    /**
     * @author Lv
     * @version 1.0
     * @Description: 定制 AccessToken 转换器,为添加额外信息在服务器端获取做准备
     * @date 2019/6/21 18:51
     */
    @Component
    public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
    
        @Override
        public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
            OAuth2Authentication authentication
                    = super.extractAuthentication(claims);
            authentication.setDetails(claims);
            return authentication;
        }
    }

    遍历获取

    /**
         * 获取UserID
         * @return
         */
        public static long getCurrentUserID() {
            try {
                OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
                Map<String, Object> map = (Map<String, Object>) details.getDecodedDetails();
                for (String key : map.keySet()) {
                    if (key.equals("user_id"))
                        return (Long) map.get(key);
                }
            }
            catch (Exception e){
                return -1L;
            }
            return -1L;
        }
  • 相关阅读:
    C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表
    C#中 @ 的3种用途
    有关于 使用 命名管道 进行网络 进程间通信 的资料收集
    MySql LAST_INSERT_ID 【插入多条数据时】
    两个“不合理继承 ”的判定标识
    MYSQL 函数 字符串到整数
    Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程
    重构,拥有多个构造函数(重载)的类
    vue二级联动select
    gulp.dest用法详解
  • 原文地址:https://www.cnblogs.com/lvlv/p/11066379.html
Copyright © 2011-2022 走看看