@@ -679,38 +679,34 @@ private boolean isSpecificAPI(String commandName) {
679679 }
680680 return false ;
681681 }
682- boolean doUseForwardHeaders () {
682+ static boolean doUseForwardHeaders () {
683683 return Boolean .TRUE .equals (ApiServer .useForwardHeader .value ());
684684 }
685685
686- String [] proxyNets () {
686+ static String [] proxyNets () {
687687 return ApiServer .proxyForwardList .value ().split ("," );
688688 }
689689 //This method will try to get login IP of user even if servlet is behind reverseProxy or loadBalancer
690- public InetAddress getClientAddress (final HttpServletRequest request ) throws UnknownHostException {
691- String ip = null ;
692- InetAddress pretender = InetAddress . getByName ( request . getRemoteAddr ());
693- if ( doUseForwardHeaders ()) {
694- if (NetUtils .isIpInCidrList (pretender , proxyNets ())) {
690+ public static InetAddress getClientAddress (final HttpServletRequest request ) throws UnknownHostException {
691+ final String remote = request . getRemoteAddr () ;
692+ if ( doUseForwardHeaders ()) {
693+ final InetAddress remoteAddr = InetAddress . getByName ( remote );
694+ if (NetUtils .isIpInCidrList (remoteAddr , proxyNets ())) {
695695 for (String header : getClientAddressHeaders ()) {
696696 header = header .trim ();
697- ip = getCorrectIPAddress (request .getHeader (header ));
697+ final String ip = getCorrectIPAddress (request .getHeader (header ));
698698 if (StringUtils .isNotBlank (ip )) {
699- LOGGER .debug (String . format ( "found ip %s in header %s " , ip , header ) );
700- break ;
699+ LOGGER .debug ("found ip {} in header {} " , ip , header );
700+ return InetAddress . getByName ( ip ) ;
701701 }
702- } // no address found in header so ip is blank and use remote addr
703- } // else not an allowed proxy address, ip is blank and use remote addr
704- }
705- if (StringUtils .isBlank (ip )) {
706- LOGGER .trace (String .format ("no ip found in headers, returning remote address %s." , pretender .getHostAddress ()));
707- return pretender ;
702+ }
703+ }
708704 }
709-
710- return InetAddress .getByName (ip );
705+ LOGGER . trace ( "no ip found in headers, returning remote address {}." , remote );
706+ return InetAddress .getByName (remote );
711707 }
712708
713- private String [] getClientAddressHeaders () {
709+ private static String [] getClientAddressHeaders () {
714710 return ApiServer .listOfForwardHeaders .value ().split ("," );
715711 }
716712
0 commit comments