
3 changed files with 91 additions and 0 deletions
@ -0,0 +1,40 @@ |
|||||
|
package com.visual.open.anpr.server.bootstrap.conf; |
||||
|
|
||||
|
import com.visual.open.anpr.server.utils.RequestUtil; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.aspectj.lang.annotation.Around; |
||||
|
import org.aspectj.lang.annotation.Aspect; |
||||
|
import org.aspectj.lang.annotation.Pointcut; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
@Aspect |
||||
|
@Component |
||||
|
public class AspectConfig { |
||||
|
|
||||
|
public Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
|
||||
|
@Pointcut(" execution(* com..PlateController.recognition(..))" ) |
||||
|
public void recognitionPointCut() { |
||||
|
} |
||||
|
|
||||
|
@Around("recognitionPointCut()") |
||||
|
public Object runTimeStatistics(ProceedingJoinPoint pjp) throws Throwable { |
||||
|
long startTime = System.currentTimeMillis(); |
||||
|
String ip = "0.0.0.0"; |
||||
|
try { |
||||
|
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
||||
|
ip = RequestUtil.getRemoteHost(sra.getRequest()); |
||||
|
}catch (Throwable t){} |
||||
|
|
||||
|
try { |
||||
|
return pjp.proceed(); |
||||
|
}finally { |
||||
|
long costTime = System.currentTimeMillis() - startTime; |
||||
|
logger.info("plate recognition cost time : {} ms, from ip: {}", costTime, ip); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,45 @@ |
|||||
|
package com.visual.open.anpr.server.utils; |
||||
|
|
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
|
||||
|
public class RequestUtil { |
||||
|
|
||||
|
/** |
||||
|
* 获取目标主机的ip |
||||
|
*/ |
||||
|
public static String getRemoteHost(HttpServletRequest request) { |
||||
|
String Xip = request.getHeader("X-Real-IP"); |
||||
|
String XFor = request.getHeader("X-Forwarded-For"); |
||||
|
if(StringUtils.isNotBlank(XFor) && !"unKnown".equalsIgnoreCase(XFor)) { |
||||
|
int index = XFor.indexOf(","); |
||||
|
if(index != -1) { |
||||
|
return XFor.substring(0, index); |
||||
|
} else { |
||||
|
return XFor; |
||||
|
} |
||||
|
} |
||||
|
XFor = Xip; |
||||
|
if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)) { |
||||
|
return XFor; |
||||
|
} |
||||
|
if(StringUtils.isBlank(XFor) || "unKnown".equalsIgnoreCase(XFor)) { |
||||
|
XFor = request.getHeader("Proxy-Client-IP"); |
||||
|
} |
||||
|
if(StringUtils.isBlank(XFor) || "unKnown".equalsIgnoreCase(XFor)) { |
||||
|
XFor = request.getHeader("WL-Proxy-Client-IP"); |
||||
|
} |
||||
|
if(StringUtils.isBlank(XFor) || "unKnown".equalsIgnoreCase(XFor)) { |
||||
|
XFor = request.getHeader("HTTP_CLIENT_IP"); |
||||
|
} |
||||
|
if(StringUtils.isBlank(XFor) || "unKnown".equalsIgnoreCase(XFor)) { |
||||
|
XFor = request.getHeader("HTTP_X_FORWARDED_FOR"); |
||||
|
} |
||||
|
if(StringUtils.isBlank(XFor) || "unKnown".equalsIgnoreCase(XFor)) { |
||||
|
XFor = request.getRemoteAddr(); |
||||
|
} |
||||
|
return XFor; |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue