@@ -116,7 +116,7 @@ public class JMXServlet extends HttpServlet {
116116 }
117117
118118 private JMXtoXML client ;
119- private final Set <String > localhostAddresses = new HashSet <>();
119+ private final Set <String > serverAddresses = new HashSet <>();
120120
121121 private Path dataDir ;
122122 private Path tokenFile ;
@@ -128,11 +128,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
128128 // Verify if request is from localhost or if user has specific servlet/container managed role.
129129 if (isFromLocalHost (request )) {
130130 // Localhost is always authorized to access
131- LOG .debug ("Local access granted" );
131+ if (LOG .isDebugEnabled ()) {
132+ LOG .debug ("Local access granted" );
133+ }
132134
133135 } else if (hasSecretToken (request , getToken ())) {
134136 // Correct token is provided
135- LOG .debug ("Correct token provided by {}" , request .getRemoteHost ());
137+ if (LOG .isDebugEnabled ()) {
138+ LOG .debug ("Correct token provided by {}" , request .getRemoteHost ());
139+ }
136140
137141 } else {
138142 // Check if user is already authorized, e.g. via MONEX allow user too
@@ -218,7 +222,7 @@ public void init(ServletConfig config) throws ServletException {
218222 client .connect ();
219223
220224 // Register all known localhost addresses
221- registerLocalHostAddresses ();
225+ registerServerAddresses ();
222226
223227 // Get directory for token file
224228 final String jmxDataDir = client .getDataDir ();
@@ -239,27 +243,36 @@ public void init(ServletConfig config) throws ServletException {
239243 }
240244
241245 /**
242- * Register all known IP-addresses for localhost .
246+ * Register all known IP-addresses for server .
243247 */
244- void registerLocalHostAddresses () {
245- // The external IP address of the server
248+ void registerServerAddresses () {
249+ // The IPv4 address of the loopback interface of the server - 127.0.0.1 on Windows/Linux/macOS, or 127.0.1.1 on Debian/Ubuntu
246250 try {
247- localhostAddresses .add (InetAddress .getLocalHost ().getHostAddress ());
248- } catch (UnknownHostException ex ) {
249- LOG .warn ("Unable to get HostAddress for localhost: {}" , ex .getMessage ());
251+ serverAddresses .add (InetAddress .getLocalHost ().getHostAddress ());
252+ } catch (final UnknownHostException ex ) {
253+ LOG .warn ("Unable to get loopback IP address for localhost: {}" , ex .getMessage ());
250254 }
251255
252- // The configured Localhost addresses
256+ // Any additional IPv4 and IPv6 addresses associated with the loopback interface of the server
253257 try {
254- for (InetAddress address : InetAddress .getAllByName ("localhost" )) {
255- localhostAddresses .add (address .getHostAddress ());
258+ for (final InetAddress loopBackAddress : InetAddress .getAllByName ("localhost" )) {
259+ serverAddresses .add (loopBackAddress .getHostAddress ());
256260 }
257- } catch (UnknownHostException ex ) {
258- LOG .warn ("Unable to retrieve ipaddresses for localhost: {}" , ex .getMessage ());
261+ } catch (final UnknownHostException ex ) {
262+ LOG .warn ("Unable to retrieve additional loopback IP addresses for localhost: {}" , ex .getMessage ());
259263 }
260264
261- if (localhostAddresses .isEmpty ()) {
262- LOG .error ("Unable to determine addresses for localhost, jmx servlet might be disfunctional." );
265+ // Any IPv4 and IPv6 addresses associated with other interfaces in the server
266+ try {
267+ for (final InetAddress hostAddress : InetAddress .getAllByName (InetAddress .getLocalHost ().getHostName ())) {
268+ serverAddresses .add (hostAddress .getHostAddress ());
269+ }
270+ } catch (final UnknownHostException ex ) {
271+ LOG .warn ("Unable to retrieve additional interface IP addresses for localhost: {}" , ex .getMessage ());
272+ }
273+
274+ if (serverAddresses .isEmpty ()) {
275+ LOG .error ("Unable to determine IP addresses for localhost, JMXServlet might be dysfunctional." );
263276 }
264277 }
265278
@@ -269,8 +282,13 @@ void registerLocalHostAddresses() {
269282 * @param request The HTTP request
270283 * @return TRUE if request is from LOCALHOST otherwise FALSE
271284 */
272- boolean isFromLocalHost (HttpServletRequest request ) {
273- return localhostAddresses .contains (request .getRemoteAddr ());
285+ boolean isFromLocalHost (final HttpServletRequest request ) {
286+ String remoteAddr = request .getRemoteAddr ();
287+ if (remoteAddr .charAt (0 ) == '[' ) {
288+ // Handle IPv6 addresses that are wrapped in []
289+ remoteAddr = remoteAddr .substring (1 , remoteAddr .length () - 1 );
290+ }
291+ return serverAddresses .contains (remoteAddr );
274292 }
275293
276294 /**
0 commit comments