Java JBoss 401 Error on SharePoint 2010 Web Service

霸气de小男生 提交于 2019-12-04 04:22:45

I am using CXF framework to create port. It will take care of all type of authentication. It is working in JBoss also.

It is simple to implement. Add CXF dependencies and use the following code to create port.

Sample Code :

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;

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!