Obtenga la dirección IP del usuario, primero obtenga la IP solicitada y luego obtenga la ubicación geográfica solicitada a través de la IP

Prefacio:

¿Cómo obtiene el software de videos cortos nuestra ubicación geográfica? Puede saber por qué a través de este artículo y puede probarlo usted mismo.

De todos modos, para llamar a la API, simplemente cargue el código directamente

1. Cree un proyecto SpringBoot

1.1 El archivo POM es el siguiente:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lms.other</groupId>
    <artifactId>ip-address</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ip-address</name>
    <description>获取请求的ip,地址</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>

        <!--httpClint-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2 Cree la siguiente estructura:

 1.2.1 Tres clases del paquete utils: AddressUtils, HttpClientUtilUse, IpUtils

DirecciónUtils:

package com.lms.other.ipaddress.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class AddressUtils {
    public static String getAddress(String ip){
        if(ip.contains("127.0.0.1") || ip.contains("0:0:0:0:0:0:0:1")){
            return "本地";
        }
        //更精确的地图API
        String url = "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+ ip +"&resource_id=5809";
        String body = HttpClientUtilUse.getInstance().sendHttpGet(url);
        JSONObject jsonObject = JSON.parseObject(body);
        JSONArray data1 = jsonObject.getJSONArray("data");
        JSONObject o = (JSONObject)data1.get(0);
        return o.getString("location");
    }
}

 HttpClientUtilUse:

package com.lms.other.ipaddress.utils;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class HttpClientUtilUse {
    //初始配置
    private RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(15000)            //读取数据超时:SocketTimeout-->指连接上一个url,获取response的返回等待时间
            .setConnectTimeout(15000)           //连接超时:connectionTimeout-->指连接一个url等待时间
            .setConnectionRequestTimeout(15000)
            .build();

    //构造自身对象
    private static HttpClientUtilUse instance = null;

    private HttpClientUtilUse() {
    }

    public static HttpClientUtilUse getInstance() {
        if (instance == null) {
            instance = new HttpClientUtilUse();
        }
        return instance;
    }

    /**
     * ****一共4种请求方式
     * 发送 post请求
     *
     * @param httpUrl 地址
     *                无参
     */
    public String sendHttpPost(String httpUrl) {
        // 创建httpPost
        HttpPost httpPost = new HttpPost(httpUrl);
        return sendHttpPost(httpPost);
    }

    /**
     * 发送 post请求
     *
     * @param httpUrl 地址
     * @param params  参数(格式: key1=value1&key2=value2 或  Sting类型xml数据★)
     */
    public String sendHttpPost(String httpUrl, String params) {
        // 创建httpPost
        HttpPost httpPost = new HttpPost(httpUrl);
        try {// 设置参数
            StringEntity stringEntity = new StringEntity(params, "UTF-8");
            stringEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(stringEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendHttpPost(httpPost);
    }

    /**
     * 发送 post请求(带参数)
     *
     * @param httpUrl 地址
     * @param maps    参数(格式:Map<String, String> maps)
     */
    public String sendHttpPost(String httpUrl, Map<String, String> maps) {
        // 创建httpPost
        HttpPost httpPost = new HttpPost(httpUrl);
        System.out.println("请求url方式信息 " + httpPost.getRequestLine());
        // 创建参数队列
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        for (String key : maps.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendHttpPost(httpPost);
    }

    /**
     * 发送 post请求(带文件)  *****
     *
     * @param httpUrl   地址
     * @param maps      参数(格式:Map<String, String> maps)
     * @param fileLists 附件(格式:List<File> fileLists)
     */
    public String sendHttpPost(String httpUrl, Map<String, String> maps, List<File> fileLists) {
        // 创建httpPost
        HttpPost httpPost = new HttpPost(httpUrl);
        // 设置参数 编码
        MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
        for (String key : maps.keySet()) {
            meBuilder.addPart(key, new StringBody(maps.get(key), ContentType.TEXT_PLAIN));
        }
        // 附件
        for (File file : fileLists) {
            FileBody fileBody = new FileBody(file);
            meBuilder.addPart("files", fileBody);
        }
        // 创建HttpEntity
        HttpEntity reqEntity = meBuilder.build();
        httpPost.setEntity(reqEntity);
        return sendHttpPost(httpPost);
    }

    /**
     * 发送Post请求   (数据处理...)  ***********
     *
     * @param httpPost
     * @return
     */
    private String sendHttpPost(HttpPost httpPost) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            // 创建httpClient实例
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);
            // 执行请求
            response = httpClient.execute(httpPost);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseContent;
    }

    /**
     * 如何在java中发送https请求?  让java信任请求服务端是安全的呢?  Https  ★★★
     * 1.导入服务器安全证书
     * 2.自定义信任管理器       使用
     */
    public String sendHttpPost(String url, Map<String, String> maps, String xmlParam, boolean isHttps) {
        //封装HttpPost
        HttpPost httpPost = new HttpPost(url);
        //1.map格式 如:alipay
        if (maps != null) {
            List<NameValuePair> nvps = new LinkedList<NameValuePair>();
            for (String key : maps.keySet())
                nvps.add(new BasicNameValuePair(key, maps.get(key)));         // 参数
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
        }
        //2.Sting类型xml数据 如:wxpay
        if (xmlParam != null) {
            httpPost.setEntity(new StringEntity(xmlParam, Consts.UTF_8));     // String的xml直接当尾参数传递进来 ★★
        }

        //执行httpClient,获取数据
        CloseableHttpClient httpClient = null;
        Integer statusCode = 0;
        String content = null;
        try {
            if (isHttps) {
                SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                    // 信任所有 SSL X509Certificate
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                }).build();
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
                httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            } else {
                httpClient = HttpClients.createDefault();
            }
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                if (response != null) {
                    if (response.getStatusLine() != null)
                        statusCode = response.getStatusLine().getStatusCode();
                    HttpEntity entity = response.getEntity();
                    // 响应内容
                    content = EntityUtils.toString(entity, Consts.UTF_8);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    /********************************************************************/

    /**
     * 发送 get请求
     *
     * @param httpUrl
     */
    public String sendHttpGet(String httpUrl) {
        // 创建get请求
        HttpGet httpGet = new HttpGet(httpUrl);
        return sendHttpGet(httpGet);
    }

    /**
     * 发送 get请求 Https★★
     *
     * @param httpUrl
     */
    public String sendHttpsGet(String httpUrl) {
        // 创建get请求
        HttpGet httpGet = new HttpGet(httpUrl);
        return sendHttpsGet(httpGet);
    }

    /**
     * 发送 get请求
     *
     * @param
     * @return
     */
    private String sendHttpGet(HttpGet httpGet) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            // 创建httpClient实例
            httpClient = HttpClients.createDefault();
            httpGet.setConfig(requestConfig);
            // 执行请求
            response = httpClient.execute(httpGet);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseContent;
    }

    /**
     * 发送 get请求 Https★★
     *
     * @param httpPost
     * @return
     */
    private String sendHttpsGet(HttpGet httpGet) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            // 创建httpClient实例 SSL
            PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
            DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
            httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();

            httpGet.setConfig(requestConfig);
            // 执行请求
            response = httpClient.execute(httpGet);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseContent;
    }

}




IpUtils:

package com.lms.other.ipaddress.utils;

import javax.servlet.http.HttpServletRequest;


public class IpUtils {

    public static String 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.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

1.2.2 AddressController en el paquete del controlador

Controlador de dirección:

package com.lms.other.ipaddress.controller;

import com.lms.other.ipaddress.utils.AddressUtils;
import com.lms.other.ipaddress.utils.IpUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class AddressController {

    //获取请求的地理位置
    @GetMapping("/address")
    public String getAddress(HttpServletRequest request){
        //通过请求获取IP
        String ip = IpUtils.getIp(request);
        //通过IP获取地理位置,并返回
        return AddressUtils.getAddress(ip);
    }
}

2. Prueba

2.1 Escriba una prueba de clase de prueba, el método de prueba es el siguiente (le he proporcionado varias direcciones IP para que las pruebe, por supuesto, también puede probar su propia IP)


    void contextLoads() {

//        String ip = "123.135.143.226";//山东省泰安市岱岳 联通
        String ip = "114.114.114.114";//江苏省南京市 电信
        String address = AddressUtils.getAddress(ip);//注意导入AddressUtils
        System.out.println(address);
    }

2.2 Si tiene su propia computadora, visite en el navegador: http://localhost:8080/address ( tenga en cuenta su propio número de puerto ), el navegador muestra los siguientes resultados:

Nota : Debido a que la IP de su propia computadora es: 127.0.0.1, que es local, es correcta

2.3 Si ingresa a través de la intranet, el navegador mostrará los resultados de la siguiente manera:

 Si desea obtener información sobre la penetración de la intranet, lea este artículo: http://t.csdn.cn/I8Qhs

Supongo que te gusta

Origin blog.csdn.net/m0_60817176/article/details/131865545
Recomendado
Clasificación