2016-05-23 12 views
2

Ich versuche, einen Web Service Client in Java zu instanziieren, dessen URL durch Benutzernamen und Passwort gesichert ist. Wenn es instanziiert wird, wird eine WebServiceException ausgelöst. hier ist die Stacktrace:JAX-WS WebService Client - "Antwort: '401: Nicht autorisiert' für die URL"

javax.xml.ws.WebServiceException: java.io.FileNotFoundException: Response: '401: Unauthorized' for url: WSDLURL 
at com.sun.xml.ws.wsdl.WSDLContext.<init>(WSDLContext.java:68) 
at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:207) 
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165) 
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:49) 
at weblogic.wsee.jaxws.spi.WLSProvider.createServiceDelegate(WLSProvider.java:18) 
at javax.xml.ws.Service.<init>(Service.java:57) 
at org.openuri.ServiceUtilityWS.<init>(ServiceUtilityWS.java:36) 
at it.cartasi.pb.giustificatomotivo.util.Util.sendEmail(Util.java:110) 
at it.cartasi.pb.giustificatomotivo.action.ConfermaAction.execute(ConfermaAction.java:36) 
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419) 
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224) 
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) 
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226) 
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124) 
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) 
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26) 
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42) 
at it.cartasi.pb.giustificatomotivo.filter.SessionFilter.doFilter(SessionFilter.java:68) 
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42) 
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402) 
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) 
at weblogic.security.service.SecurityManager.runAs(Unknown Source) 
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2140) 
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2046) 
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1398) 
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200) 
at weblogic.work.ExecuteThread.run(ExecuteThread.java:172) 
java.io.FileNotFoundException: Response: '401: Unauthorized' for url: WSDLURL 
    at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:467) 
    at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:36) 
    at java.net.URL.openStream(URL.java:1007) 
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:666) 
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(RuntimeWSDLParser.java:152) 
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:99) 
    at com.sun.xml.ws.wsdl.WSDLContext.<init>(WSDLContext.java:65) 
    at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:207) 
    at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165) 
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:49) 
    at weblogic.wsee.jaxws.spi.WLSProvider.createServiceDelegate(WLSProvider.java:18) 
    at javax.xml.ws.Service.<init>(Service.java:57) 
    at org.openuri.ServiceUtilityWS.<init>(ServiceUtilityWS.java:36) 
    at it.cartasi.pb.giustificatomotivo.util.Util.sendEmail(Util.java:110) 
    at it.cartasi.pb.giustificatomotivo.action.ConfermaAction.execute(ConfermaAction.java:36) 
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419) 
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224) 
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) 
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226) 
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124) 
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) 
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26) 
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42) 
    at it.cartasi.pb.giustificatomotivo.filter.SessionFilter.doFilter(SessionFilter.java:68) 
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42) 
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402) 
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) 
    at weblogic.security.service.SecurityManager.runAs(Unknown Source) 
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2140) 
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2046) 
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1398) 
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200) 
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:172) 

Hier mein Code-Schnipsel (ich auch das Standard-Authenticator eingestellt):

Authenticator.setDefault(new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      char[] charArray = password.toCharArray(); 
      System.out.println(user); 
      System.out.println(charArray); 
      return new PasswordAuthentication(
        user, 
        charArray); 
     } 
    }); 

    try { 
     ws = new ServiceUtilityWS(new URL(wsdlUrl), new QName("ServiceUtilityWSSoap")); // Exception thrown here 
    } catch (MalformedURLException e) { 
    } 

ich es über den Browser, diese Anmeldeinformationen zugreifen kann. Weiß jemand, warum passiert das?

Hinweis:

Die Anwendung auf Weblogic eingesetzt wird 10. Wenn ich den Code aus dem Haupt Ausnahme ausgeführt wird, nicht geworfen.

Antwort

2

Es war ein Weblogic 10 Problem aufgrund seiner URLStreamHandler. Ich löste es durch folgende Maßnahmen:

  • die WSDL-URL wie folgt instanziiert:

    URLStreamHandler handler = new sun.net.www.protocol.http.Handler(); //standard http handler 
    URL url = null; 
    try { 
        url = new URL(null, wsdlUrl, handler); //forced this http handler here 
        ws = ServiceUtilityWS.create(url, qName); 
    } catch (MalformedURLException e) { 
    } 
    
  • Danach lösen, ich auf dem Service-Betrieb Anruf eine ähnliche Ausnahme hatte. Zu lösen, habe ich einen benutzerdefinierten Handler an die SOAP-Nachricht Kette:

     Binding aBinding = bindingProvider.getBinding(); 
        List<Handler> handlerChain = aBinding.getHandlerChain(); 
        handlerChain.add(new SOAPHandler<SOAPMessageContext>() { 
    
         public Set<QName> getHeaders() { 
         return new TreeSet<QName>(); 
         } 
    
         public boolean handleMessage(SOAPMessageContext context) { 
         final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
    
         if (outInd.booleanValue()) { 
          try { 
    
           context.put(BindingProvider.USERNAME_PROPERTY, user); 
           context.put(BindingProvider.PASSWORD_PROPERTY, password); 
    
          } catch (final Exception e) { 
           return false; 
          } 
         } 
    
         return true; 
        }