【问题标题】:Java JBoss 401 Error on SharePoint 2010 Web ServiceSharePoint 2010 Web 服务上的 Java JBoss 401 错误
【发布时间】:2015-01-20 20:40:23
【问题描述】:

我的代码在 Eclipse IDE 中测试时成功运行。

我正在使用生成的 Copy.wsdl 通过 Web 服务连接到 MS SharePoint 2010

当我在 JBoss 服务器(运行 Adob​​e LifeCycle)上部署我的代码时,我的代码收到 401 错误。

错误:

Caused by: org.jboss.ws.WSException: Invalid HTTP server response [401] - Unauthorized
at org.jboss.ws.core.soap.SOAPMessageUnMarshallerHTTP.read(SOAPMessageUnMarshallerHTTP.java:75)
at org.jboss.remoting.transport.http.HTTPClientInvoker.readResponse(HTTPClientInvoker.java:608)
at org.jboss.remoting.transport.http.HTTPClientInvoker.useHttpURLConnection(HTTPClientInvoker.java:402)
at org.jboss.remoting.transport.http.HTTPClientInvoker.makeInvocation(HTTPClientInvoker.java:253)
... 156 more

现在,如果我通过 IDE 故意使用错误的登录名,我会收到此错误:

com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized

更新:

因此,经过更多研究,结果证明 J2EE 支持,以及缺乏 NTLM 是原因。到目前为止,我已经尝试了几种解决方案均无济于事。

代码:

protected void initialize(String username, String password) throws Exception {
    System.out.println("initialize()...");
    java.net.CookieManager cm = new java.net.CookieManager();
    java.net.CookieHandler.setDefault(cm);
    Authenticator.setDefault(new SharepointAuthenticator(username, password));
}

验证器

public class SharepointAuthenticator extends Authenticator {

private String username = "";
private String password = "";

public SharepointAuthenticator(String username, String password) {
    this.username = username;
    this.password = password;
    System.out.println("Initializing Authentication");
}

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

获取 SOAP

protected CopySoap getCopySoap(String username, String password, String wsdl, String endpoint) throws Exception {
    System.out.println("Creating a CopySoap instance...");
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    Copy service = new Copy(new URL(wsdl), new QName("http://schemas.microsoft.com/sharepoint/soap/", "Copy"));
    System.out.println("CopySoap 2");

    CopySoap copySoap = service.getCopySoap();

    System.out.println(endpoint + "\n" + wsdl);

    BindingProvider bp = (BindingProvider) copySoap;  
    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
    return copySoap;
}

调用上传文件:

    // make the call to upload
    port.copyIntoItems("null", destinationUrlCollection, metadata, byteArray, longHolder, resultHolder);

【问题讨论】:

    标签: java web-services jboss sharepoint-2010 http-status-code-401


    【解决方案1】:

    我正在使用CXF 框架来创建端口。它将处理所有类型的身份验证。它也在 JBoss 中工作。

    实现起来很简单。添加CXF依赖,使用如下代码创建端口。

    示例代码:

    InvocationHandlerImpl.java

    public class InvocationHandlerImpl implements InvocationHandler
    {
        CopySoap port;
    
        public InvocationHandlerImpl(CopySoap copySoap) {
            port = copySoap;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return method.invoke(port, args);
        }
    
    }
    

    TrustingX509TrustManager.java

    public class TrustingX509TrustManager implements X509TrustManager
    {
        /**
         * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
         */
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            // return null will let jsse accept all certificates!
            return null;
        }
    
        /**
         * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
         */
        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }
    
        /**
         * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
         */
        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    }
    

    SPCopyDriver.java

    public class SPCopyDriver
    {
    
        public static void main(String[] args) {
    
            JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
            proxyFactory.setServiceClass(CopySoap.class);
            proxyFactory.setUsername("domain\\username");
            proxyFactory.setPassword("password");
            proxyFactory.setAddress("https://<<IP>>/_vti_bin/Copy.asmx");
    
            HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
            httpClientPolicy.setAutoRedirect(true);
            httpClientPolicy.setAllowChunking(false);
    
            Object port = proxyFactory.create();
            Client client = ClientProxy.getClient(port);
            HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
            httpConduit.setClient(httpClientPolicy);
    
            TLSClientParameters tls = new TLSClientParameters();
            tls.setDisableCNCheck(true);
            tls.setTrustManagers(new TrustManager[] { new TrustingX509TrustManager() });
            httpConduit.setTlsClientParameters(tls);
    
            port = Proxy.newProxyInstance(SPCopyDriver.class.getClassLoader(), new Class[] { CopySoap.class }, new InvocationHandlerImpl((CopySoap) port));
    
            CopySoap copySoap = (CopySoap) port;
    
        }
    
    }
    

    【讨论】:

    • 很抱歉接受晚了。这看起来很好而且很有希望。目前正在处理其他项目,因为这个特定的项目被搁置了。稍后将更新状态。再次感谢。
    • 我实现了你的建议。我让它在我的 IDE 中工作,但是当部署到运行 Adob​​e LiveCycle 的 Jboss 中时,我收到以下错误:NullPointerException @ org.apache.cxf.jaxb.Util.getFieldXJTS @ Line 269
    • 对不起,我以前没有看到过这个异常。它对我来说很好。
    • 是的,谷歌也一无所获。我现在采取另一条路线将 LiveCycle 排除在外以避免进一步的麻烦
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多