1.通过Cookie
private String getUserCookieToken(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies == null || cookies.length == 0) {
return null;
}
for (Cookie cookie : cookies) {
String name = cookie.getName();
if (name.equals("token")) {
return cookie.getValue();
}
}
return null;
}
2.通过Request
String token = request.getHeader("token");
log.info("get token from header:{}", token);
if (token == null) {
token = getUserCookieToken(request);
log.info("get token from cookie:{}", token);
}
if (token == null) {
token = request.getParameter("token");
}