Giter VIP home page Giter VIP logo

Comments (3)

getActivity avatar getActivity commented on June 9, 2024

小伙子,后台返回的数据流不能读两次,你可以将之前的 ResponseBody 的数据全部解析出来,然后进行解密,最后重新封装到一个新的 ResponseBody 对象中。

from easyhttp.

clbDream avatar clbDream commented on June 9, 2024

我这边这边处理了一下,目前请求正常,有时间的话麻烦大佬看看有没有什么问题

override fun interceptResponse(httpRequest: HttpRequest<*>, response: Response): Response {
        val api = httpRequest.requestApi
        if (ApiManager.encryptionWhiteListApi.contains(api::class).not()) {
            EasyLog.printLog(httpRequest, "不在白名单接口,做加解密操作")
            if (response.code() == 200) {
                val bytes = response.body()?.bytes()
                if (bytes != null) {
                    val oldResponseBodyStr = String(bytes)
                    if (oldResponseBodyStr.isNotEmpty()) {
                        val oldJsonObject = JSONObject(oldResponseBodyStr)
                        val data = oldJsonObject.optString("data")
                        if (response.isSuccessful && data.isNullOrEmpty().not()) {
                            //使用AES解密
                            val encryptData =
                                AESUtils.decrypt(
                                    data,
                                    EncryptionAndDecryptionUtils.aesKey
                                )
                            if (encryptData.isNotEmpty()) {
                                if (encryptData.substring(0, 1) == "[") {
                                    //数组
                                    val jsonArray = JSONArray(encryptData)
                                    oldJsonObject.put("data", jsonArray)
                                } else if (encryptData.substring(0, 1) == "{") {
                                    //对象
                                    val newJsonObject = JSONObject(encryptData)
                                    oldJsonObject.put("data", newJsonObject)
                                } else {
                                    //具体的数据
                                    oldJsonObject.put("data", encryptData)
                                }
                                val resultJson = oldJsonObject.toString()
                                val newResponseBody =
                                    ResponseBody.create(response.body()?.contentType(), resultJson)
                                return response.newBuilder().body(newResponseBody).build()
                            }else{
                                val newResponseBody =
                                    ResponseBody.create(response.body()?.contentType(), oldResponseBodyStr)
                                return response.newBuilder().body(newResponseBody).build()
                            }
                        }else{
                            val newResponseBody =
                                ResponseBody.create(response.body()?.contentType(), oldResponseBodyStr)
                            return response.newBuilder().body(newResponseBody).build()
                        }
                    }else{
                        val newResponseBody =
                            ResponseBody.create(response.body()?.contentType(), oldResponseBodyStr)
                        return response.newBuilder().body(newResponseBody).build() 
                    }
                }
            }
        } else {
            EasyLog.printLog(httpRequest, "白名单接口,不做加解密操作")
        }

        return response
    }

from easyhttp.

getActivity avatar getActivity commented on June 9, 2024

@clbDream 小伙子,这样写有没有问题这种事情只能靠自己多测试了。

from easyhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.