springBoot配置全局拦截器打印访问者的IP地址
自定义拦截器继承HandlerInterceptorAdapter 类
@Component
public class ResourceInterceptor extends
HandlerInterceptorAdapter {
private static final String USER_AGENT = "user-agent" ;
private Logger logger = LoggerFactory. getLogger ( this . getClass ( ) ) ;
@Override
public boolean preHandle ( HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if ( logger. isInfoEnabled ( ) ) {
String uri = request. getRequestURI ( ) ;
logger. info ( uri) ;
logger. info ( "UserAgent: {}" , request. getHeader ( USER_AGENT) ) ;
logger. info ( "用户访问地址: {}, 来路地址: {}" , uri, getIpAddr ( request) ) ;
}
return true ;
}
public String getIpAddr ( HttpServletRequest request) {
String ip = request. getHeader ( "x-forwarded-for" ) ;
if ( ip == null || ip. length ( ) == 0 || "unknown" . equalsIgnoreCase ( ip) ) {
ip = request. getHeader ( "Proxy-Client-IP" ) ;
}
if ( ip == null || ip. length ( ) == 0 || "unknown" . equalsIgnoreCase ( ip) ) {
ip = request. getHeader ( "WL-Proxy-Client-IP" ) ;
}
if ( ip == null || ip. length ( ) == 0 || "unknown" . equalsIgnoreCase ( ip) ) {
ip = request. getRemoteAddr ( ) ;
}
if ( "0:0:0:0:0:0:0:1" . equals ( ip) ) {
ip = "127.0.0.1" ;
}
if ( ip. split ( "," ) . length > 1 ) {
ip = ip. split ( "," ) [ 0 ] ;
}
return ip;
} }
@Component
public class WebMvcConfig extends WebMvcConfigurerAdapter {
private Logger logger = LoggerFactory. getLogger ( this . getClass ( ) ) ;
@Autowired
private ResourceInterceptor resourceInterceptor;
public void addInterceptors ( InterceptorRegistry registry) {
logger. info ( "执行InterceptorRegistry" ) ;
registry. addInterceptor ( resourceInterceptor) ;
}
}