从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;
}
posted on 2018-06-12 14:13  爱上码  阅读(7213)  评论(0编辑  收藏  举报