springboot 獲取訪問接口的請求的IP地址的實現(xiàn)
工具類:
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')) { // 根據(jù)網(wǎng)卡取本機配置的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; }}
方法調(diào)用:
(當(dāng)接口 /test 被調(diào)用,request就能自動獲取出來,然后調(diào)用工具類方法進行解析獲取了。)
@RequestMapping(value = '/test', method = RequestMethod.GET)public String test(HttpServletRequest request){ //獲取IP地址 String ipAddress =IpUtil.getIpAddr(request); return ipAddress;}
到此這篇關(guān)于springboot 獲取訪問接口的請求的IP地址的實現(xiàn)的文章就介紹到這了,更多相關(guān)springboot 獲取接口IP地址內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )2. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對象3. ASP新手必備的基礎(chǔ)知識4. Docker 啟動Redis 并設(shè)置密碼的操作5. asp文件用什么軟件編輯6. 通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis)7. JS中6個對象數(shù)組去重的方法8. vue+element開發(fā)一個谷歌插件的全過程9. 利用CSS制作3D動畫10. Vue axios獲取token臨時令牌封裝案例

網(wǎng)公網(wǎng)安備