jwt Cookie 无密码解码方式

jwt Cookie 无密码解码方式

pom 依赖

<dependency>
   <groupId>com.auth0</groupId>
   <artifactId>java-jwt</artifactId>
   <version>3.3.0</version>
</dependency>

原本使用base64 解码中间段,但是发现部分Cookie的中间段无法直接使用base64直接界面,会报错 。遂改为一下方式。

public static SecUser getLoginUser() {
        SecUser secUser = new SecUser();
        HttpServletRequest request = getHttpServletRequest();
        if(request==null){
            return secUser;
        }
        Cookie[] cookies = request.getCookies();
        if(cookies==null){
            return secUser;
        }
        for(Cookie cookie:cookies){
            if("SUNWAY_JWT".equals(cookie.getName())&&StringUtils.isNotEmpty(cookie.getValue())){
                String value = cookie.getValue();
                // String[] split = value.split("\\.");
                // split[1];
                // 解码Base64字符串
                try {
                    // byte[] decodedBytes = Base64.getDecoder().decode(split[1]);
                    // String json = new String(decodedBytes);
                    // JSONObject jsonObject = new JSONObject(json);
                    DecodedJWT jwt = JWT.decode(value);
                    String aud = jwt.getClaim("aud").asString();
                    String[] split1 = aud.split("@");
                    if(split1.length>2){
                        secUser.setUserId(split1[0]);
                        secUser.setUserName(split1[split1.length-1]);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值