国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

springboot 獲取訪問接口的請求的IP地址的實現

瀏覽:40日期:2023-02-22 13:38:54

工具類:

import javax.servlet.http.HttpServletRequest;import java.net.InetAddress;import java.net.UnknownHostException; /** * @Author : JCccc * @CreateTime : 2018-11-23 * @Description : * @Point: Keep a good mood **/public class IpUtil { public static String getIpAddr(HttpServletRequest request) {String ipAddress = null;try { ipAddress = request.getHeader('x-forwarded-for'); if (ipAddress == null || ipAddress.length() == 0 || 'unknown'.equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader('Proxy-Client-IP'); } if (ipAddress == null || ipAddress.length() == 0 || 'unknown'.equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader('WL-Proxy-Client-IP'); } if (ipAddress == null || ipAddress.length() == 0 || 'unknown'.equalsIgnoreCase(ipAddress)) {ipAddress = request.getRemoteAddr();if (ipAddress.equals('127.0.0.1')) { // 根據網卡取本機配置的IP InetAddress inet = null; try {inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) {e.printStackTrace(); } ipAddress = inet.getHostAddress();} } // 對于通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照’,’分割 if (ipAddress != null && ipAddress.length() > 15) { // '***.***.***.***'.length()// = 15if (ipAddress.indexOf(',') > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(','));} }} catch (Exception e) { ipAddress='';}// ipAddress = this.getRequest().getRemoteAddr(); return ipAddress; }}

方法調用:

(當接口 /test 被調用,request就能自動獲取出來,然后調用工具類方法進行解析獲取了。)

@RequestMapping(value = '/test', method = RequestMethod.GET)public String test(HttpServletRequest request){ //獲取IP地址 String ipAddress =IpUtil.getIpAddr(request); return ipAddress;}

到此這篇關于springboot 獲取訪問接口的請求的IP地址的實現的文章就介紹到這了,更多相關springboot 獲取接口IP地址內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 新丰县| 普宁市| 克什克腾旗| 顺昌县| 大同市| 金平| 宝山区| 惠东县| 柘城县| 岗巴县| 乌兰浩特市| 建德市| 汾阳市| 阿克陶县| 灵寿县| 宁明县| 溧水县| 攀枝花市| 凤翔县| 黑河市| 清涧县| 麦盖提县| 东乌珠穆沁旗| 通海县| 綦江县| 延边| 泰兴市| 阿拉善盟| 连城县| 大厂| 祥云县| 漳浦县| 奎屯市| 九寨沟县| 兰西县| 政和县| 万宁市| 南郑县| 芷江| 庄浪县| 泸水县|