current position:Home>Tool class for obtaining specific information in HTTP request

Tool class for obtaining specific information in HTTP request

2022-04-29 08:45:56White Java notes


import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

 * @author xxx
 * @date 2020/9/2  from  http request Tool class for obtaining specific information in 
public class RequestInfoUtil {

    private static final String TOKEN_HEADER_KEY = "Authorization";

    private static final String REFERER_HEADER_KEY = "Referer";

    private static final String ORIGIN_HEADER_KEY = "Origin";

    private static final String SOURCE_TYPE_HEADER_KEY = "source-type";

    private static final String APP_HEADER_KEY = "userId";

    private static final String SIGNATURE_HEADER_KEY = "signature";

    public static String getToken(HttpServletRequest request) {
       // String token = request.getHeader(TOKEN_HEADER_KEY);
        Cookie[] cookies = request.getCookies();
        String token = "";
        if (Objects.nonNull(cookies)&&cookies.length>0){
            for (Cookie cookie : cookies) {
                    case "Authorization":
                        token = cookie.getValue();
                        if (StringUtils.isBlank(token)) {
        if (StringUtils.isBlank(token)){
            token = request.getHeader(TOKEN_HEADER_KEY);
        return token;

    public static String getRefererHeader(HttpServletRequest request) {
        String referer = request.getHeader(REFERER_HEADER_KEY);
        return referer;

    public static String getOriginHeader(HttpServletRequest request){
        return request.getHeader(ORIGIN_HEADER_KEY);

    public static String getSourceType(HttpServletRequest request, HttpServletResponse response) {
        if (request.getMethod().equals("OPTIONS")) {
        String sourceType = request.getHeader(SOURCE_TYPE_HEADER_KEY);
        return sourceType;

    public static String getAppId(HttpServletRequest request) {
        String referer = request.getHeader(APP_HEADER_KEY);
        return referer;

    public static String getSignature(HttpServletRequest request) {
        String referer = request.getHeader(SIGNATURE_HEADER_KEY);
        return referer;


copyright notice
author[White Java notes],Please bring the original link to reprint, thank you.