You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by gdprao <gd...@yahoo.com> on 2008/05/27 23:48:33 UTC

Basic authentication with Spring configured client

Hi,

I have configured CXF client using spring configuration as follows:

	<bean id="searchClient"
		class="com.client.service.SearchService"
		factory-bean="searchClientFactory" factory-method="create"
lazy-init="true" />

	<bean id="searchClientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="com.client.service.SearchService" />
		<property name="address" value="${webservice.host.url}/${soapservice.url}"
/>
	</bean>

What is the configuration I  can add to send username and password with
basic authentication credentials?


Thanks,
Durga
-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p17500802.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Basic authentication with Spring configured client

Posted by Daniel Kulp <dk...@apache.org>.
On Thursday 09 October 2008 1:46:34 pm Daniel Kulp wrote:
> Add a createdFromAPI="true" attribute to the jaxws:client element.
>
> With that attribute set to "true", the jaxws:client becomes a configuration
> element for a client that you create from the CXF (or jaxws) API's.
>
> Without that attribute, it's an actual spring object that you need to
> lookup from the context.

One more thing....  if you do the createdFromAPI, the "id" needs to change to 
the URL of the endpoint.   Basically, something like:

id="{http://the.name.space.com/blah}HelloPort" 
or similar so the runtime can match the config with the proxy you are 
creating.

Dan



>
> Dan
>
> On Wednesday 08 October 2008 6:04:49 pm Sagi Mann wrote:
> > could you pls post the init code of the client? I'm having the same
> > issue, I'm using CXF 2.1.2 (which should contain the fix) but I'm not
> > sure how to write the client code so it can properly use the cxf.xml.
> >
> > My cxf xml is:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <beans xmlns="http://www.springframework.org/schema/beans"
> >         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >         xmlns:jaxws="http://cxf.apache.org/jaxws"
> >         xmlns:soap="http://cxf.apache.org/bindings/soap"
> >         xsi:schemaLocation="
> > http://www.springframework.org/schema/beans
> > http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
> > http://cxf.apache.org/bindings/soap
> > http://cxf.apache.org/schemas/configuration/soap.xsd
> > http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
> >
> >         <jaxws:client id="client1"
> >                 serviceClass="cxf.client.Hello"
> >                 address="http://mann2:8080/param-tester-ws/HelloService"
> >                 username="tester1" password="test"/>
> >
> > </beans>
> >
> > Note that I use the PORT class (Hello) as the serviceClass above. I'm not
> > sure if this is correct, but if I try to use the HelloService generated
> > class, I get an exception "HelloService is not an interface".
> >
> > My client code is:
> >
> >         SpringBusFactory bf = new SpringBusFactory();
> >         URL busFile = null;
> >         if ("secure".equals(args[1])) {
> >             busFile =
> > Hello_HelloImplPort_Client.class.getResource("/JaxwsSecureClient.xml");
> >         } else if ("insecure".equals(args[1])) {
> >             busFile =
> > Hello_HelloImplPort_Client.class.getResource("/InsecureClient.xml");
> >         } else {
> >             System.out.println("arg1 needs to be either secure or
> > insecure");
> >             System.exit(1);
> >         }
> >         Bus bus = bf.createBus(busFile.toString());
> >         bf.setDefaultBus(bus);
> >
> >         cxf.client.HelloService ss = new HelloService(wsdlURL,
> > SERVICE_NAME);
> >         cxf.client.Hello port = ss.getHelloPort();
> >         // invokations start here...
> >
> > thanks...



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: Basic authentication with Spring configured client

Posted by Daniel Kulp <dk...@apache.org>.
Add a createdFromAPI="true" attribute to the jaxws:client element.

With that attribute set to "true", the jaxws:client becomes a configuration 
element for a client that you create from the CXF (or jaxws) API's.  

Without that attribute, it's an actual spring object that you need to lookup 
from the context.

Dan


On Wednesday 08 October 2008 6:04:49 pm Sagi Mann wrote:
> could you pls post the init code of the client? I'm having the same issue,
> I'm using CXF 2.1.2 (which should contain the fix) but I'm not sure how to
> write the client code so it can properly use the cxf.xml.
>
> My cxf xml is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>         xmlns:jaxws="http://cxf.apache.org/jaxws"
>         xmlns:soap="http://cxf.apache.org/bindings/soap"
>         xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
> http://cxf.apache.org/bindings/soap
> http://cxf.apache.org/schemas/configuration/soap.xsd
> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
>
>         <jaxws:client id="client1"
>                 serviceClass="cxf.client.Hello"
>                 address="http://mann2:8080/param-tester-ws/HelloService"
>                 username="tester1" password="test"/>
>
> </beans>
>
> Note that I use the PORT class (Hello) as the serviceClass above. I'm not
> sure if this is correct, but if I try to use the HelloService generated
> class, I get an exception "HelloService is not an interface".
>
> My client code is:
>
>         SpringBusFactory bf = new SpringBusFactory();
>         URL busFile = null;
>         if ("secure".equals(args[1])) {
>             busFile =
> Hello_HelloImplPort_Client.class.getResource("/JaxwsSecureClient.xml");
>         } else if ("insecure".equals(args[1])) {
>             busFile =
> Hello_HelloImplPort_Client.class.getResource("/InsecureClient.xml");
>         } else {
>             System.out.println("arg1 needs to be either secure or
> insecure");
>             System.exit(1);
>         }
>         Bus bus = bf.createBus(busFile.toString());
>         bf.setDefaultBus(bus);
>
>         cxf.client.HelloService ss = new HelloService(wsdlURL,
> SERVICE_NAME);
>         cxf.client.Hello port = ss.getHelloPort();
>         // invokations start here...
>
> thanks...



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: Basic authentication with Spring configured client

Posted by Sagi Mann <sa...@gmail.com>.
I have an update: the following client code works:

        ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext(
                new String[]{"secure_cxf.xml"});
        
        Hello client = (Hello)context.getBean("client1");
        // invoke methods...

The only concern I have left is the fact that the user/pwd are clear-text in
the xml. Is there a way to define an external provider class that will
return them instead of writing them clear-text in the .xml? (I'm guessing
Spring can do something, but I'm no Spring expert...) or do I need to create
my own config layer on top of the xml?

thanks

-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p19889075.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Basic authentication with Spring configured client

Posted by Sagi Mann <sa...@gmail.com>.
could you pls post the init code of the client? I'm having the same issue,
I'm using CXF 2.1.2 (which should contain the fix) but I'm not sure how to
write the client code so it can properly use the cxf.xml.

My cxf xml is:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:jaxws="http://cxf.apache.org/jaxws" 
        xmlns:soap="http://cxf.apache.org/bindings/soap" 
        xsi:schemaLocation=" 
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 

        <jaxws:client id="client1" 
                serviceClass="cxf.client.Hello" 
                address="http://mann2:8080/param-tester-ws/HelloService"
                username="tester1" password="test"/> 

</beans> 

Note that I use the PORT class (Hello) as the serviceClass above. I'm not
sure if this is correct, but if I try to use the HelloService generated
class, I get an exception "HelloService is not an interface".

My client code is:

        SpringBusFactory bf = new SpringBusFactory();
        URL busFile = null;
        if ("secure".equals(args[1])) {
            busFile =
Hello_HelloImplPort_Client.class.getResource("/JaxwsSecureClient.xml");
        } else if ("insecure".equals(args[1])) {
            busFile =
Hello_HelloImplPort_Client.class.getResource("/InsecureClient.xml");
        } else {
            System.out.println("arg1 needs to be either secure or
insecure");
            System.exit(1);
        }
        Bus bus = bf.createBus(busFile.toString());
        bf.setDefaultBus(bus);

        cxf.client.HelloService ss = new HelloService(wsdlURL,
SERVICE_NAME);
        cxf.client.Hello port = ss.getHelloPort();
        // invokations start here...

thanks...
-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p19888502.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Basic authentication with Spring configured client

Posted by gdprao <gd...@yahoo.com>.
Yes,

I have created one https://issues.apache.org/jira/browse/CXF-1617 


willem.jiang wrote:
> 
> I think this could be a bug of CXF. Could you please fill a JIRA[1] for
> it?
> 
> [1]http://issues.apache.org/jira/browse/CXF
> 
> Willem
>  
> gdprao wrote:
>> Finally I could able to find the solution to this problem.  The following
>> configuration and code snippets might be useful to others struggling with
>> the same problem.  
>>
>> It looks like CXF had a bug in <jaxws:client/> not sending username and
>> password values.
>>
>> applicationContext.xml:
>> -----------------------
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <beans xmlns="http://www.springframework.org/schema/beans"
>> 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> 	xmlns:jaxws="http://cxf.apache.org/jaxws"
>> 	xmlns:soap="http://cxf.apache.org/bindings/soap"
>> 	xsi:schemaLocation="
>> http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
>> http://cxf.apache.org/bindings/soap
>> http://cxf.apache.org/schemas/configuration/soap.xsd
>> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
>>
>>
>> 	<bean id="propertyConfigurer"
>> 	
>> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
>> 		<property name="location"
>> 			value="WEB-INF/web_service.properties" />
>> 	</bean>
>>
>> 	<jaxws:client id="searchClient"
>> 		serviceClass="com.client.service.SearchService"
>> 		address="${webservice.host.url}/${soapservice.url}" />
>>
>> 	<bean id="searchServiceBean"
>> 		class="com.service.SearchBusinessServiceImpl">
>> 		<constructor-arg index="0" value="${username}"/>
>> 		<constructor-arg index="1" value="${password}"/>
>> 		<property name="searchService" ref="searchClient" />
>> 	</bean>
>>
>> </beans>
>>
>> com.service.SearchBusinessServiceImpl:
>> --------------------------------------
>>
>> public class SearchBusinessServiceImpl implements SearchBusinessService{
>> // Interface generated from WSDL
>> private SearchService searchService;
>> private String userName;
>> private String password;
>>
>> public SearchBusinessServiceImpl(String userName, String password){
>> 	this.userName=userName;
>> 	this.password=password;
>> }
>>
>> public void setSearchService(SearchService searchService){
>> 	this.searchService=searchService;
>> 	//Set basic authentication properties.
>> 	BindingProvider provider = (BindingProvider) searchService;
>> 	Map<String, Object> requestContext = provider.getRequestContext();
>> 	requestContext.put(BindingProvider.USERNAME_PROPERTY, this.userName);
>> 	requestContext.put(BindingProvider.PASSWORD_PROPERTY, this.password);
>> }
>>
>> }
>>
>> WEB-INF/web_service.properties:
>> --------------------------------
>> webservice.host.url=http://localhost:8080
>> soapservice.url=appcontext/services/searchService
>> username=uname
>> password=pword
>>
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p17525723.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Basic authentication with Spring configured client

Posted by Willem Jiang <wi...@gmail.com>.
I think this could be a bug of CXF. Could you please fill a JIRA[1] for it?

[1]http://issues.apache.org/jira/browse/CXF

Willem
 
gdprao wrote:
> Finally I could able to find the solution to this problem.  The following
> configuration and code snippets might be useful to others struggling with
> the same problem.  
>
> It looks like CXF had a bug in <jaxws:client/> not sending username and
> password values.
>
> applicationContext.xml:
> -----------------------
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
> 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> 	xmlns:jaxws="http://cxf.apache.org/jaxws"
> 	xmlns:soap="http://cxf.apache.org/bindings/soap"
> 	xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
> http://cxf.apache.org/bindings/soap
> http://cxf.apache.org/schemas/configuration/soap.xsd
> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
>
>
> 	<bean id="propertyConfigurer"
> 	
> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
> 		<property name="location"
> 			value="WEB-INF/web_service.properties" />
> 	</bean>
>
> 	<jaxws:client id="searchClient"
> 		serviceClass="com.client.service.SearchService"
> 		address="${webservice.host.url}/${soapservice.url}" />
>
> 	<bean id="searchServiceBean"
> 		class="com.service.SearchBusinessServiceImpl">
> 		<constructor-arg index="0" value="${username}"/>
> 		<constructor-arg index="1" value="${password}"/>
> 		<property name="searchService" ref="searchClient" />
> 	</bean>
>
> </beans>
>
> com.service.SearchBusinessServiceImpl:
> --------------------------------------
>
> public class SearchBusinessServiceImpl implements SearchBusinessService{
> // Interface generated from WSDL
> private SearchService searchService;
> private String userName;
> private String password;
>
> public SearchBusinessServiceImpl(String userName, String password){
> 	this.userName=userName;
> 	this.password=password;
> }
>
> public void setSearchService(SearchService searchService){
> 	this.searchService=searchService;
> 	//Set basic authentication properties.
> 	BindingProvider provider = (BindingProvider) searchService;
> 	Map<String, Object> requestContext = provider.getRequestContext();
> 	requestContext.put(BindingProvider.USERNAME_PROPERTY, this.userName);
> 	requestContext.put(BindingProvider.PASSWORD_PROPERTY, this.password);
> }
>
> }
>
> WEB-INF/web_service.properties:
> --------------------------------
> webservice.host.url=http://localhost:8080
> soapservice.url=appcontext/services/searchService
> username=uname
> password=pword
>
>   


Re: Basic authentication with Spring configured client

Posted by gdprao <gd...@yahoo.com>.
Finally I could able to find the solution to this problem.  The following
configuration and code snippets might be useful to others struggling with
the same problem.  

It looks like CXF had a bug in <jaxws:client/> not sending username and
password values.

applicationContext.xml:
-----------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">


	<bean id="propertyConfigurer"
	
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location"
			value="WEB-INF/web_service.properties" />
	</bean>

	<jaxws:client id="searchClient"
		serviceClass="com.client.service.SearchService"
		address="${webservice.host.url}/${soapservice.url}" />

	<bean id="searchServiceBean"
		class="com.service.SearchBusinessServiceImpl">
		<constructor-arg index="0" value="${username}"/>
		<constructor-arg index="1" value="${password}"/>
		<property name="searchService" ref="searchClient" />
	</bean>

</beans>

com.service.SearchBusinessServiceImpl:
--------------------------------------

public class SearchBusinessServiceImpl implements SearchBusinessService{
// Interface generated from WSDL
private SearchService searchService;
private String userName;
private String password;

public SearchBusinessServiceImpl(String userName, String password){
	this.userName=userName;
	this.password=password;
}

public void setSearchService(SearchService searchService){
	this.searchService=searchService;
	//Set basic authentication properties.
	BindingProvider provider = (BindingProvider) searchService;
	Map<String, Object> requestContext = provider.getRequestContext();
	requestContext.put(BindingProvider.USERNAME_PROPERTY, this.userName);
	requestContext.put(BindingProvider.PASSWORD_PROPERTY, this.password);
}

}

WEB-INF/web_service.properties:
--------------------------------
webservice.host.url=http://localhost:8080
soapservice.url=appcontext/services/searchService
username=uname
password=pword

-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p17521301.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Basic authentication with Spring configured client

Posted by gdprao <gd...@yahoo.com>.
Hi,

I have tried following other options as well but I don't see the basic
authentication header is not passing to the server from the client.  I see
the username and password attributes are available in the XSD.  I am using
CXF-2.0.3 version.  Please help.

Front end approach:

<bean id="searchClientFactory" 
                class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
     <property name="serviceClass" 
                        value="com.client.service.SearchService" /> 
     <property name="address"
value="${webservice.host.url}/${soapservice.url}" /> 
     <property name="username" value="uname"/>
      <property name="password" value="pword"/>
 </bean> 

jaxws approach:
	<jaxws:client id="searchClient"
	serviceClass="com.client.service.SearchService"
	address="${webservice.host.url}/${soapservice.url}"  username="uname"
password="pword"/>


-- 
View this message in context: http://www.nabble.com/Basic-authentication-with-Spring-configured-client-tp17500802p17504751.html
Sent from the cxf-user mailing list archive at Nabble.com.