io.github.nichetoolkit.rest.util.IpAddressUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.util;
import io.github.nichetoolkit.rest.constant.UtilConstants;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* IpAddressUtils
* The ip address utils class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class IpAddressUtils {
/**
* getHttpServletRequest
* The get http servlet request getter method.
* @return {@link javax.servlet.http.HttpServletRequest} The get http servlet request return object is HttpServletRequest
type.
* @see javax.servlet.http.HttpServletRequest
*/
private static HttpServletRequest getHttpServletRequest() {
return BeanUtils.beanOfType(HttpServletRequest.class);
}
/**
* ipAddress
* The ip address method.
* @return {@link java.lang.String} The ip address return object is String
type.
* @see java.lang.String
*/
public static String ipAddress() {
return ipAddress(getHttpServletRequest());
}
/**
* ipAddress
* The ip address method.
* @param httpServletRequest {@link javax.servlet.http.HttpServletRequest} The http servlet request parameter is HttpServletRequest
type.
* @return {@link java.lang.String} The ip address return object is String
type.
* @see javax.servlet.http.HttpServletRequest
* @see java.lang.String
*/
public static String ipAddress(HttpServletRequest httpServletRequest) {
return ipAddress(httpServletRequest,Collections.emptyList());
}
/**
* ipAddress
* The ip address method.
* @param httpServletRequest {@link javax.servlet.http.HttpServletRequest} The http servlet request parameter is HttpServletRequest
type.
* @param ignoredIpAddresses {@link java.util.List} The ignored ip addresses parameter is List
type.
* @return {@link java.lang.String} The ip address return object is String
type.
* @see javax.servlet.http.HttpServletRequest
* @see java.util.List
* @see java.lang.String
*/
public static String ipAddress(HttpServletRequest httpServletRequest, List ignoredIpAddresses) {
if (GeneralUtils.isEmpty(httpServletRequest)) {
return UtilConstants.LOCALHOST_IPV4;
}
String ip = httpServletRequest.getHeader(UtilConstants.X_FORWARDED_FOR_HEADER);
if (GeneralUtils.isEmpty(ip)) {
return httpServletRequest.getRemoteAddr();
}
List ipAddressList = ipAddress(ip, ignoredIpAddresses);
if (ipAddressList.isEmpty()) {
return ip;
}
return ipAddressList.get(ipAddressList.size() - 1);
}
/**
* ipAddress
* The ip address method.
* @param ip {@link java.lang.String} The ip parameter is String
type.
* @return {@link java.util.List} The ip address return object is List
type.
* @see java.lang.String
* @see java.util.List
*/
public static List ipAddress(String ip) {
return ipAddress(ip,Collections.emptyList());
}
/**
* ipAddress
* The ip address method.
* @param ip {@link java.lang.String} The ip parameter is String
type.
* @param ignoredIpAddresses {@link java.util.List} The ignored ip addresses parameter is List
type.
* @return {@link java.util.List} The ip address return object is List
type.
* @see java.lang.String
* @see java.util.List
*/
public static List ipAddress(String ip, List ignoredIpAddresses) {
if (GeneralUtils.isEmpty(ip)) {
return Collections.emptyList();
}
if (!ip.contains(UtilConstants.IP_REGEX)) {
return Collections.singletonList(ip);
}
String[] ipAddressArray = ip.split(UtilConstants.IP_REGEX,-1);
List ipAddressList = new ArrayList<>();
if (GeneralUtils.isNotEmpty(ignoredIpAddresses)) {
for(String ipAddress : ipAddressArray) {
if (!ignoredIpAddresses.contains(ipAddress)) {
ipAddressList.add(ipAddress);
}
}
} else {
ipAddressList.addAll(Arrays.asList(ipAddressArray));
}
return ipAddressList;
}
/**
* remoteIpAddress
* The remote ip address method.
* @return {@link java.lang.String} The remote ip address return object is String
type.
* @see java.lang.String
*/
public static String remoteIpAddress() {
return remoteIpAddress(getHttpServletRequest());
}
/**
* remoteIpAddress
* The remote ip address method.
* @param request {@link javax.servlet.http.HttpServletRequest} The request parameter is HttpServletRequest
type.
* @return {@link java.lang.String} The remote ip address return object is String
type.
* @see javax.servlet.http.HttpServletRequest
* @see java.lang.String
* @see java.lang.SuppressWarnings
*/
@SuppressWarnings({"Duplicates", "unused"})
public static String remoteIpAddress(HttpServletRequest request) {
if (GeneralUtils.isEmpty(request)) {
return UtilConstants.LOCALHOST_IPV4;
}
String ip = request.getHeader(UtilConstants.X_FORWARDED_FOR_HEADER);
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.PROXY_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.WL_PROXY_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.HTTP_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.HTTP_X_FORWARDED_FOR_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* userIpAddress
* The user ip address method.
* @return {@link java.lang.String} The user ip address return object is String
type.
* @see java.lang.String
*/
public static String userIpAddress() {
return userIpAddress(getHttpServletRequest());
}
/**
* userIpAddress
* The user ip address method.
* @param request {@link javax.servlet.http.HttpServletRequest} The request parameter is HttpServletRequest
type.
* @return {@link java.lang.String} The user ip address return object is String
type.
* @see javax.servlet.http.HttpServletRequest
* @see java.lang.String
*/
public static String userIpAddress(HttpServletRequest request) {
if (GeneralUtils.isEmpty(request)) {
return UtilConstants.LOCALHOST_IPV4;
}
String ip = request.getHeader(UtilConstants.X_REAL_IP_HEADER);
if (ip == null || ip.isEmpty() || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.X_FORWARDED_FOR_HEADER);
}
if (ip == null || ip.isEmpty() || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (UtilConstants.LOCALHOST_IPV6.equals(ip)) {
ip = UtilConstants.LOCALHOST_IPV4;
}
}
if (UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = UtilConstants.LOCALHOST_IPV4;
} else {
int index = ip.indexOf(44);
if (index >= 0) {
ip = ip.substring(0, index);
}
}
return ip;
}
/**
* baseIpAddress
* The base ip address method.
* @param request {@link javax.servlet.http.HttpServletRequest} The request parameter is HttpServletRequest
type.
* @return {@link java.lang.String} The base ip address return object is String
type.
* @see javax.servlet.http.HttpServletRequest
* @see java.lang.String
* @see java.lang.SuppressWarnings
*/
@SuppressWarnings("Duplicates")
public static String baseIpAddress(HttpServletRequest request) {
String ip;
String xIp = request.getHeader(UtilConstants.X_REAL_IP_HEADER);
String xFor = request.getHeader(UtilConstants.X_FORWARDED_FOR_HEADER);
if (GeneralUtils.isNotEmpty(xFor) && !UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(xFor)) {
int index = xFor.indexOf(UtilConstants.IP_REGEX);
if (index >= 0) {
return xFor.substring(0, index);
}
return xFor;
}
ip = xIp;
if (GeneralUtils.isEmpty(xIp) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(xIp)) {
ip = request.getHeader(UtilConstants.PROXY_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || ip.isEmpty() || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.WL_PROXY_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.HTTP_CLIENT_IP_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getHeader(UtilConstants.HTTP_X_FORWARDED_FOR_HEADER);
}
if (GeneralUtils.isEmpty(ip) || UtilConstants.UNKNOWN_HEADER.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (UtilConstants.LOCALHOST_IPV6.equals(ip)) {
ip = UtilConstants.LOCALHOST_IPV4;
}
}
return ip;
}
}