it´s about 3days that I´m trying to create a user in WLS8.1
dynamically by using the following class , I use this class in a
signup page flow ,I´d made the roles and groups and did role
assignment to groups .Surprisingly I got a null exception which caused
by javax.management.MBeanException ,I´d searched in forums.bea.com but
no result came! ,here is the code i run for adding a user .
userManager=new
UserManager("weblogic","weblogic","t3://localhost:7001","portalServer");
userManager.addUser(aForm.username,aForm.password,"signed
up bonusClient user" );
I even don´t know what to put in url field http:// or ldap:// or t3://
I used the t3 because I found a sample in forums that used it ,I
think it should be related to transactions that I haven´t did anything
about it in the configuration of portal server.
and the UserManager Class:
package net.xagrose.userManagement;
import net.xagrose.exception.BlankException;
import weblogic.management.MBeanHome;
import weblogic.management.configuration.DomainMBean;
import weblogic.management.configuration.SecurityConfigurationMBean;
import weblogic.management.security.RealmMBean;
import weblogic.management.Helper;
import
weblogic.management.security.authentication.AuthenticationProviderMBean;
import weblogic.management.security.authentication.GroupEditorMBean;
import weblogic.management.security.authentication.UserEditorMBean;
import
weblogic.management.security.authentication.UserPasswordEditorMBean;
import weblogic.management.commo.CommoMBeanServer;
import weblogic.management.commo.Config;
import weblogic.management.utils.AlreadyExistsException;
import weblogic.management.utils.InvalidParameterException;
import weblogic.management.utils.NotFoundException;
import
weblogic.security.providers.authentication.DefaultAuthenticatorMBean;
import javax.management.JMException;
import javax.management.ObjectName;
public class UserManager
{
private MBeanHome adminHome;
private DomainMBean domain ;
private SecurityConfigurationMBean security;
private RealmMBean realm ;
private GroupEditorMBean gEditor;
private UserPasswordEditorMBean upEditor;
private UserEditorMBean uEditor;
private String adminName,adminPwd,serverName,url;
private AuthenticationProviderMBean [] providers;
private CommoMBeanServer cmbs ;
public UserManager(String adminName, String adminPwd, String url,
String serverName)throws InvalidParameterException,
NotFoundException,JMException{
this.adminName=adminName;
this.adminPwd=adminPwd;
this.serverName=serverName;
this.url=url;
initialUserManager();
}
private void initialUserManager()throws InvalidParameterException,
NotFoundException,JMException{
//CommoMBeanServer cmbs = Config.getMBeanServer(serverName,
url, adminName, adminPwd);
adminHome =
(MBeanHome)Helper.getMBeanHome(adminName,adminPwd,url,serverName);
domain = adminHome.getActiveDomain();
security =domain.getSecurityConfiguration();
realm = security.findDefaultRealm();
providers = realm.getAuthenticationProviders();
}
/**
* add a user to LDAP default provider
*/
public boolean addUser(String username,String psw,String
desc)throws BlankException, InvalidParameterException, NotFoundException{
for (int i = 0; i < providers.length; i++) {
if (providers[i] instanceof UserEditorMBean) {
uEditor =(UserEditorMBean)providers[i];
try
{
uEditor.createUser(username,psw,desc);
}
catch (Exception e)
{
throw new BlankException(e.getMessage()+e.getCause());
}
}
return true;
}
return false;
}
/**
* add a user to a group
*/
public boolean addUserToGroup(String username,String
groupName)throws BlankException,InvalidParameterException,
NotFoundException{
for (int i = 0; i < providers.length; i++) {
if (providers[i] instanceof GroupEditorMBean) {
gEditor =(GroupEditorMBean)providers[i];
try
{
gEditor.addMemberToGroup(username,groupName);
}
catch (Exception e){
throw new BlankException(e.getMessage()+e.getCause());
}
}
return true;
}
return false;
}
/**
* change a user password
*/
public void changeUserPassword(String username,String
oldPsw,String newPsw)throws InvalidParameterException, NotFoundException{
for (int i = 0; i < providers.length; i++) {
if (providers[i] instanceof UserPasswordEditorMBean) {
upEditor =(UserPasswordEditorMBean)providers[i];
try
{
upEditor.changeUserPassword(username,oldPsw,newPsw);
}
catch (Exception e)
{
}
}
}
}
public void createUser2(){
try {
DefaultAuthenticatorMBean dbean =
(DefaultAuthenticatorMBean) cmbs.getMBean(new
ObjectName("Security:Name=myrealmDefaultAuthenticator"));
//create a new user
dbean.createUser("testuser", "password", "a test user");
//create a new group
dbean.createGroup("testGrp","a test group");
//add the user to group
dbean.addMemberToGroup("testGrp","testuser");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvalidParameterException e) {
e.printStackTrace();
} catch (JMException e) {
e.printStackTrace();
} catch (AlreadyExistsException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
I have tested two ways by two different methods .addUser() and
.createUser() ,both of them throw the same Exception , firstly , I
thaugh I might be cause by not setting the MBeans in my class pass ,
but after setting it , no changes came up!