当前位置:   article > 正文

Java中使用HttpServletRequest获取ip和端口-详解_java的request获取用户请求后端接口的ip地址

java的request获取用户请求后端接口的ip地址

目录

1.简单例子

2.详细使用例子---Java项目

2.1.Controller层

2.2service层

2.2.1接口

2.2.2serviceImpl实现类

3.IpUtils工具类


1.简单例子

也可以直接用

  1. public void doGet(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. String ServerName = request.getServerName();//返回服务器的主机名
  4. String ServerPort = request.getServerPort();//返回服务器的端口号
  5. String uri = request.getRequestURI();//返回请求行中的资源名称
  6. String url = request.getRequestURL().toString();//获得客户端发送请求的完整url
  7. String ip = request.getRemoteAddr();//返回发出请求的IP地址
  8. String params = request.getQueryString();//返回请求行中的参数部分
  9. String host=request.getRemoteHost();//返回发出请求的客户机的主机名
  10. int port =request.getRemotePort();//返回发出请求的客户机的端口号
  11. System.out.println(ServerName);
  12. System.out.println(ServerPort);
  13. System.out.println(ip);
  14. System.out.println(url);
  15. System.out.println(uri);
  16. System.out.println(params);
  17. System.out.println(host);
  18. System.out.println(port);
  19. }

2.详细使用例子---Java项目

真正用到在serviceImpl实现类中

2.1.Controller层

  1. @RestController
  2. @RequestMapping("/api")
  3. public class DataReportController {
  4. private final DataReportService dataReportService;
  5. private final BusinessMsgCodeProperties props;
  6. @Autowired
  7. public DataReportController(DataReportService dataReportService, BusinessMsgCodeProperties props){
  8. this.dataReportService = dataReportService;
  9. this.props = props;
  10. }
  11. /**
  12. * 数据上报接⼝
  13. */
  14. @PostMapping("/dataReport")//这个接口既传文件参数又传实体实参,request不是参数,前端不传后端可自行获取
  15. public Resource<String> dataReport(@RequestPart(value = "file", required = true) MultipartFile file, @RequestPart @Validated DataReportInDto dto, HttpServletRequest request){
  16. String code = dataReportService.dataReport(file,dto,request);
  17. return new Resource<>(props.getProcessStatus(code)); //Resource为自行封装的返回类型,可以用不用
  18. }
  19. }

2.2service层

2.2.1接口

  1. public interface DataReportService extends IService<ReportData> {
  2. /**
  3. * 数据上报接⼝
  4. */
  5. String dataReport(MultipartFile file, DataReportInDto dto, HttpServletRequest request);
  6. }

2.2.2serviceImpl实现类

  1. @Service
  2. @Slf4j
  3. public class DataReportServiceImpl extends ServiceImpl<DataReportMapper, ReportData> implements DataReportService {
  4. private final DataReportMapper dataReportMapper;
  5. @Autowired
  6. public DataReportServiceImpl(DataReportMapper dataReportMapper){
  7. this.dataReportMapper = dataReportMapper;
  8. }
  9. @Override
  10. public String dataReport(MultipartFile file, DataReportInDto dto, HttpServletRequest request) {
  11. //一、获取调用者的ip+port
  12. String ip = "";
  13. String port = "";
  14. String loginName = "";
  15. try {
  16. ip = IpUtils.getRemoteHost(request); //自己封装一个工具类(对特殊情况做了一定处理)
  17. String ip1 = request.getRemoteAddr(); //直接获取
  18. port = String.valueOf(request.getRemotePort());
  19. System.out.println("远程端的ip1:"+ip1);
  20. System.out.println("远程的端口:"+ip);
  21. System.out.println("远程端口:"+port);
  22. log.info("获取调用者ip:"+ip+" ,"+"port:"+port);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. log.info("获取调用者ip+port失败");
  26. }
  27. return "COMMON_ERROR";
  28. }
  29. }

3.IpUtils工具类

直接整个粘贴就能用

  1. package com.nengyy.speed_rest_server.utils; //所在包
  2. import org.apache.commons.lang3.StringUtils;
  3. import javax.servlet.http.HttpServletRequest;
  4. public class IpUtils {
  5. public static String getRemoteHost(HttpServletRequest request){
  6. String ip = request.getHeader("x-forwarded-for");
  7. if(StringUtils.isNoneBlank(ip) && !"unknown".equalsIgnoreCase(ip)){
  8. int idx = ip.indexOf(",");
  9. if(idx != -1){
  10. ip = ip.substring(0, idx);
  11. }
  12. }
  13. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
  14. ip = request.getHeader("Proxy-Client-IP");
  15. }
  16. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
  17. ip = request.getHeader("WL-Proxy-Client-IP");
  18. }
  19. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
  20. ip = request.getRemoteAddr();
  21. }
  22. return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/751902
推荐阅读
相关标签
  

闽ICP备14008679号