Skip to content

Instantly share code, notes, and snippets.

@gjp0609
Created December 27, 2019 07:25
Show Gist options
  • Save gjp0609/4d72f60753dd8d304f0a21ba7fdd8eee to your computer and use it in GitHub Desktop.
Save gjp0609/4d72f60753dd8d304f0a21ba7fdd8eee to your computer and use it in GitHub Desktop.
wechat redirect
val OAUTH2_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect"
val appId = "xxxx"
/**
* 认证
* @param url 回跳链接
* @param scope 授权方式
*/
@GetMapping("/getCode")
fun redirect(url: String, scope: String, response: HttpServletResponse) {
val encodedUrl = encode(url, StandardCharsets.UTF_8)
val redirectUrl: String = OAUTH2_URL
.replace("APPID", appId)
.replace("REDIRECTURI", encode("http://www.xxx.com/redirectBack?url=$encodedUrl", StandardCharsets.UTF_8))
.replace("SCOPE", scope)
response.sendRedirect(redirectUrl)
}
/**
* 回跳
* @param url 回跳链接
* @param code 授权code
*/
@GetMapping("/redirectBack")
fun redirectBack(url: String, response: HttpServletResponse, code: String) {
val redirectUrl = url + (if (url.contains("?")) "&" else "?") + "code=$code"
response.sendRedirect(redirectUrl)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment