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

package com.zeekr.hr.recruitment.share.util;

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) {
                switch(cookie.getName()){
                    case "Authorization":
                        token = cookie.getValue();
                        if (StringUtils.isBlank(token)) {
                            continue;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        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")) {
            response.setStatus(HttpServletResponse.SC_OK);
        }
        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.
https://en.qdmana.com/2022/119/202204290618309156.html