android - 如何使用 Interceptor 使用 Retrofit2 进行参数(正文)加密
问题描述
我创建了自定义EncryptionInterceptor
类,但我无法加密完美的数据,任何帮助表示赞赏,它将节省我的时间
这是我的代码
public class EncryptionInterceptor implements Interceptor {
private static final String TAG = EncryptionInterceptor.class.getSimpleName();
private static final boolean DEBUG = true;
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RequestBody oldBody = request.body();
Buffer buffer = new Buffer();
oldBody.writeTo(buffer);
String strOldBody = buffer.readUtf8();
//MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
String strNewBody = encrypt(strOldBody);
RequestBody body = RequestBody.create(mediaType, strNewBody);
request = request.newBuilder()/*.header("Content-Type", body.contentType().toString()).header("Content-Length", String.valueOf(body.contentLength()))*/
.method(request.method(), body).build();
return chain.proceed(request);
}
解决方案
private static class NetworkInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RequestBody oldBody = request.body(); //retrieve the current request body
Buffer buffer = new Buffer();
oldBody.writeTo(buffer);
String strOldBody = buffer.readUtf8(); // String representation of the current request body
buffer.clear();
buffer.close();
MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
String strNewBody = enDecService.encryptBody(strOldBody); // Your encryption logic
RequestBody body = RequestBody.create(mediaType, strNewBody); // New request body with the encrypted string of the current request body
request = request.newBuilder()
.header("Content-Type", "application/json")
.header("Content-Length", String.valueOf(body.contentLength()))
.header("Authorization", "Bearer " + AppConstants.token)
.method(request.method(), body).build();
long t1 = System.nanoTime();
Log.d(TAG, String.format("Sending request %s on %s", request.url(), request.headers()));
Response response = chain.proceed(request); // sending req. to server. current req. body is a encrypted string.
int maxAge = 6000; // read from cache for 6000 seconds even if there is internet connection
response.header("Cache-Control", "public, max-age=" + maxAge);
response = response.newBuilder().removeHeader("Pragma").build();
long t2 = System.nanoTime();
Log.d(TAG, String.format("Received response for %s in %.1fms %s", response.request().url(), (t2 - t1) / 1e6d, response.toString()));
try {
String s = response.body().string(); // retrieve string representation of encrypted response.
ResponseBody responseBody = ResponseBody.create(mediaType, enDecService.decryptBody(s)); // decrypt the encrypted response. yor decryption logic goes here.
response = response.newBuilder().body(responseBody).build(); // build a new response with the decrypted response body.
} catch (JOSEException e) {
} catch (ParseException e) {
}
return response;
}
}
推荐阅读
- ios - Firebase更新我的表格视图时的ios通知
- workflow - NetSuite 中是否存在限制发送电子邮件操作中包含事务工作流功能的配置设置?
- ruby-on-rails - Shopify 应用程序 - 如何使用带有 ruby on rails 的应用程序代理来包含资产
- vba - 使用动态单元格引用通过 VBA 应用公式。
- c# - 如何获取存储过程和 C# 代码中的总记录?
- db2 - UPDATE 语句仅适用于 FETCH FIRST
- c# - 如果所需日期大于或小于当前日期,如何检查日期?
- apollo - 错误的时机阿波罗订阅
- python-3.x - 类型错误:不支持的操作数
- c++ - OpenMP - 在两个 for 循环中使用临界区