当前位置: 代码网 > it编程>编程语言>Java > Spring中获取HttpServletRequest的三种方式小结

Spring中获取HttpServletRequest的三种方式小结

2026年04月14日 Java 我要评论
一、简要概述spring框架web环境中,获取httpservletrequest是常见的操作,下面我们以获取客户端ip功能为例,给出获取httpservletrequest对象的几种方式。二、3种方

一、简要概述

spring框架web环境中,获取httpservletrequest是常见的操作,下面我们以获取客户端ip功能为例,给出获取httpservletrequest对象的几种方式。

二、3种方式

1、直接在请求方法参数中声明

    string[] ip_headers = {"x-forwarded-for", "x-real-ip", "proxy-client-ip", "wl-proxy-client-ip", "http_x_forwarded_for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", "http_forwarded_for", "http_forwarded", "http_via", "remote_addr"};
    
    @getmapping({"/", "/index"})
    public string index(httpservletrequest request)
    {
        return stringutils.join("client ip: ", getclientip(request));
    }
    
    /**
     * 获取客户端真实ip地址
     */
    private string getclientip(httpservletrequest request)
    {
        for (string header : ip_headers)
        {
            string ip = request.getheader(header);
            if (ip != null && !ip.isempty() && !"unknown".equalsignorecase(ip))
            {
                string[] ips = ip.split(",");
                log.info("##### ip header: {}", header);
                return ips[0].trim();
            }
        }
        log.info("##### request.getremoteaddr");
        return request.getremoteaddr();
    }

2、注入方式获取

    @autowired
    httpservletrequest request;
    
    string[] ip_headers = {"x-forwarded-for", "x-real-ip", "proxy-client-ip", "wl-proxy-client-ip", "http_x_forwarded_for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", "http_forwarded_for", "http_forwarded", "http_via", "remote_addr"};
    
    @getmapping({"/", "/index"})
    public string index()
    {
        return stringutils.join("client ip: ", getclientip(request));
    }
    
    /**
     * 获取客户端真实ip地址
     */
    private string getclientip(httpservletrequest request)
    {
        for (string header : ip_headers)
        {
            string ip = request.getheader(header);
            if (ip != null && !ip.isempty() && !"unknown".equalsignorecase(ip))
            {
                // 处理多个ip的情况(逗号分隔)
                string[] ips = ip.split(",");
                log.info("##### ip header: {}", header);
                return ips[0].trim();
            }
        }
        log.info("##### request.getremoteaddr");
        return request.getremoteaddr();
    }

或者

    @autowired
    httpservletrequest request;
    
    string[] ip_headers = {"x-forwarded-for", "x-real-ip", "proxy-client-ip", "wl-proxy-client-ip", "http_x_forwarded_for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", "http_forwarded_for", "http_forwarded", "http_via", "remote_addr"};
    
    @getmapping({"/", "/index"})
    public string index()
    {
        return stringutils.join("client ip: ", getclientip());
    }
    
    /**
     * 获取客户端真实ip地址
     */
    private string getclientip()
    {
        for (string header : ip_headers)
        {
            string ip = request.getheader(header);
            if (ip != null && !ip.isempty() && !"unknown".equalsignorecase(ip))
            {
                // 处理多个ip的情况(逗号分隔)
                string[] ips = ip.split(",");
                log.info("##### ip header: {}", header);
                return ips[0].trim();
            }
        }
        log.info("##### request.getremoteaddr");
        return request.getremoteaddr();
    }

3、隐式从上下文获取

    @getmapping({"/", "/index"})
    public string index()
    {
        return stringutils.join("client ip: ", getclientip());
    }
    
    string[] ip_headers = {"x-forwarded-for", "x-real-ip", "proxy-client-ip", "wl-proxy-client-ip", "http_x_forwarded_for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", "http_forwarded_for", "http_forwarded", "http_via", "remote_addr"};
    
    /**
     * 获取客户端真实ip地址
     */
    private string getclientip()
    {
        httpservletrequest request = gethttpservletrequest();
        for (string header : ip_headers)
        {
            string ip = request.getheader(header);
            if (ip != null && !ip.isempty() && !"unknown".equalsignorecase(ip))
            {
                // 处理多个ip的情况(逗号分隔)
                string[] ips = ip.split(",");
                log.info("##### ip header: {}", header);
                return ips[0].trim();
            }
        }
        log.info("##### request.getremoteaddr");
        return request.getremoteaddr();
    }
    
    private httpservletrequest gethttpservletrequest()
    {
        servletrequestattributes servletrequestattributes = (servletrequestattributes)requestcontextholder.getrequestattributes();
        if (servletrequestattributes != null)
        {
            return servletrequestattributes.getrequest();
        }
        return null;
    }

大家可以根据需要选择其中一种使用。

到此这篇关于spring中获取httpservletrequest的三种方式小结的文章就介绍到这了,更多相关spring获取httpservletrequest内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com