有几个组件可以正常工作。很难将所有内容都写在一个地方,因此我将尝试在此处提供端到端的概述。
我首先必须在我的 WSO2 ESB 顺序中使用类中介来处理发送和 NTLM 身份验证。类中介器引用一个自定义类,该类从中介流中获取消息上下文(称为 Synapse 消息上下文)并提取 SOAP 信封。然后我将 Synapse SOAP 信封加载到 Axis2 消息上下文对象中。然后,我使用 Axis2 客户端和消息上下文将经过身份验证的请求提交到服务器。通过Axis2对NTLM的认证来自于JCIFS_NTLMScheme类,可以参考here。
注意:您必须使用该类中的日志记录配置才能使其与 WSO2 一起使用。我刚刚删除了“org.sac.crosspather.common.util*”库并更改了我看到的任何日志记录以使用 Apache Commons 日志记录功能
在 WSO2 Developer Studio 中创建自定义中介项目
在 Developer Studio 中创建一个新项目。在项目资源管理器中右键单击项目节点,然后选择“新建 > 中介项目”。
这将为您生成一些样板代码 - 即,一个扩展 AbstractMediator 并实现 Synapse 在执行逻辑时将调用的“mediate()”方法的类在您的序列中定义。
public class NTLMAuthorisation extends AbstractMediator {
public boolean mediate(MessageContext context){
//Mediation Logic
return true;
}
}
向用户公开一些变量/属性
类中介器查找可公开访问的变量,并在 WSO2 配置中公开它们。在您创建一个可重用的调解器之前,这很有帮助,该调解器可使其自身适应 WSO2 Carbon Web UI 中定义的属性或值。这里我们需要暴露七个变量:soapAction、SoapEndpoint、域、主机、端口、用户名和密码。通过定义实例变量及其访问器和修改器来公开变量。
这对于使用 WSO2 Secure Vault 来存储您的 NTLM 密码并从具有属性的系统注册表中获取其他配置非常有用。
public class NTLMAuthorisation extends AbstractMediator {
private String soapAction;
private String soapEndpoint;
private String domain;
private String host;
private int port;
private String username;
private String password;
public boolean mediate(MessageContext context) {
//Mediation Logic
return true;
}
public void setSoapAction(String _soapAction){
soapAction = _soapAction;
}
public String getSoapAction(){
return soapAction;
}
public void setSoapEndpoint(String _soapEndpoint){
soapEndpoint = _soapEndpoint;
}
public String getSoapEndpoint(){
return soapEndpoint;
}
public void setDomain(String _domain){
domain = _domain;
}
public String getDomain(){
return domain;
}
public void setHost(String _host){
host = _host;
}
public String getHost(){
return host;
}
public void setPort(int _port){
port = _port;
}
public int getPort(){
return port;
}
public void setUsername(String _username){
username = _username;
}
public String getUsername(){
return username;
}
public void setPassword(String _password){
password = _password;
}
public String getPassword(){
return password;
}
}
自定义中介逻辑
确保您从 here 创建了一个 JCIFS_NTLMScheme 类,并将 org.samba.jcifs 依赖项添加到您的 Maven 依赖项中,如下所示:
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
现在您可以在自定义中介类中使用以下中介方法:
public boolean mediate(MessageContext context) {
//Build NTLM Authentication Scheme
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, JCIFS_NTLMScheme.class);
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setUsername(username);
auth.setPassword(password);
auth.setDomain(domain);
auth.setHost(host);
auth.setPort(port);
ArrayList<String> authPrefs = new ArrayList<String>();
authPrefs.add(AuthPolicy.NTLM);
auth.setAuthSchemes(authPrefs);
//Force Authentication - failures will get caught in the catch block
try {
//Build ServiceClient and set Authorization Options
ServiceClient serviceClient = new ServiceClient();
Options options = new Options();
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setTo(new EndpointReference(soapEndpoint));
options.setAction(soapAction);
serviceClient.setOptions(options);
//Generate an OperationClient from the ServiceClient to execute the request
OperationClient opClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
//Have to translate MsgCtx from Synapse to Axis2
org.apache.axis2.context.MessageContext axisMsgCtx = new org.apache.axis2.context.MessageContext();
axisMsgCtx.setEnvelope(context.getEnvelope());
opClient.addMessageContext(axisMsgCtx);
//Send the request to the server
opClient.execute(true);
//Retrieve Result and replace mediation (synapse) context
SOAPEnvelope result = opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE).getEnvelope();
context.setEnvelope(result);
} catch (AxisFault e) {
context.setProperty("ResponseCode", e.getFaultCodeElement().getText());
return false; //This stops the mediation flow, so I think it executes the fault sequence?
}
return true;
}
打包为 OSGi Bundle 并部署到服务器
在这个阶段,您应该能够在 WSO2 Developer Studio 的 项目浏览器 中并从上下文菜单中选择 Export Project as Deployable Archive 中的自定义中介项目。按照提示将 JAR 文件保存在系统的某个位置。生成 JAR 文件后,找到它并将其传输到 [ESB_HOME]/repository/components/dropins 目录。您可能需要重新启动服务器才能检测到新的外部库。
使用自定义中介
在您的序列中,您现在应该能够添加class mediator 并使用包名和类名一起引用您的自定义类,例如:org.strainy.ntlmauthorisation。。 p>