(资料图片)
记录下点进链接或访问网页设备的公网IP地址;我记得这个当时这是个AI帮我写的,挺好用!
package com.example.myweb.controller;import com.example.myweb.entiy.Ip;import com.example.myweb.mapper.IpMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController@RequestMapping("/get")public class GetIp { @Autowired private IpMapper ipMapper; @Autowired private Ip ips; //获取访问者IP @RequestMapping("/ip") public void getIp(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(); } //获取当前日期精确到秒 String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()); //保存IP即可 }}