我不是一个真正的java开发人员,但我目前正在开发一个需要一些java技能的项目。我从Cybersource获得了示例代码以实现他们的安全接受付款,但如果我按原样使用,我的代码就会出现问题。我认为ArrayList可能是个问题。它不会编译,但我没有看到错误消息。我正在使用一个框架,它使得它看到错误消息不同。不知道如何只测试代码的这一部分。
${
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
private String sign(HashMap params) throws InvalidKeyException, NoSuchAlgorithmException, {
return sign(buildDataToSign(params), "111111111111111");
}
private String sign(String data, String secretKey) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encodeBuffer(rawHmac).replace("\n", "");
}
private String buildDataToSign(HashMap params) {
String[] signedFieldNames = String.valueOf(params.get("signed_field_names")).split(",");
ArrayList<String> dataToSign = new ArrayList<String>();
for (String signedFieldName : signedFieldNames) {
dataToSign.add(signedFieldName + "=" + String.valueOf(params.get(signedFieldName)));
}
return commaSeparate(dataToSign);
}
private String commaSeparate(ArrayList<String> dataToSign) {
StringBuilder csv = new StringBuilder();
for (Iterator<String> it = dataToSign.iterator(); it.hasNext(); ) {
csv.append(it.next());
if (it.hasNext()) {
csv.append(",");
}
}
return csv.toString();
}
}$
答案 0 :(得分:3)
您需要做的是在常规Java文件中创建一个类,然后将其导入JSP中,如下所示:
<%@ page import="com.yourcompany.package.ClassName" %>
您可以从JSP文件中访问任何方法,打印变量等。使用&lt;%code&gt;标签。例如:
<p>The sign response is <%= sign("test", "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=") %></p>