@@ -116,7 +116,7 @@ public class JMXServlet extends HttpServlet {
116
116
}
117
117
118
118
private JMXtoXML client ;
119
- private final Set <String > localhostAddresses = new HashSet <>();
119
+ private final Set <String > serverAddresses = new HashSet <>();
120
120
121
121
private Path dataDir ;
122
122
private Path tokenFile ;
@@ -128,11 +128,15 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
128
128
// Verify if request is from localhost or if user has specific servlet/container managed role.
129
129
if (isFromLocalHost (request )) {
130
130
// Localhost is always authorized to access
131
- LOG .debug ("Local access granted" );
131
+ if (LOG .isDebugEnabled ()) {
132
+ LOG .debug ("Local access granted" );
133
+ }
132
134
133
135
} else if (hasSecretToken (request , getToken ())) {
134
136
// 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
+ }
136
140
137
141
} else {
138
142
// Check if user is already authorized, e.g. via MONEX allow user too
@@ -218,7 +222,7 @@ public void init(ServletConfig config) throws ServletException {
218
222
client .connect ();
219
223
220
224
// Register all known localhost addresses
221
- registerLocalHostAddresses ();
225
+ registerServerAddresses ();
222
226
223
227
// Get directory for token file
224
228
final String jmxDataDir = client .getDataDir ();
@@ -239,27 +243,36 @@ public void init(ServletConfig config) throws ServletException {
239
243
}
240
244
241
245
/**
242
- * Register all known IP-addresses for localhost .
246
+ * Register all known IP-addresses for server .
243
247
*/
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
246
250
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 ());
250
254
}
251
255
252
- // The configured Localhost addresses
256
+ // Any additional IPv4 and IPv6 addresses associated with the loopback interface of the server
253
257
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 ());
256
260
}
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 ());
259
263
}
260
264
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." );
263
276
}
264
277
}
265
278
@@ -269,8 +282,13 @@ void registerLocalHostAddresses() {
269
282
* @param request The HTTP request
270
283
* @return TRUE if request is from LOCALHOST otherwise FALSE
271
284
*/
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 );
274
292
}
275
293
276
294
/**
0 commit comments