当前位置: 首页 > 工具软件 > Token Field > 使用案例 >

获取token的两种方法

何烨华
2023-12-01

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");
        }
 类似资料: