【问题标题】:how to set authentication credentials for JMX in spring boot?如何在 Spring Boot 中为 JMX 设置身份验证凭据?
【发布时间】:2015-10-15 13:28:54
【问题描述】:

我在spring boot 应用程序中启用了JMX。我可以使用Jconsole 设置/获取属性。 我想添加authentication(用户名/密码)以连接到MBeanServer。如果可能的话,我更喜欢基于注释。

这是我的JMXBean

@ManagedResource(objectName = "Examples:type=JMX,name=Resource")
public class Resource {
    List<String> items = new ArrayList<>();

    @ManagedAttribute
    public String getLastItem() {
        return items.get(getSize()-1);
    }

    @ManagedAttribute
    public int getSize() {
        return items.size();
    }

    @ManagedOperation
    public void addItem(String item) {
        items.add(item);
    }

    @ManagedOperation
    public String getItem(int pos) {
        return items.get(pos);
    }

    @ManagedOperation
    public List<String> getItems() {
        return items;
    }


}

目前我没有任何XML 配置。

我在我的配置中初始化了 bean

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @Bean
    public Resource jmxResource() {
        return new Resource();
    }
}

【问题讨论】:

  • 您是否启用了对 JMX 的远程访问?
  • 不,我没有启用远程。我正在寻找有关如何操作的配置。

标签: spring spring-boot spring-jmx


【解决方案1】:

要启用 远程 JMX 访问,您需要使用以下 JVM 参数启动 Spring Boot 应用程序:

-Dcom.sun.management.jmxremote.port=<port>

要配置基于文件的密码验证,请添加以下参数:

-Dcom.sun.management.jmxremote.password.file=<file>

有两个预定义用户:monitorRolecontrolRole。默认情况下,前者只有读权限,后者也可以写(见$JRE_HOME/lib/management/jmxremote.access)。使用$JRE_HOME/lib/management 中的jmxremote.password.template 作为密码文件的模板并坚持使用这些用户名。例如:

monitorRole <password>
controlRole <password>

使用您指定的用户名和密码登录。

请注意,使用此方法时,密码以纯文本形式存储,建议在生产环境中使用。请参阅documentation,了解如何使用 SSL 客户端证书或 LDAP 设置身份验证。

【讨论】:

  • 用户名是什么?我在模板中看到的只是# monitorRole QED # controlRole R&amp;D
  • 不要让自己被“角色”部分所迷惑,这些实际上是用户名。请参阅我的更新答案。
  • 谢谢。我可以用不同的用户名替换“monitorRole”吗
  • 是的,你可以。但是您还必须通过jmxremote.access 文件授予对该用户名的访问权限。不要让事情变得复杂,只需使用预定义的用户名即可。
猜你喜欢
  • 2017-07-06
  • 2011-11-12
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 2023-03-16
  • 2015-08-09
相关资源
最近更新 更多