参考How it's done: Password change functionality in OBIEE
思路:
- 理解域运行时 MBean: myrealmDefaultAuthenticator的changeUserPassword操作 ;
- 创建Web Service并发布;
- 创建共享操作链接。
关键代码:
package userselfhelp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
@WebService
public class ChgUsrPswd {
private static JMXConnector cctor;
private static MBeanServerConnection mbsc;
//初始化连接
@WebMethod(exclude = true)
@SuppressWarnings("unchecked")
public static void InlCctn(String hostname, String portString, String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
cctor = JMXConnectorFactory.connect(serviceURL, h);
mbsc = cctor.getMBeanServerConnection();
}
// 下面方法用于重置密码及验证
@WebMethod(exclude = true)
public String changeUserPassword(String usrID, String oldPswd, String newPswd, String cfrmPswd) throws Exception {
ObjectName securityMBeanName = new ObjectName("Security:Name=myrealmDefaultAuthenticator");
Object objUser[] = new Object[] { (usrID), (oldPswd), (newPswd) };
String objStr[] = new String[] { ("java.lang.String"), ("java.lang.String"), ("java.lang.String") };
try {
if (cfrmPswd.equals(newPswd)) {
mbsc.invoke(securityMBeanName, "changeUserPassword", objUser, objStr);
return "重置密码成功";
} else {
return "新密码不匹配";
}
} catch (Exception e) {
if (e.getCause()
.getMessage()
.contains("Validation of old password failed"))
return "旧密码验证失败";
else if (e.getCause()
.getMessage()
.contains("must Be Eight Chars"))
return "密码长度必须为8位字符";
else if (e.getCause()
.getMessage()
.contains("missing Special Chars"))
return "密码至少包含1个数字或特殊字符";
else
return "未能重置密码,请联系管理员";
}
}
public String ChgPswd(String usrID, String oldPswd, String newPswd, String cfrmPswd) throws Exception {
ChgUsrPswd c = new ChgUsrPswd();
InlCctn("ip", "port", "Admin-user", "Admin-password");
String result = c.changeUserPassword(usrID, oldPswd, newPswd, cfrmPswd);
cctor.close();
return result;
}
}
建议使用JDeveloper Studio,可以一键部署。