From 0f94ecdc8840afde69ecbea2deac7d1cc073e888 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 26 Jun 2020 11:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E5=B8=A6=E7=94=A8=E6=88=B7=E7=8A=B6=E6=80=81token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cm/common/wechat/filter/WechatFilter.java | 2 +- .../wechat/filter/WechatMiniAppFilter.java | 4 ++-- .../impl/WechatMiniAppAuthServiceImpl.java | 22 ++++++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java index cb80477..1c2797c 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatFilter.java @@ -43,7 +43,7 @@ public class WechatFilter implements Filter { private WechatOfficialAccountProperties wechatOfficialAccountProperties; private AntPathMatcher antPathMatcher; - private static final String WECHAT_LOGIN_URL = String.format("/**%s/sign/login", ISystemConstant.WECHAT_PREFIX); + private static final String WECHAT_LOGIN_URL = String.format("/**%s/sign/login**", ISystemConstant.WECHAT_PREFIX); /** * 微信类放行 */ diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatMiniAppFilter.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatMiniAppFilter.java index 9563d36..d780362 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatMiniAppFilter.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/filter/WechatMiniAppFilter.java @@ -37,11 +37,11 @@ public class WechatMiniAppFilter implements Filter { /** * 授权登录 */ - private static final String WECHAT_MINI_APP_AUTH_LOGIN_URL = String.format("/**%s/auth/login", ISystemConstant.WECHAT_MINI_APP_PREFIX); + private static final String WECHAT_MINI_APP_AUTH_LOGIN_URL = String.format("/**%s/auth/login**", ISystemConstant.WECHAT_MINI_APP_PREFIX); /** * 认证登录 */ - private static final String WECHAT_MINI_APP_SIGN_LOGIN_URL = String.format("/**%s/sign/login", ISystemConstant.WECHAT_MINI_APP_PREFIX); + private static final String WECHAT_MINI_APP_SIGN_LOGIN_URL = String.format("/**%s/sign/login**", ISystemConstant.WECHAT_MINI_APP_PREFIX); /** * 微信类放行 */ diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java index e72224f..85bf99b 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java @@ -6,6 +6,7 @@ import com.cm.common.exception.WechatUserInfoException; import com.cm.common.exception.base.SystemException; import com.cm.common.result.SuccessResultData; import com.cm.common.utils.AesUtil; +import com.cm.common.utils.http.RestRequestUtil; import com.cm.common.wechat.config.pojo.WechatMiniAppProperties; import com.cm.common.wechat.pojo.WechatMiniAppLoginVO; import com.cm.common.wechat.pojo.WechatMiniAppUserInfo; @@ -20,6 +21,8 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.io.IOException; @@ -88,13 +91,20 @@ public class WechatMiniAppAuthServiceImpl implements IWechatMiniAppAuthService { Map params = new HashMap<>(1); params.put("signInfo", wechatSignInfo); HttpEntity> requestBody = new HttpEntity<>(params); - ResponseEntity result = restTemplate.postForEntity(wechatMiniAppProperties.getBindUserUrl(), requestBody, Map.class); - LOG.debug("状态码:{}", result.getStatusCodeValue()); - if (HttpStatus.OK.value() != result.getStatusCodeValue()) { - throw new SearchException("获取Token失败"); + try { + ResponseEntity result = restTemplate.postForEntity(wechatMiniAppProperties.getBindUserUrl(), requestBody, Map.class); + LOG.debug("状态码:{}", result.getStatusCodeValue()); + if (HttpStatus.OK.value() != result.getStatusCodeValue()) { + throw new SearchException("获取Token失败"); + } + Map body = result.getBody(); + return (String) body.get("data"); + } catch (HttpClientErrorException e) { + if (e.getRawStatusCode() == HttpStatus.BAD_REQUEST.value()) { + throw new SearchException(e.getResponseBodyAsString()); + } + throw new SearchException("绑定用户 | 登录失败"); } - Map body = result.getBody(); - return (String) body.get("data"); } }