Web Services 使用具有NTLM身份验证的SOAP服务

sycxhyv7  于 2023-11-22  发布在  其他
关注(0)|答案(3)|浏览(172)

我尝试通过创建一个NTLM引擎(按照http://hc.apache.org/httpcomponents-client-4.3.x/ntlm.html上的说明)来使用一个带有NTLM身份验证的SOAP服务,该引擎实现了AuthSchemeFactory,并最终将AuthSchemeFactory注册到我的HTTP客户端。当我使用HTTP客户端访问该服务时,我得到一个响应“Status code - 415,Message - The server cannot service the request because the media type is unsupported”。
有人能告诉我如何解决这个问题不受支持的媒体消费一个受NTLM保护的SOAP Web服务在Java平台上.使用JCIFS是一个正确的选择来conmbloom受保护的服务或有任何更好的方法(S).提前感谢.

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

字符串

j5fpnvbx

j5fpnvbx1#

如果您使用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;
}

字符串

sqougxex

sqougxex2#

在Apache HttpClient 5的情况下,您应该使用HttpClient 5MessageClient

org.apache.hc.client5.http.auth.NTCredentials
org.springframework.ws.transport.http.HttpComponents5MessageSender

字符串
例如Kotlin

val httpComponentsMessageSender = HttpComponents5MessageSender()
httpComponentsMessageSender.setCredentials(NTCredentials(user,
            password.toCharArray(),
            workstation,
            credentials.domain))

unhi4e5o

unhi4e5o3#

示例python实现,使用NTFS Auth with FLASK。
如果你想使用java,运行下面的独立flask代码,并通过http请求从java代码调用url(例如POST请求/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)

字符串

相关问题