Commit d066ae4f authored by xianjun's avatar xianjun

添加返回转byte

parent b903cfcb
......@@ -262,6 +262,47 @@ public class HttpHelper {
return result.toString();
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param httpConn 链接信息
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
public static byte[] postResultByteArray(HttpURLConnection httpConn, String jsonString) throws IOException {
// 返回的结果
byte[] byteArray;
PrintWriter out = null;
// 处理请求参数
String params = "";
try {
params = jsonString;
// 获取HttpURLConnection对象对应的输出流
out = new PrintWriter(httpConn.getOutputStream());
// 发送请求参数
out.write(params);
// flush输出流的缓冲
out.flush();
int returnCode = httpConn.getResponseCode();
if (returnCode == HttpURLConnection.HTTP_OK) {
byteArray = readByteArray(httpConn.getInputStream());
} else {
throw new HttpCodeException(StringHelper.toString(returnCode), httpConn.getResponseMessage());
}
} catch (HttpCodeException ex) {
throw ex;
} catch (Exception ex) {
byteArray = readByteArray(httpConn.getErrorStream());
throw new HttpCodeException(StringHelper.toString(httpConn.getResponseCode()), ex.getMessage(), ex);
} finally {
if (out != null) {
out.close();
}
}
return byteArray;
}
/**
* 发送GET请求,当请求失败时,抛出异常或返回空字符串
*
......@@ -320,6 +361,23 @@ public class HttpHelper {
return in;
}
/**
* 读取数据流
*
* @param input 流
* @return 读取的缓存流
* @throws IOException IO异常
*/
public static byte[] readByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
/**
* 下载文件
*
......
package com.yanzuoguang.wxxcx.base;
/**
* @author 李贤军
*/
public interface WxXcxByteArrayRequest {
/**
* 请求其他
*
* @param req 请求后端实现
* @param convert 转换参数
* @return 返回请求流
*/
byte[] request(WxXcxRequestUrl req, WxXcxResponseArrayByteConvert convert);
}
package com.yanzuoguang.wxxcx.base;
import com.yanzuoguang.util.helper.HttpHelper;
import org.springframework.beans.factory.annotation.Value;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
/**
* @author 李贤军
*/
public class WxXcxRequestByteArrayImpl implements WxXcxByteArrayRequest {
@Value("${yzg.wx.xwx.charset:utf-8}")
private String defaultCharSet;
@Override
public byte[] request(WxXcxRequestUrl req, WxXcxResponseArrayByteConvert convert) {
String url = req.getUrl();
// String charSet = defaultCharSet;
String charSet = "utf-8";
Map<String, String> header;
if (req instanceof WxXcxRequestHeader) {
// 设置header信息
header = ((WxXcxRequestHeader) req).getHeader();
} else {
header = new HashMap<>(0);
}
byte[] response = null;
try {
if (req instanceof WxXcxRequestPostFormData) {
WxXcxRequestPostFormData post = (WxXcxRequestPostFormData) req;
HttpURLConnection conn = HttpHelper.getConn(url, header);
response = HttpHelper.postResultByteArray(conn, post.getPost());
} else if (req instanceof WxXcxRequestPostApplication) {
WxXcxRequestPostApplication post = (WxXcxRequestPostApplication) req;
// 打开URL连接
java.net.HttpURLConnection httpConn = HttpHelper.getConn(url, header, true);
response = HttpHelper.postResultByteArray(httpConn, post.getPost());
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return convert.getResponse(response);
}
}
package com.yanzuoguang.wxxcx.base;
/**
* @author 李贤军
*/
public interface WxXcxResponseArrayByteConvert {
/**
* 将字符串转换为结果
*
* @param response 字符串
* @return 实体结果
*/
byte[] getResponse(byte[] response);
}
package com.yanzuoguang.wxxcx.codeorlink.urlscheme;
import java.io.Serializable;
/**
* 获取小程序 scheme 码,适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。目前仅针对国内非个人主体的小程序开放
*
* @author 李贤军
*/
public class WxXcxUrlSchemeGenerateJumpWxaReq {
public class WxXcxUrlSchemeGenerateJumpWxaReq implements Serializable {
/**
* 通过 scheme 码进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带 query。path 为空时会跳转小程序主页
*/
......
package com.yanzuoguang.wxxcx.codeorlink.urlscheme;
import java.io.Serializable;
/**
* 获取小程序 scheme 码 请求
* 适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。目前仅针对国内非个人主体的小程序开放
*
* @author 李贤军
*/
public class WxXcxUrlSchemeGenerateReq {
/**
* 接口调用凭证
*/
private String access_token;
public class WxXcxUrlSchemeGenerateReq implements Serializable {
/**
* 跳转到的目标小程序信息。
*/
......@@ -28,13 +27,6 @@ public class WxXcxUrlSchemeGenerateReq {
*/
private int expire_interval;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public WxXcxUrlSchemeGenerateJumpWxaReq getJump_wxa() {
return jump_wxa;
......
package com.yanzuoguang.wxxcx.codeorlink.urlscheme;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.helper.CheckerHelper;
import com.yanzuoguang.util.helper.HttpHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.wxxcx.base.*;
import com.yanzuoguang.wxxcx.codeorlink.urlscheme.service.WxXcxUrlSchemeGenerateService;
import com.yanzuoguang.wxxcx.codeorlink.urlscheme.service.impl.WxXcxUrlSchemeGenerateServiceImpl;
import java.util.HashMap;
import java.util.Map;
/**
* 获取小程序 scheme 码 转换
......@@ -13,7 +20,7 @@ import com.yanzuoguang.wxxcx.base.*;
*
* @author 李贤军
*/
public class WxXcxUrlSchemeGenerateRequest implements WxXcxRequestUrl, WxXcxRequestUrlPara, WxXcxRequestPostFormData {
public class WxXcxUrlSchemeGenerateRequest implements WxXcxRequestUrl, WxXcxRequestUrlPara, WxXcxRequestPostApplication {
private final WxXcxConfig wxXcxConfig;
private final WxXcxRequest wxXcxRequest;
......@@ -39,11 +46,10 @@ public class WxXcxUrlSchemeGenerateRequest implements WxXcxRequestUrl, WxXcxRequ
@Override
public String getPost() {
return HttpHelper.getUrlParameter(req);
return JSON.toJSONString(req);
}
public WxXcxUrlSchemeGenerateRes getResponse() {
checkParam(req);
return this.wxXcxRequest.request(this, response -> {
WxXcxUrlSchemeGenerateRes ret = JsonHelper.deserialize(response, new TypeReference<WxXcxUrlSchemeGenerateRes>() {
});
......@@ -52,7 +58,5 @@ public class WxXcxUrlSchemeGenerateRequest implements WxXcxRequestUrl, WxXcxRequ
});
}
private void checkParam(WxXcxUrlSchemeGenerateReq req) {
CheckerHelper.newInstance().notBlankCheck(WxXcxAccessTokenField.FIELD_ACCESS_TOKEN, req.getAccess_token()).checkException();
}
}
\ No newline at end of file
package com.yanzuoguang.wxxcx.codeorlink.wxacode;
import java.io.Serializable;
/**
* 获取小程序二维码 请求
* 适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
*
* @author 李贤军
*/
public class WxXcxWxaCodeCreateQrCodeReq {
public class WxXcxWxaCodeCreateQrCodeReq implements Serializable {
/**
* 接口调用凭证
*/
......
package com.yanzuoguang.wxxcx.codeorlink.wxacode;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.helper.CheckerHelper;
import com.yanzuoguang.util.helper.HttpHelper;
......@@ -38,7 +39,7 @@ public class WxXcxWxaCodeCreateQrCodeRequest implements WxXcxRequestUrl, WxXcxRe
@Override
public String getPost() {
return HttpHelper.getUrlParameter(req);
return JSONObject.toJSONString(req);
}
public WxXcxWxaCodeCreateQrCodeRes getResponse() {
......
......@@ -7,10 +7,7 @@ package com.yanzuoguang.wxxcx.codeorlink.wxacode;
* @author 李贤军
*/
public class WxXcxWxaCodeGetUnlimitedReq {
/**
* 接口调用凭证
*/
private String access_token;
/**
* 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式
*/
......@@ -27,10 +24,6 @@ public class WxXcxWxaCodeGetUnlimitedReq {
* 要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
*/
private String env_version;
/**
* 二维码的宽度,单位 px,最小 280px,最大 1280px
*/
private int width;
/**
* 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
*/
......@@ -44,14 +37,6 @@ public class WxXcxWxaCodeGetUnlimitedReq {
*/
private boolean is_hyaline;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getScene() {
return scene;
}
......@@ -84,14 +69,6 @@ public class WxXcxWxaCodeGetUnlimitedReq {
this.env_version = env_version;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public boolean isAuto_color() {
return auto_color;
}
......
package com.yanzuoguang.wxxcx.codeorlink.wxacode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sun.org.apache.regexp.internal.RE;
import com.yanzuoguang.util.helper.CheckerHelper;
import com.yanzuoguang.util.helper.HttpHelper;
import com.yanzuoguang.util.helper.JsonHelper;
......@@ -12,16 +14,16 @@ import com.yanzuoguang.wxxcx.base.*;
*
* @author 李贤军
*/
public class WxXcxWxaCodeGetUnlimitedRequest implements WxXcxRequestUrl, WxXcxRequestUrlPara, WxXcxRequestPostFormData {
public class WxXcxWxaCodeGetUnlimitedRequest implements WxXcxRequestUrl, WxXcxRequestUrlPara, WxXcxRequestPostApplication {
private final WxXcxConfig wxXcxConfig;
private final WxXcxRequest wxXcxRequest;
private final WxXcxByteArrayRequest wxXcxByteArrayRequest;
private final WxXcxAccessToken accessToken;
private final WxXcxWxaCodeGetUnlimitedReq req;
public WxXcxWxaCodeGetUnlimitedRequest(WxXcxConfig wxXcxConfig, WxXcxRequest wxXcxRequest, WxXcxAccessToken accessToken, WxXcxWxaCodeGetUnlimitedReq req) {
public WxXcxWxaCodeGetUnlimitedRequest(WxXcxConfig wxXcxConfig, WxXcxByteArrayRequest wxXcxByteArrayRequest, WxXcxAccessToken accessToken, WxXcxWxaCodeGetUnlimitedReq req) {
this.wxXcxConfig = wxXcxConfig;
this.wxXcxRequest = wxXcxRequest;
this.wxXcxByteArrayRequest = wxXcxByteArrayRequest;
this.accessToken = accessToken;
this.req = req;
}
......@@ -38,22 +40,21 @@ public class WxXcxWxaCodeGetUnlimitedRequest implements WxXcxRequestUrl, WxXcxRe
@Override
public String getPost() {
return HttpHelper.getUrlParameter(req);
return JSON.toJSONString(req);
}
public WxXcxWxaCodeGetUnlimitedRes getResponse() {
checkParam(req);
return this.wxXcxRequest.request(this, response -> {
WxXcxWxaCodeGetUnlimitedRes ret = JsonHelper.deserialize(response, new TypeReference<WxXcxWxaCodeGetUnlimitedRes>() {
});
return this.wxXcxByteArrayRequest.request(this, response -> {
WxXcxWxaCodeGetUnlimitedRes ret = new WxXcxWxaCodeGetUnlimitedRes();
ret.setData(response);
WxXcxAssert.assertBaseError(ret);
return ret;
});
}
private void checkParam(WxXcxWxaCodeGetUnlimitedReq req) {
CheckerHelper.newInstance().notBlankCheck(WxXcxAccessTokenField.FIELD_ACCESS_TOKEN, req.getAccess_token())
.notBlankCheck("scene", req.getScene())
CheckerHelper.newInstance().notBlankCheck("scene", req.getScene())
.checkException();
}
}
\ No newline at end of file
......@@ -2,8 +2,6 @@ package com.yanzuoguang.wxxcx.codeorlink.wxacode;
import com.yanzuoguang.wxxcx.base.WxXcxResponseBaseError;
import java.nio.Buffer;
/**
* 获取小程序码 响应
* 适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制
......@@ -11,28 +9,13 @@ import java.nio.Buffer;
* @author 李贤军
*/
public class WxXcxWxaCodeGetUnlimitedRes extends WxXcxResponseBaseError {
/**
* 数据类型 (MIME Type)
*/
private String contentType;
/**
* 数据 Buffer
*/
private Buffer buffer;
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
private byte[] data;
public Buffer getBuffer() {
return buffer;
public byte[] getData() {
return data;
}
public void setBuffer(Buffer buffer) {
this.buffer = buffer;
public void setData(byte[] data) {
this.data = data;
}
}
package com.yanzuoguang.wxxcx.codeorlink.wxacode.service.impl;
import com.yanzuoguang.wxxcx.base.WxXcxAccessToken;
import com.yanzuoguang.wxxcx.base.WxXcxByteArrayRequest;
import com.yanzuoguang.wxxcx.base.WxXcxConfig;
import com.yanzuoguang.wxxcx.base.WxXcxRequest;
import com.yanzuoguang.wxxcx.codeorlink.wxacode.WxXcxWxaCodeGetUnlimitedReq;
......@@ -17,11 +18,11 @@ import org.springframework.stereotype.Component;
@Component
public class WxXcxWxaCodeGetUnlimitedServiceImpl implements WxXcxWxaCodeGetUnlimitedService {
private final WxXcxConfig wxXcxConfig;
private final WxXcxRequest wxXcxRequest;
private final WxXcxByteArrayRequest wxXcxByteArrayRequest;
public WxXcxWxaCodeGetUnlimitedServiceImpl(WxXcxConfig wxXcxConfig, WxXcxRequest wxXcxRequest) {
public WxXcxWxaCodeGetUnlimitedServiceImpl(WxXcxConfig wxXcxConfig, WxXcxByteArrayRequest wxXcxByteArrayRequest) {
this.wxXcxConfig = wxXcxConfig;
this.wxXcxRequest = wxXcxRequest;
this.wxXcxByteArrayRequest = wxXcxByteArrayRequest;
}
/**
......@@ -33,7 +34,7 @@ public class WxXcxWxaCodeGetUnlimitedServiceImpl implements WxXcxWxaCodeGetUnlim
*/
@Override
public WxXcxWxaCodeGetUnlimitedRes getWxaCodeGetUnlimited(WxXcxAccessToken accessToken, WxXcxWxaCodeGetUnlimitedReq req) {
WxXcxWxaCodeGetUnlimitedRequest request = new WxXcxWxaCodeGetUnlimitedRequest(wxXcxConfig, wxXcxRequest, accessToken, req);
WxXcxWxaCodeGetUnlimitedRequest request = new WxXcxWxaCodeGetUnlimitedRequest(wxXcxConfig, wxXcxByteArrayRequest, accessToken, req);
return request.getResponse();
}
}
......@@ -2,7 +2,6 @@ package com.yanzuoguang.wxxcx.login.service;
import com.yanzuoguang.wxxcx.login.WxXcxAuthCode2SessionReq;
import com.yanzuoguang.wxxcx.login.WxXcxAuthCode2SessionRes;
import com.yanzuoguang.wxxcx.base.WxXcxAccessToken;
/**
* 登录凭证校验参数 接口
......@@ -13,10 +12,9 @@ public interface WxXcxAuthCode2SessionService {
/**
* 登录凭证校验参数
*
* @param accessToken accessToken
* @param req 登录凭证校验参数 入参
* @return 登录凭证校验参数 返回
*/
WxXcxAuthCode2SessionRes getAuthCode2Session(WxXcxAccessToken accessToken, WxXcxAuthCode2SessionReq req);
WxXcxAuthCode2SessionRes getAuthCode2Session(WxXcxAuthCode2SessionReq req);
}
......@@ -4,7 +4,6 @@ import com.yanzuoguang.wxxcx.login.WxXcxAuthCode2SessionReq;
import com.yanzuoguang.wxxcx.login.WxXcxAuthCode2SessionRequest;
import com.yanzuoguang.wxxcx.login.WxXcxAuthCode2SessionRes;
import com.yanzuoguang.wxxcx.login.service.WxXcxAuthCode2SessionService;
import com.yanzuoguang.wxxcx.base.WxXcxAccessToken;
import com.yanzuoguang.wxxcx.base.WxXcxConfig;
import com.yanzuoguang.wxxcx.base.WxXcxRequest;
import org.springframework.stereotype.Component;
......@@ -27,12 +26,11 @@ public class WxXcxAuthCode2SessionServiceImpl implements WxXcxAuthCode2SessionSe
/**
* 登录凭证校验参数
*
* @param accessToken accessToken
* @param req 登录凭证校验参数 入参
* @return 登录凭证校验参数返回
*/
@Override
public WxXcxAuthCode2SessionRes getAuthCode2Session(WxXcxAccessToken accessToken, WxXcxAuthCode2SessionReq req) {
public WxXcxAuthCode2SessionRes getAuthCode2Session( WxXcxAuthCode2SessionReq req) {
WxXcxAuthCode2SessionRequest request = new WxXcxAuthCode2SessionRequest(wxXcxConfig, wxXcxRequest, req);
return request.getResponse();
}
......
package com.yanzuoguang.wxxcx.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
/**
* 微信工具类
*
* @author 李贤军
*/
public class WeChatUtil {
public static void main(String[] args) {
String result = decryptData(
"+6fr9M+bNeRk8LQOOuxqBLuzsydnIh7D2UvImuWicfzJsbrRFptPr7yXHubgTdRd6JHwvjDXD+Q9L0oeTjXlBZilfipjRZJSV7nOpaq++DB5wQr6hKAPsvLmUjOpTPocpVrRDbkXRQKAgl6uTXkR8SdUL3j0zihANr3ANaz2kgg8X+iCJKSxmCuxwPswFCrYaXih2Z7+s/EqWU0ACFgaoZMNkliYBy9mF/pwzCfzsDVx+eJu1eG2UmU6e0e8rUOcEGv4KxfYntvLBN7LJEfOEfYcyemNYtVt3ALDE2sOxI4pb8XxUUO3zn+Yt7e5xWqmfqFj1YK3CCl0ILHle5JlZxGz178PsztDoCnOMoA4NwEoGfqCsUH1pM7AsyfcOh27yqqk=",
"2gCRTCyEpjQTW5Fc89yg==",
"XBzf8VMLBIsFBZvjOWNg=="
);
System.out.println("result = " + result);
}
public static String decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
return new String(
decryptOfDiyIV(
Base64.decode(encryptDataB64),
Base64.decode(sessionKeyB64),
Base64.decode(ivB64)
)
);
}
private static final String KEY_ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/CBC/PKCS7Padding";
private static Key key;
private static Cipher cipher;
private static void init(byte[] keyBytes) {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
// 转化成JAVA的密钥格式
key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
try {
// 初始化cipher
cipher = Cipher.getInstance(ALGORITHM_STR, "BC");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解密方法
*
* @param encryptedData 要解密的字符串
* @param keyBytes 解密密钥
* @param ivs 自定义对称解密算法初始向量 iv
* @return 解密后的字节数组
*/
private static byte[] decryptOfDiyIV(byte[] encryptedData, byte[] keyBytes, byte[] ivs) {
byte[] encryptedText = null;
init(keyBytes);
try {
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivs));
encryptedText = cipher.doFinal(encryptedData);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedText;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment