Cordys Java 后台模拟登入 且获取 WebService数据

本文介绍了一个使用SOAP协议进行SAML身份验证的过程。通过Java客户端向指定的服务端发送带有用户名和密码的SOAP请求,并从响应中解析SAML断言。接着利用获取到的SAML断言向目标服务发起新的SOAP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class HttpClientSoap {

	private static final String soapRequestOfSaml = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"
		+ "	 	<SOAP:Header>"
		+ "      	<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
		+ "          	<wsse:UsernameToken xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
		+ "              	<wsse:Username>201613</wsse:Username>"
		+ "              	<wsse:Password>201613</wsse:Password>"
		+ "          	</wsse:UsernameToken>"
		+ "      	</wsse:Security>"
		+ "  	</SOAP:Header>"
		+ "  	<SOAP:Body>"
		+ "      	<samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" MinorVersion=\"1\">"
		+ "          	<samlp:AuthenticationQuery>"
		+ "              	<saml:Subject xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\">"
		+ "                  	<saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\">201613</saml:NameIdentifier>"
		+ "              	</saml:Subject>"
		+ "          	</samlp:AuthenticationQuery>"
		+ "      	</samlp:Request>"
		+ "		</SOAP:Body>"
		+ "	</SOAP:Envelope>";
	
	private static final String soapRequestOfTargetService =""
					+"<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"
					+"<SOAP:Body>"
					+"<GetDepartmentByParentID xmlns=\"http://schemas.cordys.com/CommonPackage\" preserveSpace=\"no\" qAccess=\"0\" qValues=\"\">"
					+"<ParentID>29</ParentID>"
					+"</GetDepartmentByParentID>"
					+"</SOAP:Body>"
					+"</SOAP:Envelope>";

	public static void Test() {
		
		try {
			
			System.out.println(soapRequestOfSaml);
			PostMethod postmethod = new PostMethod("http://192.168.100.122/comac/com.eibus.web.soap.Gateway.wcp");
			byte[] b = soapRequestOfSaml.getBytes("UTF-8");
			InputStream is = new ByteArrayInputStream(b, 0, b.length);
			RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
			postmethod.setRequestEntity(re);
			HttpClient httpClient = new HttpClient();
			int statusCode = httpClient.executeMethod(postmethod);
			System.err.println("statuscode = " + statusCode);
			String soapResponseData = postmethod.getResponseBodyAsString();
			System.out.println(soapResponseData);
			
			
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document dom = builder.parse(postmethod.getResponseBodyAsStream());
			Node samlArtifact = XPathAPI.selectSingleNode(dom, "//*[local-name()='AssertionArtifact']");
			System.err.println("SamlArtifact = " + samlArtifact.getTextContent());
			//System.out.println(samlArtifact.getAttributes().getNamedItem("xmlns:samlp").getNodeValue());
			

			System.out.println(soapRequestOfTargetService);
			postmethod = new PostMethod("http://192.168.100.122/comac/com.eibus.web.soap.Gateway.wcp?SAMLart=" + samlArtifact.getTextContent());
			b = soapRequestOfTargetService.getBytes("UTF-8");
			is = new ByteArrayInputStream(b, 0, b.length);
			re = new InputStreamRequestEntity(is, b.length, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
			postmethod.setRequestEntity(re);
			//httpClient = new HttpClient();
			statusCode = httpClient.executeMethod(postmethod);
			System.err.println("statuscode = " + statusCode);
			soapResponseData = postmethod.getResponseBodyAsString();
			System.out.println(soapResponseData);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值