gpt4 book ai didi

java - 通过 WSO2ESB 访问 NTLM 安全 WS

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:17 24 4
gpt4 key购买 nike

大家好,我正在尝试在 WSO2ESB 上设置代理服务来访问 NTLMv2 安全的 WS。我创建了一个调解器类来实现此目的,但到目前为止还不太幸运,我不断收到 401 状态

这是代码。

代理服务:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="test"
transports="http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target endpoint="fincasEP">
<inSequence>
<class name="com.aig.mediator.NTLMAuthMediator">
<property name="port" value="remote-port"/>
<property name="username" value="username-credential"/>
<property name="host" value="remote-host-ip"/>
<property name="domain" value="remot-host-domain"/>
<property name="password" value="**********"/>
</class>
</inSequence>
</target>
<publishWSDL key="fincas-wsdl"/>
<description/>
</proxy>

中介类:

public class NTLMAuthMediator extends AbstractMediator {

private String domain;
private String host;
private String port;
private String username;
private String password;

public boolean mediate(MessageContext context) {
org.apache.axis2.context.MessageContext axis2MsgContext;


axis2MsgContext = ((Axis2MessageContext) context).getAxis2MessageContext();
String authString = (String)tmp.get("Authorization");

HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
setCredentials(auth);
List<String> authSchemes = new ArrayList<String>();
authSchemes.add(HttpTransportProperties.Authenticator.NTLM);
auth.setAuthSchemes(authSchemes);
auth.setPreemptiveAuthentication(true); // send authentication info at once
Options options = new Options();
options.setProperty(HTTPConstants.CHUNKED, "false");
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
options.setProperty(HTTPConstants.AUTHENTICATE, auth);

axis2MsgContext.setOptions(options);



return true;
}

private void setCredentials(Authenticator auth) {

boolean isDomain = this.domain != null ? true : this.domain.trim()
.length() > 0 ? true : false;
boolean isUsername = this.username != null ? true : this.username
.trim().length() > 0 ? true : false;
boolean isPassword = this.password != null ? true : this.password
.trim().length() > 0 ? true : false;
boolean isHost = this.host != null ? true
: this.host.trim().length() > 0 ? true : false;
boolean isPort = this.username != null ? true : this.username.trim()
.length() > 0 ? true : false;

if (!isDomain) {
throw new RuntimeException("Domain parameter must NOT be null");
}
if (!isUsername) {
throw new RuntimeException("Username parameter must NOT be null");
}
if (!isPassword) {
throw new RuntimeException("Password parameter must NOT be null");
}
if (!isHost) {
throw new RuntimeException("Host parameter must NOT be null");
}
if (!isPort) {
throw new RuntimeException("Port parameter must NOT be null");
}

auth.setUsername(this.username);
auth.setPassword(this.password);
auth.setDomain(this.domain);
auth.setRealm(AuthScope.ANY_REALM);
auth.setHost(this.host);
auth.setPort(Integer.valueOf(this.port));
auth.setPreemptiveAuthentication(true);
}

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

我正在使用 wso2esb 最新版本。

考虑到 NTLM 是一种旧机制,WSO2 没有提供此案例的文档,这确实令人沮丧。

如有任何建议,我们将不胜感激

顺便说一句,错误是:

401 - Unauthorized: Access is denied due to invalid credentials.

最佳答案

事实是,我终于可以使用 ESB Mule 解决这个问题,尽管如此,我将解释我如何尝试使用 WSO2ESB 以及最后的 MULE 解决它...

我正在查看 NTLM 关于 httpclient 的情况,在几个站点之后我注意到 httpclient 3.x 不支持这种类型的机制,这是因为它使用了 NTLMSchema。

我找到了这个 git 仓库 https://github.com/DovAmir/httpclientAuthHelper这家伙写了一个与 httpclient 3.x 一起使用的 NTLMcuston shema 类,做得很好,我克隆了这个 repo 生成了 jar 等,然后我修改了以下类

org.apache.axis2.transport.http.AbstractHTTPSender
...
...
...
protected void setAuthenticationInfo(HttpClient agent, MessageContext msgCtx, HostConfiguration config)
throws AxisFault, UnknownHostException
{
String localhost = InetAddress.getLocalHost().getHostName().toUpperCase();
...
...
if (domain != null) {

creds = new NTCredentials(username, password, localhost, domain);
} else {
creds = new UsernamePasswordCredentials(username, password);
}
tmpHttpState.setCredentials(new AuthScope(host, port, realm), creds);
}
...

然后编写了一个测试用例以确保 axis2 ServerClient 确实可以工作..并且确实如此。但是...我想我不太了解 PassThroughHttpSender 的机制。显然还有其他事情要做,要让它发挥作用,我真的没有时间,然后我开始考虑其他事情,然后我意识到我们还有一个正在运行的 ESB Mule 3.4.0 CE 实例...

我只需修改该类

HttpConnector
{
...
...
// Properties added to enable NTLMv2 Auth

private String ntlmUser;
private String ntlmPassword;
private String ntlmDomain;
private String ntlmHost;
private String ntlmPort;
private boolean ntlmAuthentication;

//getters and setters

protected HttpClient doClientConnect() throws Exception
{
HttpState state = new HttpState();
HttpClient client = new HttpClient();
String localhost = InetAddress.getLocalHost().getHostName();
//TODO setting domain as well.
Credentials credentials;

if (getProxyUsername() != null || getNtlmUser() != null)
{
if (isProxyNtlmAuthentication())
{
credentials = new NTCredentials(getProxyUsername(), getProxyPassword(), localhost, "");
AuthScope authscope = new AuthScope(getProxyHostname(), getProxyPort());
state.setProxyCredentials(authscope, credentials);
}
else if(isNtlmAuthentication()){
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, CustomNTLM2Scheme.class);
AuthScope authscope = new AuthScope(getNtlmHost(), Integer.valueOf(getNtlmPort()));
credentials = new NTCredentials(getNtlmUser(), getNtlmPassword(), localhost, getNtlmDomain());
state.setCredentials(authscope, credentials);
}
else
{
credentials = new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword());
AuthScope authscope = new AuthScope(getProxyHostname(), getProxyPort());
state.setProxyCredentials(authscope, credentials);
}

}
client.setState(state);
client.setHttpConnectionManager(getClientConnectionManager());
return client;
}

流程是:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pattern="http://www.mulesoft.org/schema/mule/pattern"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/pattern http://www.mulesoft.org/schema/mule/pattern/current/mule-pattern.xsd">


<http:connector name="ntlmconn"
doc:name="HTTP-HTTPS">
<spring:property name="ntlmAuthentication" value="${ntlm.auth}"/>
<spring:property name="ntlmUser" value="${ntlm.username}"/>
<spring:property name="ntlmPassword" value="${ntlm.password}"/>
<spring:property name="ntlmHost" value="${ntlm.host}"/>
<spring:property name="ntlmPort" value="${ntlm.port}"/>
<spring:property name="ntlmDomain" value="${ntlm.domain}"/>
</http:connector>


<pattern:web-service-proxy name="fincas-service"
wsdlFile="${fincas.wsdl}">
<http:inbound-endpoint address="http://localhost:8080/fincas" />
<http:outbound-endpoint address="${endpoint}" connector-ref="ntlmconn"
exchange-pattern="request-response"></http:outbound-endpoint>
</pattern:web-service-proxy>

最后,通过这个补丁,我可以让它工作了,我将 WS 部署在 ESB 上,并且由于服务已启动并运行,我可以花更多时间尝试寻找 WSO2ESB 的解决方案。

我希望它也适合你。

关于java - 通过 WSO2ESB 访问 NTLM 安全 WS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com