Announcement Announcement Module
Collapse
No announcement yet.
Basic authentication for Webservices using Spring Integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Basic authentication for Webservices using Spring Integration

    Hi,

    I am trying to integrate my webservices using Spring Integration.But the problem is that my webservices server require basic authentication.
    I am using message template to set the request and i am not able to figure out where i should set the username and password.
    I am getting the org.springframework.ws.client.WebServiceTransportE xception: Unauthorized [401] error.
    Where should i set my username and password for basic authentication?I am using a ws:outboundgateway.
    here is my code:

    Code:
    public static void main(String[] args)
    	{
    		ApplicationContext context = new FileSystemXmlApplicationContext(
    				"/WebContent/WEB-INF/spring/integration/temperature-config.xml");
    
    		RecSOAPReq recSOAPReq = new RecSOAPReq();
    		recSOAPReq.setCode("0000055073");
    		recSOAPReq.setDivision("10");
    		
    		MessageChannel channel = context.getBean("requests", MessageChannel.class);
    
    		MessagingTemplate messagingTemplate = context.getBean("messagingTemplate", MessagingTemplate.class);
    
    		Message<RecPROXY> output = (Message<RecPROXY>) messagingTemplate.sendAndReceive(channel,MessageBuilder.withPayload(recSOAPReq).build());
    
    		System.out.println("OUTPUT IS " + output.getPayload());
    	}
    here is my Configuration File

    Code:
    <?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:ws="http://www.springframework.org/schema/integration/ws"
    	xmlns:int="http://www.springframework.org/schema/integration"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/integration
    http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
    http://www.springframework.org/schema/integration/ws
    http://www.springframework.org/schema/integration/ws/spring-integration-ws-2.0.xsd">
    		
    <int:channel id="requests" />
    	
    	<ws:outbound-gateway id="gateway" request-channel="requests"   uri="http://localhost/myurl"
    		marshaller="jaxb2Marshaller" unmarshaller="jaxb2Marshaller" />
    
    
    	<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    		<property name="classesToBeBound">
    			<list>
    				<value>com.sales.bill_rec.RecSOAPReq</value>
    				<value>com.sales.bill_rec.RecPROXY</value>
    			</list>
    		</property>
    
    	</bean>
    
    	<bean id="messagingTemplate" class="org.springframework.integration.core.MessagingTemplate"/>
    </beans>
    This is the error i am getting:

    Code:
    Exception in thread "main" org.springframework.integration.MessageHandlingException: error occurred in message handler [org.springframework.integration.ws.MarshallingWebServiceOutboundGateway#0]
    	at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:84)
    	at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110)
    	at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
    	at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:44)
    	at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    	at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
    	at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
    	at org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
    	at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
    	at com.sale.bill_rec.Test.main(Test.java:34)
    Caused by: org.springframework.ws.client.WebServiceTransportException: Unauthorized [401]
    	at org.springframework.ws.client.core.WebServiceTemplate.handleError(WebServiceTemplate.java:622)
    	at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:546)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:496)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:343)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:337)
    	at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.doHandle(MarshallingWebServiceOutboundGateway.java:66)
    	at org.springframework.integration.ws.AbstractWebServiceOutboundGateway.handleRequestMessage(AbstractWebServiceOutboundGateway.java:104)
    	at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:98)
    	at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    	... 9 more
    Please help...
    Thanks in advance.
    Last edited by annuk; Oct 13th, 2011, 01:46 AM.

  • #2
    Hello.
    You have to use messageSender:
    HTML Code:
    <bean id="messageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender"
              p:credentials-ref="credentials"/>
    
    <ws:outbound-gateway id="gateway" request-channel="requests"   uri="http://localhost/myurl"
    		marshaller="jaxb2Marshaller" unmarshaller="jaxb2Marshaller" 
                    message-sender="messageSender"/> <!-- HERE -->
    Good luck,
    Artem Bilan

    Comment


    • #3
      Hi Artem,

      Thanks for your reply..
      I tried your solution.

      This is my new configuration:
      Code:
      <?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:ws="http://www.springframework.org/schema/integration/ws"
          xmlns:int="http://www.springframework.org/schema/integration"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/integration
      http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
      http://www.springframework.org/schema/integration/ws
      http://www.springframework.org/schema/integration/ws/spring-integration-ws-2.0.xsd">
              
      <int:channel id="requests" />
          
       <bean name="messageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
                  <property name="credentials">
                      <bean class="org.apache.commons.httpclient.UsernamePasswordCredentials">
                          <constructor-arg value="shane"/>
                          <constructor-arg value="shane"/>
                      </bean>
                  </property>
            </bean>
      
          <ws:outbound-gateway id="gateway" request-channel="requests"   uri="http://localhost/myurl"
              marshaller="jaxb2Marshaller" unmarshaller="jaxb2Marshaller" message-sender="messageSender" />
      
      
          <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
              <property name="classesToBeBound">
                  <list>
                      <value>com.sales.bill_rec.RecSOAPReq</value>
                      <value>com.sales.bill_rec.RecPROXY</value>
                  </list>
              </property>
      
          </bean>
      
          <bean id="messagingTemplate" class="org.springframework.integration.core.MessagingTemplate"/>
      </beans>

      but got the following error:
      Code:
      Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.ws.MarshallingWebServiceOutboundGateway#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.ws.MarshallingWebServiceOutboundGateway]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.ws.client.core.WebServiceTemplate.setDestinationProvider(Lorg/springframework/ws/client/support/destination/DestinationProvider;)V
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:286)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
          at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
          at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
          at com.sale.bill_rec.Test.main(Test.java:14)
      Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.ws.MarshallingWebServiceOutboundGateway]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.ws.client.core.WebServiceTemplate.setDestinationProvider(Lorg/springframework/ws/client/support/destination/DestinationProvider;)V
          at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:105)
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
          ... 14 more
      Caused by: java.lang.NoSuchMethodError: org.springframework.ws.client.core.WebServiceTemplate.setDestinationProvider(Lorg/springframework/ws/client/support/destination/DestinationProvider;)V
          at org.springframework.integration.ws.AbstractWebServiceOutboundGateway.<init>(AbstractWebServiceOutboundGateway.java:57)
          at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.<init>(MarshallingWebServiceOutboundGateway.java:37)
          at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.<init>(MarshallingWebServiceOutboundGateway.java:45)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
          at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
          ... 16 more
      I tried setting p:credentials-ref="credentials" too but i am not getting the name space associated with p to set in my config file.'xmlns: p'=??
      Please help.
      Thanks in advance
      Last edited by annuk; Oct 13th, 2011, 01:47 AM.

      Comment


      • #4
        You have to use Spring-WS 2.0.2.RELEASE

        Comment


        • #5
          Hi,

          Thanks
          i think the basic authentication worked.
          I used the spring-ws-core-2.0.2.RELEASE.jar
          But i got the following error which i think is my webservices error:

          Code:
          log4j:WARN Please initialize the log4j system properly.
          Exception in thread "main" org.springframework.integration.MessageHandlingException: error occurred in message handler [org.springframework.integration.ws.MarshallingWebServiceOutboundGateway#0]
              at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:84)
              at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110)
              at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
              at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:44)
              at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
              at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
              at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
              at org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
              at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
              at com.sales.bill_rec.Test.main(Test.java:32)
          Caused by: org.springframework.ws.soap.client.SoapFaultClientException: Server Error
              at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
              at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:733)
              at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:559)
              at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:496)
              at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:343)
              at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:337)
              at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.doHandle(MarshallingWebServiceOutboundGateway.java:66)
              at org.springframework.integration.ws.AbstractWebServiceOutboundGateway.handleRequestMessage(AbstractWebServiceOutboundGateway.java:104)
              at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:98)
              at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
              ... 9 more
          Thanks for your replies.

          Comment

          Working...
          X