Administrator
2021-11-09 5f50bd6ea5d5bdb7b8ea4d9e9a5851067b9aec1b
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
package com.duqing.missions.retrofit.Interceptor;
 
 
import com.duqing.missions.retrofit.utils.RSAUtils;
 
import org.json.JSONObject;
 
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
 
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
 
/**
 * My father is Object, ites purpose of     加密拦截器
 *
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2021-10-8.
 */
 
public class EncryptInterceptor implements Interceptor {
 
    protected static final Charset UTF8 = Charset.forName("UTF-8");
    private final String ENCRYPT = "encrypt";
 
    @Override
    public Response intercept(Chain chain) throws IOException {
        return chain.proceed(encryptRequest(chain.request()));
    }
 
 
    //加密
    protected Request encryptRequest(Request request) throws IOException {
        Headers headers = request.headers();
        RequestBody requestBody = request.body();
        Request.Builder builder = request.newBuilder();
        for(int i = 0 ; i < headers.size() ; i ++){
            builder.addHeader(headers.name(i),headers.value(i));
        }
        if(requestBody != null){
            Charset charset = UTF8;
            MediaType contentType = requestBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }
            HashMap param = new HashMap();
            if(requestBody instanceof MultipartBody){
                MultipartBody body = (MultipartBody) requestBody;
                for(MultipartBody.Part part:body.parts()){
                    Buffer buffer1 = new Buffer();
                    part.body().writeTo(buffer1);
                    String str=buffer1.readString(charset).replaceAll("%(?![0-9a-fA-F]{2})","%25");
                    param.put(part.headers().get(part.headers().name(0)), URLDecoder.decode(str, "UTF-8"));
                }
                MultipartBody.Builder mbuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                mbuilder.addFormDataPart(ENCRYPT,encryptParam(param));
                builder.post(mbuilder.build());
            }else if(requestBody instanceof FormBody){
                FormBody body = (FormBody) requestBody;
                for(int i = 0 ; i < body.size() ; i ++ ){
                    param.put(body.name(i),body.value(i));
                }
                FormBody.Builder formBuild = new FormBody.Builder();
                formBuild.add(ENCRYPT,encryptParam(param));
                builder.post(formBuild.build());
            }else{
                Buffer buffer = new Buffer();
                requestBody.writeTo(buffer);
                String str = buffer.readString(charset);
                String encrypt = encryptJson(str);
                param.put(ENCRYPT,encrypt);
                builder.post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new JSONObject(param).toString()));
            }
        }
        return builder.build();
    }
 
 
    /**
     * 加密传递的参数
     * @param params
     * @return
     */
    public static String encryptParam(Map<String, Object> params){
        return encryptJson(new JSONObject(params).toString());
    }
    public static String encryptJson(String json){
        try {
            return RSAUtils.encrypt(json,RSAUtils.getPublicKey(RSAUtils.PUBLIC_KEY));
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }
}