昨天看了支付寶的登錄接口代碼,覺得有些東西還是對以后的開發有幫助的。下面就記錄自己的感想。
首先是AlipayCore.java這個類,該類是請求、通知返回兩個文件所調用的公用函數核心處理文件,不需要修改。方法主要是對簽名和請求參數進行拼接:
/**
* 生成簽名結果
* @param sArray 要簽名的數組
* @return 簽名結果字符串
*/
public static String buildMysign(Map<String, String> sArray) {
String prestr = createLinkString(sArray); //把數組所有元素,按照“參數=參數值”的模式用“&”字符拼接成字符串
prestrprestr = prestr + AlipayConfig.key; //把拼接后的字符串再與安全校驗碼直接連接起來
String mysign = AlipayMd5Encrypt.md5(prestr);
return mysign;
}
/**
* 除去數組中的空值和簽名參數
* @param sArray 簽名參數組
* @return 去掉空值與簽名參數后的新簽名參數組
*/
public static Map<String, String> paraFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<String, String>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
|| key.equalsIgnoreCase("sign_type")) {
continue;
}
result.put(key, value);
}
return result;
}
/**
* 把數組所有元素排序,並按照“參數=參數值”的模式用“&”字符拼接成字符串
* @param params 需要排序並參與字符拼接的參數組
* @return 拼接后字符串
*/
public static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {//拼接時,不包括最后一個&字符
prestrprestr = prestr + key + "=" + value;
} else {
prestrprestr = prestr + key + "=" + value + "&";
}
}
return prestr;
}
AlipayConfig.java這個類是一個基礎配置類,主要用來設置賬戶有關信息和返回路徑:
//↓↓↓↓↓↓↓↓↓↓請在這里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 合作身份者ID,以2088開頭由16位純數字組成的字符串
public static String partner = "";
// 交易安全檢驗碼,由數字和字母組成的32位字符串
public static String key = "";
// 當前頁面跳轉后的頁面 要用 http://格式的完整路徑,不允許加?id=123這類自定義參數
// 域名不能寫成http://localhost/alipay.auth.authorize_jsp_utf8/return_url.jsp ,否則會導致return_url執行無效
public static String return_url = "http://127.0.0.1:8080/alipay.auth.authorize_jsp_utf8/return_url.jsp";
//↑↑↑↑↑↑↑↑↑↑請在這里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
// 調試用,創建TXT日志路徑
public static String log_path = "D:\\alipay_log_" + System.currentTimeMillis()+".txt";
// 字符編碼格式 目前支持 gbk 或 utf-8
public static String input_charset = "UTF-8";
// 簽名方式 不需修改
public static String sign_type = "MD5";
//訪問模式,根據自己的服務器是否支持ssl訪問,若支持請選擇https;若不支持請選擇http
public static String transport = "http";
/**
* 對字符串進行MD5簽名
*
* @param text
* 明文
*
* @return 密文
*/
public static String md5(String text) {
return DigestUtils.md5Hex(getContentBytes(text, AlipayConfig.input_charset));
}
/**
* @param content
* @param charset
* @return
* @throws SignatureException
* @throws UnsupportedEncodingException
*/
private static byte[] getContentBytes(String content, String charset) {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5簽名過程中出現錯誤,指定的編碼集不對,您目前指定的編碼集是:" + charset);
}
}
/**
* HTTPS形式消息驗證地址
*/
private static final String HTTPS_VERIFY_URL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify&";
/**
* HTTP形式消息驗證地址
*/
private static final String HTTP_VERIFY_URL = "http://notify.alipay.com/trade/notify_query.do?";
/**
* 驗證消息是否是支付寶發出的合法消息
* @param params 通知返回來的參數數組
* @return 驗證結果
*/
public static boolean verify(Map<String, String> params) {
String mysign = getMysign(params);
String responseTxt = "true";
if(params.get("notify_id") != null) {responseTxt = verifyResponse(params.get("notify_id"));}
String sign = "";
if(params.get("sign") != null) {sign = params.get("sign");}
//寫日志記錄(若要調試,請取消下面兩行注釋)
//String sWord = "responseTxt=" + responseTxt + "\n notify_url_log:sign=" + sign + "&mysign="
// + mysign + "\n notify回來的參數:" + AlipayCore.createLinkString(params);
//AlipayCore.logResult(sWord);
//驗證
//responsetTxt的結果不是true,與服務器設置問題、合作身份者ID、notify_id一分鍾失效有關
//mysign與sign不等,與安全校驗碼、請求時的參數格式(如:帶自定義參數等)、編碼格式有關
if (mysign.equals(sign) && responseTxt.equals("true")) {
return true;
} else {
return false;
}
}
/**
* 根據反饋回來的信息,生成簽名結果
* @param Params 通知返回來的參數數組
* @return 生成的簽名結果
*/
private static String getMysign(Map<String, String> Params) {
Map<String, String> sParaNew = AlipayCore.paraFilter(Params);//過濾空值、sign與sign_type參數
String mysign = AlipayCore.buildMysign(sParaNew);//獲得簽名結果
return mysign;
}
/**
* 獲取遠程服務器ATN結果,驗證返回URL
* @param notify_id 通知校驗ID
* @return 服務器ATN結果
* 驗證結果集:
* invalid命令參數不對 出現這個錯誤,請檢測返回處理中partner和key是否為空
* true 返回正確信息
* false 請檢查防火牆或者是服務器阻止端口問題以及驗證時間是否超過一分鍾
*/
private static String verifyResponse(String notify_id) {
//獲取遠程服務器ATN結果,驗證是否是支付寶服務器發來的請求
String transport = AlipayConfig.transport;
String partner = AlipayConfig.partner;
String veryfy_url = "";
if (transport.equalsIgnoreCase("https")) {
veryfy_url = HTTPS_VERIFY_URL;
} else {
veryfy_url = HTTP_VERIFY_URL;
}
veryfy_urlveryfy_url = veryfy_url + "partner=" + partner + "¬ify_id=" + notify_id;
return checkUrl(veryfy_url);
}
/**
* 獲取遠程服務器ATN結果
* @param urlvalue 指定URL路徑地址
* @return 服務器ATN結果
* 驗證結果集:
* invalid命令參數不對 出現這個錯誤,請檢測返回處理中partner和key是否為空
* true 返回正確信息
* false 請檢查防火牆或者是服務器阻止端口問題以及驗證時間是否超過一分鍾
*/
private static String checkUrl(String urlvalue) {
String inputLine = "";
try {
URL url = new URL(urlvalue);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
ininputLine = in.readLine().toString();
} catch (Exception e) {
e.printStackTrace();
inputLine = "";
}
return inputLine;
}
/**
* 生成要請求給支付寶的參數數組
* @param sParaTemp 請求前的參數數組
* @return 要請求的參數數組
*/
private static Map<String, String> buildRequestPara(Map<String, String> sParaTemp) {
//除去數組中的空值和簽名參數
Map<String, String> sPara = AlipayCore.paraFilter(sParaTemp);
//生成簽名結果
String mysign = AlipayCore.buildMysign(sPara);
//簽名結果與簽名方式加入請求提交參數組中
sPara.put("sign", mysign);
sPara.put("sign_type", AlipayConfig.sign_type);
return sPara;
}
/**
* 構造提交表單HTML數據
* @param sParaTemp 請求參數數組
* @param gateway 網關地址
* @param strMethod 提交方式。兩個值可選:post、get
* @param strButtonName 確認按鈕顯示文字
* @return 提交表單HTML文本
*/
public static String buildForm(Map<String, String> sParaTemp, String gateway, String strMethod,
String strButtonName) {
//待請求參數數組
Map<String, String> sPara = buildRequestPara(sParaTemp);
List<String> keys = new ArrayList<String>(sPara.keySet());
StringBuffer sbHtml = new StringBuffer();
sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + gateway
+ "_input_charset=" + AlipayConfig.input_charset + "\" method=\"" + strMethod
+ "\">");
for (int i = 0; i < keys.size(); i++) {
String name = (String) keys.get(i);
String value = (String) sPara.get(name);
sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
}
//submit按鈕控件請不要含有name屬性
sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");
sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");
return sbHtml.toString();
}
public static String sendPostInfo(Map<String, String> sParaTemp, String gateway)
throws Exception {
//待請求參數數組
Map<String, String> sPara = buildRequestPara(sParaTemp);
HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();
HttpRequest request = new HttpRequest(HttpResultType.BYTES);
//設置編碼集
request.setCharset(AlipayConfig.input_charset);
request.setParameters(generatNameValuePair(sPara));
request.setUrl(gateway+"_input_charset="+AlipayConfig.input_charset);
HttpResponse response = httpProtocolHandler.execute(request);
if (response == null) {
return null;
}
String strResult = response.getStringResult();
return strResult;
}
/**
* MAP類型數組轉換成NameValuePair類型
* @param properties MAP類型數組
* @return NameValuePair類型數組
*/
private static NameValuePair[] generatNameValuePair(Map<String, String> properties) {
NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
int i = 0;
for (Map.Entry<String, String> entry : properties.entrySet()) {
nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue());
}
return nameValuePair;
}
public HttpResponse execute(HttpRequest request) {
HttpClient httpclient = new HttpClient(connectionManager);
// 設置連接超時
int connectionTimeout = defaultConnectionTimeout;
if (request.getConnectionTimeout() > 0) {
connectionTimeout = request.getConnectionTimeout();
}
httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
// 設置回應超時
int soTimeout = defaultSoTimeout;
if (request.getTimeout() > 0) {
soTimeout = request.getTimeout();
}
httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
// 設置等待ConnectionManager釋放connection的時間
httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout);
String charset = request.getCharset();
charsetcharset = charset == null ? DEFAULT_CHARSET : charset;
HttpMethod method = null;
if (request.getMethod().equals(HttpRequest.METHOD_GET)) {
method = new GetMethod(request.getUrl());
method.getParams().setCredentialCharset(charset);
// parseNotifyConfig會保證使用GET方法時,request一定使用QueryString
method.setQueryString(request.getQueryString());
} else {
method = new PostMethod(request.getUrl());
((PostMethod) method).addParameters(request.getParameters());
method.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; text/html; charset=" + charset);
}
// 設置Http Header中的User-Agent屬性
method.addRequestHeader("User-Agent", "Mozilla/4.0");
HttpResponse response = new HttpResponse();
try {
httpclient.executeMethod(method);
if (request.getResultType().equals(HttpResultType.STRING)) {
response.setStringResult(method.getResponseBodyAsString());
} else if (request.getResultType().equals(HttpResultType.BYTES)) {
response.setByteResult(method.getResponseBody());
}
response.setResponseHeaders(method.getResponseHeaders());
} catch (UnknownHostException ex) {
return null;
} catch (IOException ex) {
return null;
} catch (Exception ex) {
return null;
} finally {
method.releaseConnection();
}
return response;
}
private HttpProtocolHandler() {
connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost);
connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn);
IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread();
ict.addConnectionManager(connectionManager);
ict.setConnectionTimeout(defaultIdleConnTimeout);
ict.start();
}
private static HttpProtocolHandler httpProtocolHandler = new HttpProtocolHandler();
/**
* 工廠方法
*
* @return
*/
public static HttpProtocolHandler getInstance() {
return httpProtocolHandler;
}
private static String DEFAULT_CHARSET = "GBK";
/** 連接超時時間,由bean factory設置,缺省為8秒鍾 */
private int defaultConnectionTimeout = 8000;
/** 回應超時時間, 由bean factory設置,缺省為30秒鍾 */
private int defaultSoTimeout = 30000;
/** 閑置連接超時時間, 由bean factory設置,缺省為60秒鍾 */
private int defaultIdleConnTimeout = 60000;
private int defaultMaxConnPerHost = 30;
private int defaultMaxTotalConn = 80;
/** 默認等待HttpConnectionManager返回連接超時(只有在達到最大連接數時起作用):1秒*/
private static final long defaultHttpConnectionManagerTimeout = 3 * 1000;
/**
* HTTP連接管理器,該連接管理器必須是線程安全的.(如何設置線程安全上文已經寫了)
*/
private HttpConnectionManager connectionManager;
最后一個是支付寶各接口構造類AlipayService.java,
到這里支付寶的登錄接口基本上已經結束了,講的不是很詳細
/**
* 支付寶提供給商戶的服務接入網關URL(新)
*/
private static final String ALIPAY_GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";
/**
* 構造快捷登錄接口
* @param sParaTemp 請求參數集合
* @return 表單提交HTML信息
*/
public static String alipay_auth_authorize(Map<String, String> sParaTemp) {
//增加基本配置
sParaTemp.put("service", "alipay.auth.authorize");
sParaTemp.put("target_service", "user.auth.quick.login");
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("return_url", AlipayConfig.return_url);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
String strButtonName = "確認";
return AlipaySubmit.buildForm(sParaTemp, ALIPAY_GATEWAY_NEW, "get", strButtonName);
}
/**
* 用於防釣魚,調用接口query_timestamp來獲取時間戳的處理函數
* 注意:遠程解析XML出錯,與服務器是否支持SSL等配置有關
* @return 時間戳字符串
* @throws IOException
* @throws DocumentException
* @throws MalformedURLException
*/
public static String query_timestamp() throws MalformedURLException,
DocumentException, IOException {
//構造訪問query_timestamp接口的URL串
String strUrl = ALIPAY_GATEWAY_NEW + "service=query_timestamp&partner=" + AlipayConfig.partner;
StringBuffer result = new StringBuffer();
SAXReader reader = new SAXReader();
Document doc = reader.read(new URL(strUrl).openStream());
List<Node> nodeList = doc.selectNodes("//alipay/*");
for (Node node : nodeList) {
// 截取部分不需要解析的信息
if (node.getName().equals("is_success") && node.getText().equals("T")) {
// 判斷是否有成功標示
List<Node> nodeList1 = doc.selectNodes("//response/timestamp/*");
for (Node node1 : nodeList1) {
result.append(node1.getText());
}
}
}
return result.toString();
}