关于Java:无法禁用远程Spring Boot JMX访问

Cannot disable remote Spring Boot JMX access

我们有一个带有执行器的Spring Boot应用程序。 我们正在尝试禁用远程JMX访问,但是以某种方式无法正常工作。 我们尝试了以下设置:

在Tomcat启动选项中:

1
2
3
4
5
6
7
8
9
-Dcom.sun.management.jmxremote=false
-Dcom.sun.management.jmxremote.password.file=....../jmxremote.password
-Dcom.sun.management.jmxremote.registry.ssl=true
-Djava.security.manager
-Djava.security.policy=jmx.policy
-Djavax.net.ssl.keyStore=....jks
-Djavax.net.ssl.keyStorePassword=****
-Djavax.net.ssl.trustStore=.....jks
-Djavax.net.ssl.trustStorePassword=****

在application.properties中:

1
2
3
4
spring.jmx.enabled=false
spring.datasource.jmx-enabled=false
endpoints.jmx.enabled=false
spring.jmx.server=localhost

但是,我们仍然能够从远程系统访问JMX。 选项spring.jmx.enabled的唯一区别是特定于Spring的MBean不可用,但其他MBean仍然可用。

我们如何禁用对JMX的远程访问? 理想情况下,从本地计算机连接时,我们仍然希望访问,但是如果需要,也可以将其禁用。

添加
build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
buildscript {
    ext {
        springBootVersion = '1.5.16.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply from:"../dependencies.gradle"

repositories {
    mavenCentral()
}

bootRepackage {
    enabled = false
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    providedRuntime project(':....')
    compile project(':...')
    compile project(':...')
    compile project(':...')
    compile project(':...')

    compile group: 'com.hazelcast', name: 'hazelcast', version: '3.12'
    compile group: 'com.hazelcast', name: 'hazelcast-client', version: '3.12'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.11.Final'
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.2'

    compile group: 'org.apache.poi', name: 'poi', version: '4.0.1'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'

    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.2'

    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-actuator")

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
}


基于Diederik的解决方案,需要更改两个属性

  • 将身份验证设置为true。 并指定password.file和access.file。

  • 将主机设置为localhost或127.0.0.1。

  • 如果authenticate为false或host不是localhost,则将启用远程访问。

    这是一个样本

    1
    2
    3
    4
    5
    6
    7
    -Dcom.sun.management.jmxremote \\
    -Dcom.sun.management.jmxremote.port=1099 \\
    -Dcom.sun.management.jmxremote.host=localhost \\
    -Dcom.sun.management.jmxremote.authenticate=true \\
    -Dcom.sun.management.jmxremote.password.file=<path to password file> \\
    -Dcom.sun.management.jmxremote.access.file=<path to access file> \\
    -Dcom.sun.management.jmxremote.ssl=false \\

    出现了完全相同的问题,并通过使用以下设置解决了它:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    -Dcom.sun.management.jmxremote
    -Dcom.sun.management.jmxremote.local.only=true
    -Dcom.sun.management.jmxremote.authenticate=true
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.port=1099
    -Dcom.sun.management.jmxremote.host=localhost
    -Djava.rmi.server.hostname=localhost
    -Dcom.sun.management.jmxremote.password.file=<path to jmxremote.password>
    -Dcom.sun.management.jmxremote.access.file=<path to jmxremote.access>

    请注意,可能有必要将命令和将属性显式设置为其默认值,即使显然没有必要。