java获取request的请求头信息

java获取request的请求头信息,第1张

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;


@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;


@Service
public class RetIpServiceImpl implements RetIpService {

    @Override
    public Map 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 getHeadersInfo(HttpServletRequest request) {
        Map map = new HashMap();
        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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5482833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存