gpt4 book ai didi

java - MBeanOperationInfo 和 MBeanAttributeInfo 元数据?

转载 作者:行者123 更新时间:2023-11-30 03:45:27 25 4
gpt4 key购买 nike

我为我们的一个应用程序编写了一个 JMX 接口(interface)。然后另一个应用程序连接并允许用户通过此管理工具远程查看各种状态相关属性/调用操作。我偶然发现了一个小错误,我们的数据库连接设置通过 JMX 公开,且密码未加密。我想标记应使用某些标志进行混淆的属性/操作,但它似乎不支持为名称和描述添加任何用户定义的值异常(exception)。我想我可以像

一样分隔描述字段
String desc = getAttrDesc() + ";" + getIsObfuscated();

但我不太喜欢这种做法。那么问题是,是否有更好的方法来向属性/操作信息对象或 Dynamic MBean 本身提供任意键值对?它不必在信息对象本身上,只要我可以在管理工具方面将它们匹配即可。任何见解将不胜感激。

为了澄清起见,当我构造 MBeanOperationInfo(为了示例而省略属性)时,我这样做:

        LinkedList<MBeanOperationInfo> opperInfos = new LinkedList<MBeanOperationInfo>();
for (Method m : m_InstObj.getMethods()) {
InstrumentedOperation anno = m.getAnnotation(InstrumentedOperation.class);
String desc = anno.description();
opperInfos.add(new MBeanOperationInfo(desc, m));
}

m_Operations = new MBeanOperationInfo[opperInfos.size()];
int I = 0;
for (MBeanOperationInfo info : opperInfos) {
m_Operations[I] = info;
I++;
}

我希望 InstrumentedOperation 注释有一个用于混淆的字段,我可以这样使用:

anno.obfuscated(); // <- retreives a boolean set as a compile time constant on the annotation

并能够将此值包含在 Info 对象中。

然后在接收端我这样做:

MBeanOperationInfo[] operInfos = conn.getMBeanInfo(name).getOperations();

for (MBeanOperationInfo info : operInfos) {
String propName = getPropNameFromInfo(info.getName());
if (!uniqueSettings.contains(propName)) {
// this setting hasn't been handled, get the getters and setters and make the method map
String getter = getGetterForSetting(operInfos, info.getName());
String setter = getSetterForSetting(operInfos, info.getName());
Object value = conn.invoke(name, getter, new Object[] {}, new String[] {});
if (getter != null && setter != null) {
SettingMethodMap map = new SettingMethodMap(name.getKeyProperty("type"), propName, info.getName(), setter, getter, value);
uniqueSettings.add(propName);
m_Settings.add(map);
}
}
}

在这里,我希望能够通过某种机制检索键值对,所以我知道我需要以不同的方式处理这个字段并在编辑器中对其进行混淆。

最佳答案

这可以使用 javax.management.DescriptorKey 来实现.

例如,使用我为此改编的代码示例,使用 standard mbean :

“混淆”注释:

import java.lang.annotation.*;
import javax.management.DescriptorKey;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Obfuscated {
@DescriptorKey("obfuscated")
boolean value() default true;
}

MBean接口(interface):

public interface LoginMBean {
String getName();

@Obfuscated
String getPassword();
}

MBean 实现:

public class Login implements LoginMBean {
private final String user;
private final String password;

public Login(String user, String password) {
this.user = user;
this.password = password;
}

@Override public String getName() { return user; }

@Override public String getPassword() { return password; }
}

注册 MBean 并浏览其信息的一些代码:

import java.lang.management.ManagementFactory;
import javax.management.*;

public class Main {
public static void main(String[] args) {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName mbeanName = new ObjectName("com.mydomain", "type", "login");
server.registerMBean(
new StandardMBean(new Login("John Doe", "password"), LoginMBean.class), mbeanName);
MBeanInfo mbeanInfo = server.getMBeanInfo(mbeanName);
MBeanAttributeInfo[] attrs = mbeanInfo.getAttributes();
for (MBeanAttributeInfo attr: attrs) {
Descriptor desc = attr.getDescriptor();
boolean obfuscated = false;
if (desc.getFieldValue("obfuscated") != null) {
obfuscated = (Boolean) desc.getFieldValue("obfuscated");
}
if (obfuscated) System.out.printf("field '%s' is obfuscated%n", attr.getName());
else {
Object value = server.getAttribute(mbeanName, attr.getName());
System.out.printf("value of field '%s' is '%s'%n",
attr.getName(), value == null ? "null" : value.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

最后,运行Main后的输出:

value of field 'Name' is 'John Doe'field 'Password' is obfuscated

关于java - MBeanOperationInfo 和 MBeanAttributeInfo 元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25851774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com