zoukankan      html  css  js  c++  java
  • 从cookies 获取token

    从cookie中获取token,在根据token获取想要的数据

    public String getToken2(HttpServletRequest request) throws Exception {
    try {
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
    for (Cookie cookie : cookies) {
    if ("token".equals(cookie.getName())) {
    String token = cookie.getValue();
    HttpHeaders headers = new HttpHeaders();
    RestTemplate restTemplate = new RestTemplate();
    headers.put(HttpHeaders.COOKIE, Collections.singletonList("token=" + token));
    HttpEntity<String> requestEntity = new HttpEntity<>("", headers);
    ResponseEntity<String> responseEntity = restTemplate.exchange(accessTokenUrl, HttpMethod.GET, requestEntity, String.class);
    String body = responseEntity.getBody();
    JSONObject bodyObj = JSON.parseObject(body);
    Integer code = bodyObj.getInteger("code");
    if (code == 200 ) {
    JSONObject object = bodyObj.getJSONObject("member");
    String uid = object.getString("uid");
    return uid;
    }
    break;
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
  • 相关阅读:
    缺陷笔记
    Eclipse中常用快捷键
    PL/SQL中复制粘贴表结构信息
    request之setAtrribute
    list+map
    套接字初始化失败问题
    上机编程题(2016校招)
    动态规划
    VS中碰到的问题
    IE8添加元素报错《没有权限》错误
  • 原文地址:https://www.cnblogs.com/xiaoxiaojuan/p/9172739.html
Copyright © 2011-2022 走看看