配置文件
<bean id="wxMpInMemoryConfigStorage" class="me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage"> <property name="appId" value="${wx_mp_appid}"/> <property name="secret" value="${wx_mp_secret}"/> <property name="token" value="${wx_mp_mytoken}"/> </bean> <bean id="wxMpService" class="me.chanjar.weixin.mp.api.impl.WxMpServiceImpl"> <property name="wxMpConfigStorage" ref="wxMpInMemoryConfigStorage"/> </bean>
wx_mp_appid=appid
wx_mp_secret=secret
wx_mp_redirect_uri=http://*******************/userInfo
wx_mp_mytoken=testtoken
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin.tools.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
/**
* 檢驗token是否合法
*
* @param response
* @param timestamp
* @param nonce
* @param signature
* @param echostr
*/
@RequestMapping("/check")
@ApiOperation("檢驗token是否合法")
public void check(ServletResponse response, String timestamp, String nonce, String signature, String echostr) {
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
log.error("不合法");
throw new RuntimeException();
}
PrintWriter o = null;
try {
o = new PrintWriter(response.getWriter());
o.print(echostr);
} catch (IOException e) {
log.error("寫回微信端錯誤{}", e.getMessage());
} finally {
o.close();
}
}
/**
* 授權,獲取code
*
* @param returnUrl
* @return
*/
@RequestMapping("authorize")
@ApiOperation("授權,獲取code")
public String authorize(@RequestParam("returnUrl") String returnUrl) {
String result = wxMpService.oauth2buildAuthorizationUrl(redirectUri, WxConsts.OAuth2Scope.SNSAPI_BASE, returnUrl);
log.info("獲取得redirectUrl ==========>>>>>>{}", result);
return "redirect:" + result;
}
/**
* 處理業務層,比如獲取到open_id跳轉到某個頁面
*
* @param code 授權code
* @param returnUrl 返回用戶的請求url
* @return
*/
@ApiOperation("處理業務層,比如獲取到open_id跳轉到某個頁面")
@GetMapping(value = {"userInfo"})
public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) {
log.info("code===>{} , state====>{}", code, returnUrl);
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = null;
try {
wxMpOAuth2AccessToken = this.wxMpService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
log.error("wechat error{}", e.getError().getErrorMsg());
throw new RuntimeException(e.getError().getErrorMsg());
}
boolean valid = this.wxMpService.oauth2validateAccessToken(wxMpOAuth2AccessToken);
if (!valid) {
try {
wxMpOAuth2AccessToken = this.wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
} catch (WxErrorException e) {
log.error("wechat error{}", e.getError().getErrorMsg());
throw new RuntimeException(e.getError().getErrorMsg());
}
}
WxMpUser wxMpUser = null;
try {
wxMpUser = this.wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
log.info("獲取到的用戶信息是:=================================>>>>{}", wxMpUser);
log.info("獲取到wxMpOAuth2AccessToken========================>>>>{}", wxMpOAuth2AccessToken);
} catch (WxErrorException e) {
log.error("wechat error{}", e.getError().getErrorMsg());
e.printStackTrace();
}
//更新保存用戶數據
UserEntity entity = this.userService.selectByOpenIdUser(wxMpUser.getOpenId());
if (ObjectUtils.isEmpty(entity)) {
entity = new UserEntity();
entity.setOpenId(wxMpUser.getOpenId());
entity.setUserImg(wxMpUser.getHeadImgUrl());
entity.setCreateTime(new Date());
entity.setNickName(wxMpUser.getNickname());
try {
this.userService.inserUser(entity);
} catch (RRException r) {
r.setMsg("插入用戶數據異常");
log.error(r.getMsg());
}
} else {
try {
this.userService.updateById(entity);
} catch (RRException r) {
r.setMsg("插入用戶數據異常");
log.error(r.getMsg());
}
}
RedisUtil redisUtil = RedisUtil.getInstance();
// redisService.add(wxMpOAuth2AccessToken.getAccessToken(),entity.toString(),7200,0);
redisUtil.putInRedis(wxMpOAuth2AccessToken.getAccessToken(),entity.toString(),7200);
return "redirect:" + returnUrl + "?token=" + wxMpOAuth2AccessToken.getAccessToken();
}
公衆號配置:
開發者工具---->公衆平臺測試賬號
URL:與項目中的要一致
Token:與項目中的一致
關注公衆號
網頁服務--------->網頁賬號-------------->修改,輸入下面路徑
junwei.free.idcfengye.com(解析出來的網址)
微信工具訪問:
例:http://www.baidu.com(映射到外網的地址)/**********/authorize?returnUrl=http://www.baidu.com
以上是測試環境
配置不變:
在公衆號中:開發者工具------->web開發者工具--------->綁定開發者微信號
接口權限:網頁服務---->網頁授權---------->修改------->網頁授權域名 配置的域名是解析並備案的只要域名(www.baidu.com)
基本配置中的,服務器配置------->修改配置
配置完成
本地調試可搜索“內網映射外網工具”進行映射訪問