获取进来的用户信息

/**

  • 全局user

  • @author 刘洋

  • @date 2020-04-30
    */
    public class LocalUser {
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    public static void set(YxUser user, Integer scope) {
    Map<String, Object> map = new HashMap<>();
    map.put(“user”, user);
    map.put(“scope”, scope);
    LocalUser.threadLocal.set(map);
    }

    public static void clear() {
    LocalUser.threadLocal.remove();
    }

    public static YxUser getUser() {
    Map<String, Object> map = LocalUser.threadLocal.get();
    YxUser user = (YxUser)map.get(“user”);
    return user;
    }

    public static Integer getScope() {
    Map<String, Object> map = LocalUser.threadLocal.get();
    Integer scope = (Integer)map.get(“scope”);
    return scope;
    }

    public static Long getUidByToken(){
    String bearerToken = RequestUtils.getRequest().getHeader(“Authorization”);
    if (StringUtils.isEmpty(bearerToken)) {
    return 0L;
    }

     if (!bearerToken.startsWith("Bearer")) {
         return 0L;
     }
     String[] tokens = bearerToken.split(" ");
     if (!(tokens.length == 2)) {
         return 0L;
     }
     String token = tokens[1];
    
     Optional<Map<String, Claim>> optionalMap = JwtToken.getClaims(token);
     Map<String, Claim> map = optionalMap
             .orElseThrow(() -> new UnAuthenticatedException(ApiCode.UNAUTHORIZED));
    
     return  map.get("uid").asLong();
    

    }
    }

获取进来的用户的 IP地址

/**

  • 请求详情信息

  • @author niuniu

  • @date 2020-04-30
    */
    @Data
    @Accessors(chain = true)
    public class RequestDetail implements Serializable {
    private static final long serialVersionUID = 2543641512850125440L;

    /**

    • 请求ip地址
      */
      private String ip;

    /**

    • 请求路径
      */
      private String path;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值