# CreateUser.jws Sample

This topic inludes the source code for the CreateUser.jws Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/security/roleBased/createUser/

## Sample Source Code

`001 package security.roleBased.createUser; 002 003 004 import java.util.Vector; 005 import weblogic.management.Helper; 006 import weblogic.management.MBeanHome; 007 import weblogic.management.WebLogicObjectName; 008 import weblogic.management.configuration.SecurityConfigurationMBean; 009 import weblogic.management.security.authentication.AuthenticationProviderMBean; 010 import weblogic.management.security.authentication.UserEditorMBean; 011 import weblogic.management.security.authentication.UserReaderMBean; 012 import weblogic.security.providers.authentication.DefaultAuthenticatorMBean; 013 014 /** 015 * The following web service demonstrates the attributes 016 * 017 * common:security run-as-principal 018 * 019 * and 020 * 021 * common:security run-as 022 * 023 * Note that the user weblogic and the role Administrators are pre-defined in the default 024 * authentication provider in the WebLogic Server security framework. 025 * 026 * The attributes @common:security run-as-principal="weblogic" run-as="Administrators" cause the 027 * following elements to be written to the deployment descriptor weblogic-ejb.jar: 028 * 029 * <security-role-assignment> 030 * <role-name>Administrators</role-name> 031 * <externally-defined/> 032 * </security-role-assignment> 033 * 034 * @common:security run-as-principal="weblogic" run-as="Administrators" 035 * @common:target-namespace namespace="http://openuri.org/bea/samples/workshop/WebServices/security/roleBased/createUser" 036 */ 037 public class createUser implements com.bea.jws.WebService 038 { 039 static final long serialVersionUID = 1L; 040 041 /** 042 * <p>This method lists all of the users in each authentication provider used by WebLogic Server. 043 * 044 * @common:operation 045 */ 046 public Vector listUsers() 047 { 048 Vector users = new Vector(); 049 050 MBeanHome adminHome; 051 052 String url = "http://127.0.0.1:7001"; 053 054 adminHome = (MBeanHome)Helper.getMBeanHome("weblogic","weblogic",url,"cgServer"); 055 056 AuthenticationProviderMBean[] providers = adminHome.getActiveDomain().getSecurityConfiguration().findDefaultRealm().getAuthenticationProviders(); 057 058 for (int i=0; providers != null && i <providers.length; i++) 059 { 060 if (providers[i] instanceof UserReaderMBean) 061 { 062 UserReaderMBean reader = (UserReaderMBean)providers[i]; 063 064 try 065 { 066 String cursor = reader.listUsers("*",100); 067 068 while (reader.haveCurrent(cursor)) 069 { 070 users.add(reader.getCurrentName(cursor)); 071 reader.advance(cursor); 072 } 073 074 } 075 catch (Exception e) 076 { 077 e.printStackTrace(); 078 } 079 } 080 } 081 082 return users; 083 } 084 085 /** 086 * This method adds a user to WebLogic Server's default authenticator 087 * 088 * <p>The password must be 8 or more characters long. 089 * 090 * @common:operation 091 */ 092 public void addUser(String username, String password) 093 { 094 MBeanHome adminHome; 095 096 String url = "http://127.0.0.1:7001" ; 097 098 adminHome = (MBeanHome)Helper.getMBeanHome("weblogic","weblogic",url,"cgServer"); 099 100 //providers[0] is the default authenticator 101 AuthenticationProviderMBean[] providers = adminHome.getActiveDomain().getSecurityConfiguration().findDefaultRealm().getAuthenticationProviders(); 102 103 UserEditorMBean editor = (UserEditorMBean)providers[0]; 104 105 try 106 { 107 editor.createUser(username, password, "This user created by the createUser web service."); 108 } 109 catch (Exception e) 110 { 111 e.printStackTrace(); 112 } 113 } 114 115 116 /** 117 * This method deletes a user from WebLogic Server's default authenticator. 118 * 119 * @common:operation 120 */ 121 public void deleteUser(String username) 122 { 123 MBeanHome adminHome; 124 125 String url = "http://127.0.0.1:7001" ; 126 127 adminHome = (MBeanHome)Helper.getMBeanHome("weblogic","weblogic",url,"cgServer"); 128 129 AuthenticationProviderMBean[] providers = adminHome.getActiveDomain().getSecurityConfiguration().findDefaultRealm().getAuthenticationProviders(); 130 131 //providers[0] is the default authenticator 132 UserEditorMBean editor = (UserEditorMBean)providers[0]; 133 134 try 135 { 136 editor.removeUser(username); 137 } 138 catch (Exception e) 139 { 140 e.printStackTrace(); 141 } 142 } 143 }`