一:RsaUtils,加密解密工具
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
public class RsaUtils {
/* byte数组转换成十六进制输出:"abcd" => "61626364" */
public static String bytesToHexString(byte[] bArr) {
StringBuffer sb = new StringBuffer(bArr.length);
String sTmp;
for (int i = 0; i < bArr.length; i++) {
sTmp = Integer.toHexString(0xFF & bArr[i]);
if (sTmp.length() < 2)
sb.append(0);
sb.append(sTmp.toUpperCase());
}
return sb.toString();
}
public static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
public static byte[] encrypt(byte[] content, String publicKeyStr) throws Exception {
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//javaĬ "RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
}
二:对参数进行加密,appId,secret,publicKey,接口路径等参数让对接方提供
String appIdRsa = RsaUtils.bytesToHexString(RsaUtils.encrypt(appId.getBytes(), publicKey));
String secretRsa = RsaUtils.bytesToHexString(RsaUtils.encrypt(secret.getBytes(), publicKey));
三:拼接通信xml
String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <tem:GetToken>\n" +
" <!--Optional:-->\n" +
" <tem:appid>" + appIdRsa + "</tem:appid>\n" +
" <!--Optional:-->\n" +
" <tem:secret>" + secretRsa + "</tem:secret>\n" +
" </tem:GetToken>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
四:设置通信url,以及解析返回的xml
public static Map<String, Object> tokenSoapPostXml(String soapRequestData, String uploadDataIP) {
Map<String, Object> resultMap = new HashMap<>();
StringBuilder result = new StringBuilder();
try {
//SoapAction路径,如http://tempuri.org/IToken/GetToken
String SOAPAction = UrlConfig.smartCityTokenSoapAction;
//接口路径
URL url = new URL(uploadDataIP + UrlConfig.smartCityConstructionPort + UrlConfig.smartCityTokenUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
// how big it is so that we can set the HTTP Cotent-Length
// property. (See complete e-mail below for more on this.)
// byte[] b = bout.toByteArray();
byte[] b = soapRequestData.getBytes("utf-8");
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", SOAPAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
// Read the response and write it to standard out.
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
result.append(inputLine);
in.close();
} catch (MalformedURLException e) {
logger.error(e.toString());
} catch (IOException e) {
logger.error(e.toString());
}
System.out.println("结果值:" + result.toString());
String deptXML = result.toString();
try {
Document document = XmlUtils.strXmlToDocument(deptXML);
String response = XmlUtils.getValueByElementName(document, "GetTokenResult");
response = response.replaceAll("\r", "");
resultMap = GsonUtil.gsonToMaps(response);
} catch (Exception e) {
logger.error(e.toString());
}
return resultMap;
}
五:第四步中xml解析工具XmlUtils,以及所需要的依赖
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XmlUtils {
/**
* 将字符串类型的XML 转化成Docunent文档结构
*
* @param parseStrXml 待转换的XML字符串
* @return
*/
public static Document strXmlToDocument(String parseStrXml) {
StringReader read = new StringReader(parseStrXml);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
// 新建立构造器
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try {
doc = sb.build(source);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
/**
* 根据目标节点名获取值
*
* @param doc 文档结构
* @param finalNodeName 节点名
* @return
*/
public static String getValueByElementName(Document doc, String finalNodeName) {
Element root = doc.getRootElement();
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> resultmap = getChildAllText(doc, root, map);
String result = (String) resultmap.get(finalNodeName);
return result;
}
/**
* 递归获得子节点的值
*
* @param doc 文档结构
* @param e 节点元素
* @param resultmap 递归将值存入map中
* @return
*/
public static Map<String, Object> getChildAllText(Document doc, Element e, Map<String, Object> resultmap) {
if (e != null) {
//如果存在子节点
if (e.getChildren() != null) {
List<Element> list = e.getChildren();
//循环输出
for (Element el : list) {
//如果子节点还存在子节点,则递归获取
if (el.getChildren().size() > 0) {
getChildAllText(doc, el, resultmap);
} else {
//将叶子节点值压入map
resultmap.put(el.getName(), el.getTextTrim());
}
}
}
}
return resultmap;
}
}