All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.common.util.CommonServletUtil Maven / Gradle / Ivy

package com.nxyfan.framework.common.util;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.nxyfan.framework.common.exception.CommonException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HttpServlet工具类,获取当前request和response
 *
 * @author amour
 * @date 2020/3/30 15:09
 */
public class CommonServletUtil {

    /**
     * 从请求中中获取参数
     *
     * @author amour
     * @date 2021/10/14 10:44
     **/
    public static String getParamFromRequest(String paramName) {
        HttpServletRequest request = getRequest();

        // 1. 尝试从请求体里面读取
        String paramValue = request.getParameter(paramName);

        // 2. 尝试从header里读取
        if (ObjectUtil.isEmpty(paramValue)) {
            paramValue = request.getHeader(paramName);
        }
        // 3. 尝试从cookie里读取
        if (ObjectUtil.isEmpty(paramValue)) {
            Cookie[] cookies = request.getCookies();
            if(ObjectUtil.isNotEmpty(cookies)) {
                for (Cookie cookie : cookies) {
                    String cookieName = cookie.getName();
                    if (cookieName.equals(paramName)) {
                        return cookie.getValue();
                    }
                }
            }
        }
        // 4. 返回
        return paramValue;
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes;
        try {
            servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        }catch(Exception e) {
            e.printStackTrace();
            throw new CommonException("非Web上下文无法获取Request");
        }
        if(servletRequestAttributes == null) {
            throw new CommonException("非Web上下文无法获取Request");
        }else {
            return servletRequestAttributes.getRequest();
        }
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes;
        try {
            servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException("非Web上下文无法获取Response");
        }
        if (servletRequestAttributes == null) {
            throw new CommonException("非Web上下文无法获取Response");
        } else {
            return servletRequestAttributes.getResponse();
        }
    }

    public static boolean isWeb() {
        return RequestContextHolder.getRequestAttributes() != null;
    }
    
    /**
     * 
     * Describe: 通过Host去判断发起请求的ip是否为内网
     * Author: Administrator  
     * Create Time: 2024年4月28日 上午8:42:39   
     * @return 是否为内网
     */
    public static boolean isInnerIP() {
    	HttpServletRequest request = CommonServletUtil.getRequest();
        String ip = request.getHeader("Host").split(":")[0];// 
        if("127.0.0.1".equals(ip) || "localhost".equals(ip)) {
        	return true;
        }
		boolean isInnerIP = false;
		try {
			isInnerIP = NetUtil.isInnerIP(ip);
		}catch(Exception e) {
		}
		return isInnerIP;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy