Starrier/commons

View on GitHub
src/main/java/org/starrier/common/utils/IpUtil.java

Summary

Maintainability
A
3 hrs
Test Coverage
package org.starrier.common.utils;
 
import javax.servlet.http.HttpServletRequest;
 
/**
* @author imperator
* @date 2019-09-10
*/
public class IpUtil {
 
/**
* 获取客户端真实ip地址
*
* @param request
* @return
*/
Method `getIpAddress` has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
 
Similar blocks of code found in 4 locations. Consider refactoring.
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
 
Similar blocks of code found in 4 locations. Consider refactoring.
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
 
Similar blocks of code found in 4 locations. Consider refactoring.
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
 
Similar blocks of code found in 4 locations. Consider refactoring.
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;
}
 
}