2012-08-09 49 views
1

我正在尝试访问我正在处理的项目的Web服务。我正在使用JAX-WS,并且该应用程序部署在weblogic上。当我尝试访问WS,我得到下面的异常:javax.xml.ws.WebServiceException:无法访问WSDL。回应:'401:未经授权'

javax.portlet.PortletException: javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://xxx.xxxx.ro:40000/idm/ws/cup?wsdl. It failed with: 
Response: '401: Unauthorized' for url: 'http://xxx.xxxx.ro:40000/idm/ws/cup?wsdl'. 
at com.bea.portlet.container.PortletStub.processAction(PortletStub.java:346) 
at com.bea.portlet.container.AppContainer.invokeProcessAction(AppContainer.java:678) 
........ 

我通过有关问题很多帖子的阅读,我尝试了不同类型的权威性的。我试图在不同的用例中使用BindingProvider,basicHTTPAuth,禁用HostnameVerifier等,但仍然没有结果。

下面是我的代码片段,作为最后的尝试版本:

Authenticator.setDefault(new Authenticator() { 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(
          username, 
          password.toCharArray()); 
        } 
       }); 


    ComputeUserProfileImplService computeUserProfileImplService = new ComputeUserProfileImplService(
    new URL(null, endpointURL, new sun.net.www.protocol.http.Handler()), 
    new QName("http://xxx.xx.xxxxx.xxxxx.com/", 
      "ComputeUserProfileImplService")); 
    ComputeUserProfileImpl computeUserProfile = computeUserProfileImplService 
    .getComputeUserProfileImplPort(); 

的ComputeUserProfileImplService代码如下所示:

private final static URL COMPUTEUSERPROFILEIMPLSERVICE_WSDL_LOCATION; 
private final static Logger logger = Logger.getLogger(com.xxxxx.xxxxx.xx.xxxxx.cup.ComputeUserProfileImplService.class.getName()); 

static { 
    URL url = null; 
    try { 
     URL baseUrl; 
     baseUrl = com.xxxxx.xxxxx.xx.xxxxx.xxx.ComputeUserProfileImplService.class.getResource(""); 
     url = new URL(baseUrl, "http://xxxx.xxxxx.ro:40000/idm/ws/cup?wsdl"); 

    } catch (MalformedURLException e) { 
     logger.warning("Failed to create URL for the wsdl Location: 'xxxxxxxxxxxxxxxx', retrying as a local file"); 
     logger.warning(e.getMessage()); 
    } 
    COMPUTEUSERPROFILEIMPLSERVICE_WSDL_LOCATION = url; 
} 

对不起更换的联系,但我不认可张贴他们,因为它是一个非常知名的组织。如果你能帮助我一些建议,我将不胜感激。我一直在寻找解决方案,但我卡住了..我无法弄清楚。它应该是适用于我的这个weblogic问题的解决方法......但我无法找到它。如果你需要它,我会发布一些其他片段。希望我对此很清楚。

在此先感谢!

回答

4

不幸的是,Weblogic有一个不同的方法,它使用自己的URLStreamHandler,由于某种原因忽略认证。

尝试使用Sun的实现:中

代替

url = new URL(address); // use WebLogic handler 

使用

url = new URL(null, address, new sun.net.www.protocol.http.Handler()); // use Sun's handler 
2

我并没有尝试这种在WebLogic中,但在Tomcat,Glassfish的,阿帕奇Karaf的cliet调用的Web服务,这需要基本身份验证这工作好了:

/** 
* Proxy. 
*/ 
private OperationsService proxy; 

/** 
* Operations wrapper constructor. 
* 
* @throws SystemException if error occurs 
*/ 
public OperationsWrapper() 
     throws SystemException { 
    try { 
     final String username = getBundle().getString("wswrappers.operations.username"); 
     final String password = getBundle().getString("wswrappers.operations.password"); 
     Authenticator.setDefault(new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(
         username, 
         password.toCharArray()); 
      } 
     }); 
     URL url = new URL(getBundle().getString("wswrappers.operations.url")); 
     QName qname = new QName("http://hltech.lt/ws/operations", "Operations"); 
     Service service = Service.create(url, qname); 
     proxy = service.getPort(OperationsService.class); 
     Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext(); 
     requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString()); 
     requestContext.put(BindingProvider.USERNAME_PROPERTY, username); 
     requestContext.put(BindingProvider.PASSWORD_PROPERTY, password); 
     Map<String, List<String>> headers = new HashMap<String, List<String>>(); 
     headers.put("Timeout", Collections.singletonList(getBundle().getString("wswrappers.operations.timeout"))); 
     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); 
    } catch (Exception e) { 
     LOGGER.error("Error occurred in operations web service client initialization", e); 
     throw new SystemException("Error occurred in operations web service client initialization", e); 
    } 
} 

我不认为在WebLogic有不同的东西。这应该工作。

4

我有同样的问题在Weblogic的。 我可以确认你通过Paulius Matulionis提出的解决方案中的Weblogic 11为我工作:

1)在WebLogic脚本的setDomainEnv变化:

import my.MyPortType; 

import javax.xml.namespace.QName; 
import javax.xml.ws.BindingProvider; 
import javax.xml.ws.Service; 
import javax.xml.ws.handler.MessageContext; 
import java.net.Authenticator; 
import java.net.MalformedURLException; 
import java.net.PasswordAuthentication; 
import java.net.URL; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class MyWebServiceClient { 

    public static void main(String[] args) { 
     URL url = null; 
     try { 
      url = new URL("http://host:10001/mycontext/ws?WSDL"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     try { 
      final String username = "some"; 
      final String password = "other"; 
      Authenticator.setDefault(new Authenticator() { 
       @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(
          username, 
          password.toCharArray()); 
       } 
      }); 
      QName qname = new QName("http://whatevertheqname/", "whatevertheservicename"); 
      Service service = Service.create(url, qname); 
      MyPortType proxy = service.getPort(MyPortType.class); 
      Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext(); 
      requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString()); 
      requestContext.put(BindingProvider.USERNAME_PROPERTY, username); 
      requestContext.put(BindingProvider.PASSWORD_PROPERTY, password); 
      Map<String, List<String>> headers = new HashMap<String, List<String>>(); 
      headers.put("Timeout", Collections.singletonList("10000")); 
      requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); 
      String result = proxy.myMethod23"); 

      System.out.println("Result is: " + result); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new RuntimeException("Error occurred in operations web service client initialization", e); 
     } 


    } 
} 
0

,可以解决你的问题,以下是建议。 sh强制WEBLOGIC使用SunHttpHandler

2)呼叫从使用shell脚本/批处理文件中的命令提示的web服务。

3)试试这个URL对象在你的web服务new URL(null, "http://...", new sun.net.www.protocol.http.Handler());

4)写你自己的太阳HTTP处理程序。

5)尝试在它加入新创建的Web服务更新您的weblogic-webservice.xml。

6)尝试使用JAX-RPC而不是JAX-WS。

希望前两个中的任何一个都能解决您的问题,但其他选项也很有帮助。

相关问题