Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Official Spring Client Library for IPinfo API (IP geolocation and other types of IP data)

License

NotificationsYou must be signed in to change notification settings

ipinfo/spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IPinfo IPinfo Spring Client Library

License

This is the official Spring client library for the IPinfo.io IP address API,allowing you to look up your own IP address, or get any of the following detailsfor an IP:

  • IP geolocation data (city, region, country, postal code, latitude, and longitude)
  • ASN information (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
  • Company data (the name and domain of the business that uses the IP address)
  • Carrier details (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)

Check all the data we have for your IP addresshere.

Getting Started

You'll need an IPinfo API access token, which you can get by signing up for afree account athttps://ipinfo.io/signup.

The free plan is limited to 50,000 requests per month, and doesn't include someof the data fields such as IP type and company data. To enable all the datafields and additional request volumes seehttps://ipinfo.io/pricing

Click here to view the Java Spring SDK's API documentation.

The library also supports the Lite API, see theLite API section for more info.

Usage

Maven

<dependencies>    <dependency>        <groupId>io.ipinfo</groupId>        <artifactId>ipinfo-spring</artifactId>        <version>0.4.0</version>        <scope>compile</scope>    </dependency></dependencies>

Construction

Using this library is very simple.IPinfoSpring is exposed through a builder:

IPinfoSpringipinfoSpring =newIPinfoSpring.Builder()// Set the IPinfo instance. By default we provide one, however you're// allowed to change this here. Also provide your IPinfo Access Token here.        .setIPinfo(newIPinfo.Builder().setToken("IPINFO ACCESS TOKEN").build())// Set the InterceptorStrategy. By default we use// BotInterceptorStrategy.        .interceptorStrategy(newBotInterceptorStrategy())// Set the IPStrategy. By default we use SimpleIPStrategy.        .ipStrategy(newSimpleIPStrategy())// Set the AttributeStrategy. By default we use SessionAttributeStrategy.        .attributeStrategy(newSessionAttributeStrategy())// Finally build it.        .build();

Adding to Interceptors

To use this as an interceptor in Spring, you simply need to expose yourconfiguration and addIPinfoSpring you obtained from the builder here:

@ConfigurationpublicclassApplicationConfigurationimplementsWebMvcConfigurer {publicvoidaddInterceptors(InterceptorRegistryregistry) {registry.addInterceptor(newIPinfoSpring.Builder().build());    }}

Accessing Value

There are two methods of getting the IPResponse that was injected into theattributes:

  1. Access it directly using the key defined inIPinfoSpring.
  2. Access it using a reference toattributeStrategy.

The code below showcases the two different methods:

importio.ipinfo.api.model.IPResponse;importio.ipinfo.spring.IPinfoSpring;importio.ipinfo.spring.strategies.attribute.AttributeStrategy;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjavax.servlet.http.HttpServletRequest;@RestControllerpublicclassMainController {@AutowiredprivateAttributeStrategyattributeStrategy;@RequestMapping("/foo")publicStringfoo(HttpServletRequestrequest) {IPResponseipResponse =attributeStrategy.getAttribute(request);if (ipResponse ==null) {return"no ipresponse";        }returnipResponse.toString();    }@RequestMapping("/bar")publicStringbar(HttpServletRequestrequest) {IPResponseipResponse = (IPResponse)request                .getSession()                .getAttribute(IPinfoSpring.ATTRIBUTE_KEY);if (ipResponse ==null) {return"no ipresponse";        }returnipResponse.toString();    }}

InterceptorStrategy

TheInterceptorStrategy allows the middleware to know when to actually runthe API calls toipinfo.io.

  • BotInterceptorStrategy (default)This does some very basic checks to see if the request is coming from aspider/crawler, and ignores them.

  • TrueInterceptorStrategyThis runs the API calls all the time.

IPStrategy

TheIPStrategy allows the middleware to know how to extract the IP address ofan incoming request.

Unfortunately, due to the topography of the web today, it's not as easy asgetting the IP address from the request. CDNs, reverse proxies, and countlessother technologies change the origin of a request that a web server can see.

  • SimpleIPStrategy (default)This strategy simply looks at the IP of a request and uses that to extractmore data using IPinfo.

  • XForwardedForIPStrategyThis strategy will extract the IP from theX-Forwarded-For header and if it's nullit'll extract IP usingREMOTE_ADDR of the client.

AttributeStrategy

TheAttributeStrategy allows the middleware to know where to store theIPResponse fromipinfo.io.

  • SessionAttributeStrategy (default)This strategy stores the IPResponse for the entire session. This would bemuch more efficient.

  • RequestAttributeStrategyThis strategy stores the IPResponse per request; this would lead to more APIcalls toipinfo.io

Errors

Any exceptions such asRateLimitedException is passed through Spring's errorhandling system.

Lite API

The library gives the possibility to use theLite API too, authentication with your token is still required.

The returned details are slightly different from the Core API.

To use the Lite API you must use theIPinfoLiteSpring, it works in the same way as theIPinfoSpring class.

IPinfoLiteSpringipinfoSpring =newIPinfoLiteSpring.Builder()// Set the IPinfo instance. By default we provide one, however you're// allowed to change this here. Also provide your IPinfo Access Token here.        .setIPinfo(newIPinfoLite.Builder().setToken("IPINFO ACCESS TOKEN").build())// Set the InterceptorStrategy. By default we use// BotInterceptorStrategy.        .interceptorStrategy(newBotInterceptorStrategy())// Set the IPStrategy. By default we use SimpleIPStrategy.        .ipStrategy(newSimpleIPStrategy())// Set the AttributeStrategy. By default we use SessionAttributeStrategy.        .attributeStrategy(newSessionAttributeStrategy())// Finally build it.        .build();

Other Libraries

There areofficial IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

About IPinfo

Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for businesses and developers.

image

About

Official Spring Client Library for IPinfo API (IP geolocation and other types of IP data)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp