Integrating IP2Location.io with Spring

The Java Spring IP2Location.io middleware enables users to read their IP geolocation data.

Install using Maven dependency below:

<dependency>
	<groupId>com.ip2location</groupId>
	<artifactId>ip2location-io-spring</artifactId>
	<version>1.0.0</version>
</dependency>

NOTE: You will need an API key so sign up for one at the pricing page if you don't have one.

Adding to the interceptors:

Adding to the interceptors:

package com.example.demo;

import com.ip2location.IPGeolocation;
import com.ip2location.spring.IPGeolocationSpring;
import com.ip2location.spring.strategies.attribute.*;
import com.ip2location.spring.strategies.interceptor.BotInterceptorStrategy;
import com.ip2location.spring.strategies.ip.SimpleIPStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class DemoApplicationConfiguration implements WebMvcConfigurer {
    @Bean(name = "attributeStrategy")
    public AttributeStrategy createBean() {
        AttributeStrategy bean = new RequestAttributeStrategy();
        return bean;
    }

    public void addInterceptors(InterceptorRegistry registry) {
        com.ip2location.Configuration config = new com.ip2location.Configuration();
        String apiKey = "YOUR_API_KEY";  // Provide your IP2Location.io API key here.
        config.setApiKey(apiKey);
        IPGeolocation ipl = new IPGeolocation(config);
        IPGeolocationSpring ipGeolocationSpring = new IPGeolocationSpring.Builder()
                // Set the IPGeolocation instance.
                .setIPGeolocation(ipl)
                // Set the InterceptorStrategy. By default we use
                // BotInterceptorStrategy.
                .interceptorStrategy(new BotInterceptorStrategy())
                // Set the IPStrategy. By default we use SimpleIPStrategy.
                .ipStrategy(new SimpleIPStrategy())
                // Set the AttributeStrategy. By default we use SessionAttributeStrategy.
                .attributeStrategy(new SessionAttributeStrategy())
                // Finally build it.
                .build();
        registry.addInterceptor(ipGeolocationSpring);
    }
}

Accessing the IP geolocation data

Accessing the IP geolocation data

There are two methods of getting the JsonObject that was injected into the attributes:

  1. Access it directly using the key defined in IPGeolocationSpring.
  2. Access it using a reference to attributeStrategy.

The code below showcases the two different methods:

package com.example.demo;

import com.google.gson.JsonObject;
import com.ip2location.IPGeolocation;
import com.ip2location.spring.IPGeolocationSpring;
import com.ip2location.spring.strategies.attribute.AttributeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;

@RestController
public class DemoApplicationController {
    @Autowired
    private AttributeStrategy attributeStrategy;

    @RequestMapping("/foo")
    public String foo(HttpServletRequest request) {
        JsonObject ipResponse = (JsonObject) attributeStrategy.getAttribute(request);

        if (ipResponse == null) {
            return "no response";
        }

        return ipResponse.toString();
    }

    @RequestMapping("/bar")
    public String bar(HttpServletRequest request) {
        JsonObject ipResponse = (JsonObject) request
                .getSession()
                .getAttribute(IPGeolocationSpring.ATTRIBUTE_KEY);

        if (ipResponse == null) {
            return "no response";
        }

        return ipResponse.toString();
    }

}

For more information, please refer the https://github.com/ip2location/ip2location-io-spring.