- package org.example;
- import com.grapecity.forguncy.commands.ICommandExecutableInServerSide;
- import com.grapecity.forguncy.commands.IServerCommandExecuteContext;
- import com.grapecity.forguncy.commands.annotation.ResultToProperty;
- import com.grapecity.forguncy.commands.annotation.common.Category;
- import com.grapecity.forguncy.commands.entity.Command;
- import com.grapecity.forguncy.commands.entity.ExecuteResult;
- import com.grapecity.forguncy.commands.enumeration.CommandScope;
- import com.grapecity.forguncy.plugincommon.common.annotation.*;
- import lombok.Data;
- import org.apache.commons.codec.binary.Base64;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- @Data
- public class TestChengJie extends Command implements ICommandExecutableInServerSide {
- private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- @DisplayName("AppSecret")
- @FormulaProperty
- @Required
- private String appSecret;
- @DisplayName("请求ID")
- @FormulaProperty
- @Required
- private String requestId;
- @DisplayName("时间戳")
- @FormulaProperty
- @Required
- private String timestamp;
- @DisplayName("数据")
- @FormulaProperty
- @Required
- private String data;
- @ResultToProperty
- @FormulaProperty
- @DisplayName("签名结果3")
- private String resultTo = "结果4";
- @Override
- public ExecuteResult execute(IServerCommandExecuteContext dataContext) {
- Long innerTimestamp = Long.parseLong(timestamp);
- String res = null;
- try {
- res = sign(appSecret, requestId, innerTimestamp, data);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- try {
- dataContext.getParameters().put(resultTo, res);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- ExecuteResult executeResult = new ExecuteResult();
- executeResult.getReturnValues().put("resultTo", "resultTo 11111");
- executeResult.getReturnValues().put("结果6666", "结果6666 111111");
- executeResult.getReturnValues().put("结果7777", res+2333);
- executeResult.getReturnValues().put("结果8888", res+34444444);
- return executeResult;
- }
- @Override
- public boolean getDesignerPropertyVisible(String propertyName, CommandScope commandScope) {
- return super.getDesignerPropertyVisible(propertyName, commandScope);
- }
- @Override
- public String toString() {
- return "签名程杰";
- }
- public static String sign(String appSecret, String requestId, Long timestamp, String data) throws Exception{
- // 1.签名参数按自然升序排列,拼接上data
- StringBuilder sb = new StringBuilder();
- sb.append("appSecret=").append(appSecret).append("&")
- .append("requestId=").append(requestId).append("&")
- .append("timestamp=").append(timestamp)
- .append(data);
- // 2.对签名字符串base64编码后获取32位md5值
- // 2.对签名字符串base64编码后获取32位md5值
- String base64Encode = base64Encode(sb.toString().getBytes("UTF-8"));
- String md5Value = md5(base64Encode);
- // 3.将得到的MD5值进行sha1散列,转换为16进制字符串
- String sign = sha1(md5Value);
- return sign;
- }
- /**
- * 对字符串进行MD5加密,得到32位MD5值
- * @param text 明文
- * @return 密文
- */
- public static String md5(String text) {
- try {
- MessageDigest msgDigest = MessageDigest.getInstance("MD5");
- msgDigest.update(text.getBytes("UTF-8"));
- byte[] bytes = msgDigest.digest();
- // 转成16进制
- return new String(encodeHex(bytes));
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalStateException("System doesn't support MD5 algorithm.");
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("System doesn't support your EncodingException.");
- }
- }
- /***
- * SHA加密
- * @return
- */
- public static String sha1(String content) throws Exception {
- MessageDigest sha = MessageDigest.getInstance("SHA1");
- byte[] byteArray = content.getBytes("UTF-8");
- return new String(encodeHex(sha.digest(byteArray)));
- }
- /**
- * base64编码
- *
- * @param content
- * @return
- * @throws Exception
- */
- public static String base64Encode(byte[] content) throws Exception {
- return Base64.encodeBase64String(content).replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "");
- }
- /**
- * base64解码
- *
- * @param content
- * @return
- * @throws Exception
- */
- public static byte[] base64Decode(String content) throws Exception {
- return Base64.decodeBase64(content);
- }
- /**
- * 转换成16进制
- * @param data
- * @return
- */
- private static char[] encodeHex(byte[] data) {
- int l = data.length;
- char[] out = new char[l << 1];
- // two characters form the hex value.
- for (int i = 0, j = 0; i < l; i++) {
- out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
- out[j++] = DIGITS[0x0F & data[i]];
- }
- return out;
- }
复制代码 |