其他分享
首页 > 其他分享> > 如何在春季启动时为JMX设置身份验证凭据?

如何在春季启动时为JMX设置身份验证凭据?

作者:互联网

我在春季启动应用程序中启用了JMX.我能够设置/获取使用Jconsole的属性.
我想添加身份验证(用户名/密码)以连接到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访问,您需要使用以下JVM参数启动Spring Boot应用程序:

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

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

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

有两个预定义的用户:monitorRole和controlRole.默认情况下,前者仅具有读访问权限,后者也可能具有写访问权限(请参见$JRE_HOME / lib / management / jmxremote.access).将$JRE_HOME / lib / management中的jmxremote.password.template用作密码文件的模板,并坚持使用这些用户名.例如:

monitorRole <password>
controlRole <password>

使用这些用户名和您指定的密码登录.

建议使用此方法时,密码以纯文本形式存储,不建议在生产中使用.有关如何使用SSL客户端证书或LDAP设置身份验证的信息,请参见documentation.

标签:spring-boot,spring,spring-jmx
来源: https://codeday.me/bug/20191120/2040260.html