栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java获取request的请求头信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号