Hi,
Here is a Simple demonstration of using the UserName Token in WebLogic JAXWS WebServices.
Step1). Create a Directory “F:DELETEAuthXML_UserName_Token” where we can place all our application sources…
Step2). Now we need to create our own UserNameToken.xml file. In order to do that Please create a Directory “policies” in the current directory “F:DELETEAuthXML_UserName_Token” and place the following “UsernameToken.xml”
<?xml version="1.0"?> <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200512"> <sp:SupportingTokens> <wsp:Policy> <sp:UsernameToken sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200512/IncludeToken/AlwaysToRecipient"> <wsp:Policy> <sp:WssUsernameToken10/> </wsp:Policy> </sp:UsernameToken> </wsp:Policy> </sp:SupportingTokens> </wsp:Policy>
Step3). Now In the “F:DELETEAuthXML_UserName_Token” Directory Write your WebService “SecureHelloWorldImpl.java“
package examples.webservices.security_jws; import weblogic.jws.Policies; import weblogic.jws.Policy; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.soap.SOAPBinding; @SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED) @Policies({@Policy(uri="policy:UsernameToken.xml")}) @WebService(name="SecureHelloWorldPortType",serviceName="SecureHelloWorldService") public class SecureHelloWorldImpl { @WebMethod() public String sayHello(String s) { return "JAXWS Service Says ... Hello " + s; } }
Step4). Now write the WebService Client in “F:DELETEAuthXML_UserName_Token” the Client name is “SecureHelloWorldClient.java“
package client; import examples.webservices.security_jws.client.*; import java.util.*; import javax.xml.ws.BindingProvider; import weblogic.wsee.security.unt.ClientUNTCredentialProvider; import weblogic.xml.crypto.wss.provider.CredentialProvider; import weblogic.xml.crypto.wss.WSSecurityContext; public class SecureHelloWorldClient { public static void main(String[] args) throws Throwable { String USERNAME = "weblogic"; String PASSWORD = "weblogic"; SecureHelloWorldService service=new SecureHelloWorldService(); SecureHelloWorldPortType port=service.getSecureHelloWorldPortTypePort(); //////////NOT TRY TO COMMENT OUT THE BELOW 8 Lines....Client Will Fail With Security Header Missing Exception System.out.println("nnt-----1 Port: "+port); BindingProvider bindingProvider = (BindingProvider) port; Map<String,Object> rc = (Map<String,Object>)bindingProvider.getRequestContext(); List<CredentialProvider> credProviders = new ArrayList<CredentialProvider>(); credProviders.add(new ClientUNTCredentialProvider(USERNAME.getBytes(),PASSWORD.getBytes())); rc.put(WSSecurityContext.CREDENTIAL_PROVIDER_LIST, credProviders); rc.put(BindingProvider.USERNAME_PROPERTY, USERNAME); rc.put(BindingProvider.PASSWORD_PROPERTY, PASSWORD);; ///////////////////////////////////////////////////////////// String response = port.sayHello("World"); System.out.println("response = " + response); } }
Step 5). Now we need to write the “build.xml” so build the Service and the Client…
<project name="webservices-security_jws" default="all" basedir="."> <!-- set global properties for this build --> <property name="ws.file" value="SecureHelloWorldImpl" /> <property name="ear.dir" value="${basedir}/webservicesSecurityeEar" /> <property name="client.dir" value="${basedir}/webservicesSecurity_client" /> <property name="cert-dir" value="C:replicationcerts" /> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="AdminServer" /> <path id="client.class.path"> <pathelement path="${client.dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <target name="all" depends="clean,server,zip,deploy,client,run" /> <!-- Cleaning the WorkSpace --> <target name="build" depends="clean,server,client" /> <target name="clean"> <delete dir="${ear.dir}"/> <delete dir="${client.dir}"/> </target> <!-- To Build the WebService --> <target name="server"> <mkdir dir="${ear.dir}"/> <jwsc srcdir="${basedir}" destdir="${ear.dir}" classpath="${java.class.path}" fork="true" keepGenerated="true" deprecation="${deprecation}" debug="true"> <jws file="${ws.file}.java" type="JAXWS"/> </jwsc> </target> <!-- To add the Policy File Inside the WebService WAR file --> <target name="zip"> <zip destfile="${ear.dir}/${ws.file}.war" update="true"> <zipfileset dir="." prefix="WEB-INF"> <include name="policies/*.xml"/> </zipfileset> </zip> </target> <!-- Deploy Application --> <target name="deploy"> <wldeploy action="deploy" source="${ear.dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <!-- Redeploy Application --> <target name="redeploy"> <wldeploy action="redeploy" source="${ear.dir}" user="${wls.username}" password="${wls.password}" verbose="true" failonerror="${failondeploy}" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <!-- To Generate the Client Side Artifacts --> <target name="client"> <mkdir dir="${client.dir}"/> <clientgen type="JAXWS" wsdl="http://localhost:7001/SecureHelloWorldImpl/SecureHelloWorldService?WSDL" destDir="${client.dir}" classpath="${java.class.path}" packageName="examples.webservices.security_jws.client"/> <javac srcdir="${client.dir}" destdir="${client.dir}" classpath="${java.class.path}" includes="examples/webservices/security_jws/client/**/*.java"/> <javac srcdir="${basedir}" destdir="${client.dir}" classpath="${java.class.path};${client.dir}" includes="SecureHelloWorldClient.java"/> </target> <!-- To Run the Client Program --> <target name="run" > <java fork="true" classname="client.SecureHelloWorldClient" failonerror="false" > <classpath refid="client.class.path"/> <arg line="weblogic weblogic" /> </java> </target> </project>
Step6). Now open a Command prompt and then run “setWLSEnv.cmd” or “setWLSEnv.sh” and then just enter “ant” in the command prompt…
.
.
Thanks
Jay SenSharma