使用NTLM身份验证使用soap服务

时间:2016-06-06 15:15:44

标签: java web-services soap axis ntlm

我试图通过创建NTLM引擎(遵循http://hc.apache.org/httpcomponents-client-4.3.x/ntlm.html上的说明)实现AuthSchemeFactory并最终将AuthSchemeFactory注册到我的HTTP客户端来使用具有NTLM身份验证的SOAP服务。当我使用我的HTTP客户端点击服务时,我得到一个响应“状态代码 - 415,消息 - 服务器无法为请求提供服务,因为媒体类型不受支持。”

有人可以告诉我如何解决这个不受支持的媒体问题,以便在Java平台上使用受NTLM保护的SOAP Web服务。使用JCIFS是一个正确的选项来约束NTLM受保护的服务,还是有更好的方法。提前致谢。

    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();

2 个答案:

答案 0 :(得分:0)

使用带有FLASK的NTLM Auth实现python实现。

如果你想使用java,请运行下面的独立烧瓶代码并通过http请求从java代码调用url(例如POST request / dora / httpWithNTLM)

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)

答案 1 :(得分:0)

如果您使用Spring WS支持:

检查此解决方案 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;
}