GatewayServlet.java
6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Alipay.com Inc.
* Copyright (c) 2004-2014 All Rights Reserved.
*/
package com.alipay.servlet.gateway;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.constants.AlipayServiceEnvConstants;
import com.alipay.dispatcher.Dispatcher;
import com.alipay.executor.ActionExecutor;
import com.alipay.util.LogUtil;
import com.alipay.util.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
* 开发者网关,支付宝所有主动和开发者的交互会经过此网关进入开发者系统(配置在开放平台的应用网关)
*
* @author taixu.zqq
* @version $Id: GatewayServlet.java, v 0.1 2014年7月22日 下午5:59:55 taixu.zqq Exp $
*/
public class GatewayServlet extends HttpServlet {
private final static Logger logger = LoggerFactory.getLogger(GatewayServlet.class);
/**
*
*/
private static final long serialVersionUID = 1210436705188940602L;
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
logger.info("enter ali GatewayServlet req {} ",req);
this.doPost(req, resp);
logger.info("enter ali GatewayServlet end ");
}
/**
* 网关处理
*
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
logger.info("enter ali GatewayServlet post ");
//支付宝响应消息
String responseMsg = "";
//1. 解析请求参数
Map<String, String> params = RequestUtil.getRequestParams(request);
//打印本次请求日志,开发者自行决定是否需要
LogUtil.log("支付宝请求串", params.toString());
try {
//2. 验证签名
this.verifySign(params);
//3. 获取业务执行器 根据请求中的 service, msgType, eventType, actionParam 确定执行器
ActionExecutor executor = Dispatcher.getExecutor(params);
//4. 执行业务逻辑
responseMsg = executor.execute();
}
catch (AlipayApiException alipayApiException) {
//开发者可以根据异常自行进行处理
alipayApiException.printStackTrace();
logger.error("enter ali GatewayServlet post error {} ",alipayApiException);
}
catch (Exception exception) {
//开发者可以根据异常自行进行处理
exception.printStackTrace();
logger.error("enter ali GatewayServlet post exception {} ",exception);
} finally {
//5. 响应结果加签及返回
try {
//对响应内容加签
responseMsg = encryptAndSign(responseMsg,
AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY,
AlipayServiceEnvConstants.PRIVATE_KEY, AlipayServiceEnvConstants.CHARSET,
false, true, AlipayServiceEnvConstants.SIGN_TYPE);
//http 内容应答
response.reset();
response.setContentType("text/xml;charset=GBK");
PrintWriter printWriter = response.getWriter();
printWriter.print(responseMsg);
response.flushBuffer();
//开发者自行决定是否要记录,视自己需求
LogUtil.log("开发者响应串", responseMsg);
} catch (AlipayApiException alipayApiException) {
//开发者可以根据异常自行进行处理
alipayApiException.printStackTrace();
logger.error("enter ali GatewayServlet post exception {} ",alipayApiException);
}
}
}
/**
* 验签
*
* @param
* @return
*/
private void verifySign(Map<String, String> params) throws AlipayApiException {
if (!AlipaySignature.rsaCheckV2(params, AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY,
AlipayServiceEnvConstants.SIGN_CHARSET, AlipayServiceEnvConstants.SIGN_TYPE)) {
throw new AlipayApiException("verify sign fail.");
}
}
public static String encryptAndSign(String bizContent, String alipayPublicKey, String cusPrivateKey, String charset,
boolean isEncrypt, boolean isSign, String signType) throws AlipayApiException {
StringBuilder sb = new StringBuilder();
if (StringUtils.isEmpty(charset)) {
charset = AlipayConstants.CHARSET_GBK;
}
sb.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
if (isEncrypt) {// 加密
sb.append("<alipay>");
String encrypted = AlipaySignature.rsaEncrypt(bizContent, alipayPublicKey, charset);
sb.append("<response>" + encrypted + "</response>");
sb.append("<encryption_type>AES</encryption_type>");
if (isSign) {
String sign = AlipaySignature.rsaSign(encrypted, cusPrivateKey, charset, signType);
sb.append("<sign>" + sign + "</sign>");
sb.append("<sign_type>");
sb.append(signType);
sb.append("</sign_type>");
}
sb.append("</alipay>");
} else if (isSign) {// 不加密,但需要签名
sb.append("<alipay>");
sb.append("<response>" + bizContent + "</response>");
String sign = AlipaySignature.rsaSign(bizContent, cusPrivateKey, charset, signType);
sb.append("<sign>" + sign + "</sign>");
sb.append("<sign_type>");
sb.append(signType);
sb.append("</sign_type>");
sb.append("</alipay>");
} else {// 不加密,不加签
sb.append(bizContent);
}
return sb.toString();
}
}