Consuming soap service with NTLM Authentication

佐手、 提交于 2021-01-27 09:06:37

问题


I am trying to consume a SOAP service with NTLM authentication by creating a NTLM engine (following instructions on http://hc.apache.org/httpcomponents-client-4.3.x/ntlm.html ) implemented AuthSchemeFactory and finally registered the AuthSchemeFactory to my HTTP Client. When I hit the service using my HTTP Client I get a reponse that "Status code - 415 , Message - The server cannot service the request because the media type is unsupported."

Can anybody tell how can I fix this issue of unsupported media to consume a NTLM-protected SOAP web service on Java platform. Is using JCIFS a correct option to conmsume NTLM protected service or are there any better approach(s). Thanks in advance.

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getAuthSchemes().register(AuthSchemes.NTLM,
            new JCIFSNTLMSchemeFactory());

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    NTCredentials ntcred = new NTCredentials("USERNAME", "PASSWORD",
            "HOST", "DOMAIN");

    credsProvider.setCredentials(new AuthScope("HOST", 443,
            AuthScope.ANY_REALM, "NTLM"), ntcred);

    httpclient.setCredentialsProvider(credsProvider);

    httpclient.getParams().setParameter(
            CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

    Writer writer = new StringWriter();
        writer.write("MY SOAP REQUEST BODY");

    HttpPost httppost = new HttpPost(
            "https://<HOST_NAME>/XiPay30WS.asmx");
    httppost.setEntity(new StringEntity(writer.toString()));

    httppost.setHeader("Content-Type",
                "application/x-www-form-urlencoded");

    HttpResponse httpresponse = httpclient.execute(
                new HttpHost("HOST", 443, "https"),
                httppost, new BasicHttpContext());

    String statusCode = httpresponse.getStatusCode();

回答1:


Sample python implementation with NTLM Auth with FLASK.

If you want to use with java , run the standalone flask code below and call the url (e.g POST request /dora/httpWithNTLM ) from java code by http request

from flask import  Flask, render_template, flash, request, url_for, redirect, session , Response
import requests,sys,json
from requests_ntlm import HttpNtlmAuth
app = Flask(__name__)




@app.route("/dora/httpWithNTLM",methods=['POST'])
def invokeHTTPReqWithNTLM():
    url =""
    reqData = json.loads(request.data)        
    reqxml=request.data
    headers = {}
    headers["SOAPAction"] = "";
    headers["Content-Type"] = "text/xml"
    headers["Accept"] = "text/xml"
    print("req headers "+str(request.headers))

    r = requests.Request("POST",url,auth=HttpNtlmAuth('domain\\username','password'), data=reqxml, headers=headers) 

    prepared = r.prepare()
    s = requests.Session()
    resp = s.send(prepared)
    print (resp.status_code)
    return Response(resp.text.replace("&lt;","<").replace("&gt;",">"),resp.status_code)




if __name__ == '__main__':
    app.run(host="0.0.0.0",port=5001)



回答2:


If you use Spring WS support:

Check this Solution http://dolszewski.com/spring/sharepoint-web-services-spring-and-ntlm-authentication/

@Bean("navisionMessageSender")
public HttpComponentsMessageSender httpComponentsMessageSender() {
    HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
    String user = env.getProperty("navision.endpoint.user");
    String password = env.getProperty("navision.endpoint.password");
    String domain = env.getProperty("navision.endpoint.domain");
    NTCredentials credentials = new NTCredentials(user, String.valueOf(password), null, domain);
    httpComponentsMessageSender.setCredentials(credentials);
    return httpComponentsMessageSender;
}


来源:https://stackoverflow.com/questions/37660995/consuming-soap-service-with-ntlm-authentication

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