java获取request的请求头信息

不墨迹,直接上代码”

Java Web 中通过HttpServletRequest 获取请求头信息以及客户端真实IP

Controller

package com.example.controller;

import com.example.service.RetIpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName RequestIpController
 * @Description TODO
 * @Date 2021/11/12 上午11:56
 */
@RestController
@RequestMapping(value = "/reslut/ip")
@Api(value = "用户请求信息" , description = "用户请求信息")
public class RetIpController {

    @Autowired
    private RetIpService reslutIpService;

    @RequestMapping(value = "/getHeadersInfoAndIp", method = RequestMethod.GET)
    @ApiOperation(value = "获取请求用户的 请求头信息以及ip")
    public Object getHeadersInfoAndIp(HttpServletRequest request){
        return reslutIpService.retIpInfo(request);
    }

}

service

package com.example.service.serviceImpl;

import com.example.service.RetIpService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ReslutIpServiceImpl
 * @Description TODO
 * @Author 
 * @Date 2021/11/12 上午11:58
 */
@Service
public class RetIpServiceImpl implements RetIpService {

    @Override
    public Map<String, Object> retIpInfo(HttpServletRequest request) {
        String ip = this.getIpAddr(request);
        Map map = this.getHeadersInfo(request);
        map.put("ip",ip);
        return map;
    }

    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "本地";
        }
        return ip;
    }

    public Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

}

查看输出结果:

{
  "sec-fetch-mode": "cors",
  "referer": "http://127.0.0.1:8080/swagger-ui.html",
  "sec-fetch-site": "same-origin",
  "accept-language": "zh-CN",
  "ip": "127.0.0.1",
  "host": "127.0.0.1:8080",
  "connection": "keep-alive",
  "accept-encoding": "gzip, deflate",
  "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0",
  "accept": "*/*",
  "sec-fetch-dest": "empty"
}

如果需要获取单个value:

System.out.println(map.get("host"));
输出:127.0.0.1:8080

你可能感兴趣的:(java,后端)