All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.example.kotlin.http.hello.HelloWechat.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.firefly.example.kotlin.http.hello

import com.firefly.codec.http2.model.HttpMethod
import com.firefly.kotlin.ext.http.HttpServer
import com.firefly.utils.codec.HexUtils
import java.security.MessageDigest

/**
 * @author Pengtao Qiu
 */
fun main(args: Array) {
    val wechatToken = "xxxxddd"
    HttpServer {
        router {
            httpMethod = HttpMethod.GET
            path = "/"

            asyncHandler {
                val echoStr = getParamOpt("echostr").orElse("")
                val signature = getParamOpt("signature").orElse("")
                val nonce = getParamOpt("nonce").orElse("")
                val timestamp = getParamOpt("timestamp").orElse("")

                if (echoStr != "" && signature != "" && nonce != "" && timestamp != "") {
                    val paramArray = arrayOf(nonce, timestamp, wechatToken).sortedArray()
                    val sign = StringBuilder()
                    paramArray.forEach { sign.append(it) }

                    val hexSign =
                        HexUtils.bytesToHex(MessageDigest.getInstance("SHA-1").digest(sign.toString().toByteArray()))
                    println("verify wechat token $sign | $hexSign | $signature")
                    if (hexSign == signature) {
                        end(echoStr)
                    } else {
                        end("success")
                    }
                } else {
                    end("success")
                }
            }
        }

        router {
            httpMethod = HttpMethod.POST
            path = "/"

            asyncHandler {
                println(stringBody)
                end("success")
            }
        }

        router {
            httpMethod = HttpMethod.GET
            path = "/helloWechat"

            asyncHandler {
                end("hello")
            }
        }
    }.listen("localhost", 8080)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy