简体   繁体   English

在 Spring 引导中获取传入请求的地理位置

[英]Get geoLocation of incoming request in Spring boot

I want to know the location of the user that is on our web application.我想知道我们的 web 应用程序上的用户的位置。 The information should contain user's Country and City .该信息应包含用户的CountryCity I am unable to find a good way to do so using Java or Spring Boot .我找不到使用JavaSpring Boot的好方法。 I have tried using GeoLite2-City but it gives me exception saying that The address 10.212.134.200 is not in the database.我曾尝试使用GeoLite2-City ,但它给了我一个例外,说The address 10.212.134.200 is not in the database. I was also getting this exception on my localhost with ip 127.0.0.1 .我在我的localhost上也遇到了这个异常 ip 127.0.0.1 Following is the code I have used:以下是我使用的代码:

// String ipAddress = httpServletRequest.getRemoteAddr();
public String getGeoLocation(String ipAddress) {
        String geoLocation = "";
        try {
            File database = ResourceUtils.getFile("classpath:GeoLite2-City/GeoLite2-City.mmdb");;
            DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
            CityResponse response = dbReader.city(InetAddress.getByName(ipAddress));
            
            String countryName = response.getCountry().getName();
            String cityName = response.getCity().getName();
            String postal = response.getPostal().getCode();
            String state = response.getLeastSpecificSubdivision().getName();
            
            geoLocation = "Country: " + countryName + "cityName: " + cityName + "postal: " + postal + "state: " + state;
            
            System.out.println(geoLocation);
        } catch(Exception ex) {
            logger.error("Exception occured while trying to get GeoLocation of user: " + ex);
        }
        return geoLocation;
    }

But I am always getting exception.但我总是例外。

I only have downloaded and added Geolite2-city file.我只下载并添加了Geolite2-city文件。 I am also not confirmed if I need to add any other file.我也没有确认是否需要添加任何其他文件。 The dependency I have added is:我添加的依赖项是:

<dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>2.8.0</version>
</dependency>

Note:笔记:

GeoLite2 was one of the way I adopted to get the GeoLocation of the user. GeoLite2 是我用来获取用户 GeoLocation 的一种方式。 If you have a better API or suggestion, please suggest me a better way to get user current GeoLocation. 如果您有更好的 API 或建议,请向我建议一种更好的方法来获取用户当前的 GeoLocation。

10.212.134.200 and 127.0.0.1 are addresses that have a meaning only inside your network, or inside your machine, not on the internet. 10.212.134.200 和 127.0.0.1 是仅在您的网络内部或您的机器内部有意义的地址,而不是在互联网上。 There is no way for MaxMind to know where these are located. MaxMind 无法知道它们的位置。 (You may be able to tell which city you are in by looking out of the window.) (您可以通过向外看 window 来判断您所在的城市。)

If the request actually comes from the internet, so from outside your network, it is perhaps proxied.如果请求实际上来自互联网,那么来自您的网络外部,它可能是代理的。 If that is the case, it is possible that the proxy has added a X-Forwarded-For or X-Real-IP request header or similar containing the external ("real") IP address of the original requester.如果是这种情况,代理可能添加了X-Forwarded-ForX-Real-IP请求 header 或包含原始请求者的外部(“真实”)IP 地址的类似请求。 You would feed that into getGeoLocation .您会将其输入getGeoLocation

Geo location based on IP may not give you original result.基于 IP 的地理位置可能无法为您提供原始结果。 Say a user is on a VPN then you would not get user's original location.假设用户在 VPN 上,那么您将无法获得用户的原始位置。 Based on the limited context I could grab, a better solution would be to ask for user's location permission on the client side which you could then send it over to server.基于我可以获取的有限上下文,更好的解决方案是在客户端请求用户的位置许可,然后您可以将其发送到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM