【微信授權】極其簡單的實現方法
阿新 • • 發佈:2019-01-11
強烈推薦使用一個工具包,在maven的中心倉庫中搜索"weixin-java"就可以搜尋到,感謝這位大佬的作品,大大簡化了微信端開發的難度。
接下來是一個簡單例子,是我在我的實際專案中抽取出來的一部分,專案使用的是springboot框架,但是無論使用什麼框架微信授權的步驟和原理是一樣的。
這個例子是微信公眾號授權的例子。
程式碼如下:
package cn.smileyan.boot.main.controler; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import cn.smileyan.boot.main.entity.User; import cn.smileyan.boot.main.service.UserService; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; import me.chanjar.weixin.mp.bean.result.WxMpUser; @Controller @Slf4j @RequestMapping("/login") @CrossOrigin public class LoginControler { @Autowired private UserService userService; @Value("${wechat.appid}") private String appid; @Value("${wechat.appsecret}") private String appsecret; @Value("${wechat.login_url}") private String login_url; // 授權網址 @Value("${wechat.success_login_url}") private String success_login_url; // 登入成功後返回的網頁 private WxMpService wxMpService; /** * 提示使用者點選同意,授權登入 * @return */ @GetMapping("/welcome") public String welcome() { // 1.根據appid和appsecret和回撥地址配置微信授權 WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); wxMpConfigStorage.setAppId(appid); wxMpConfigStorage.setSecret(appsecret); wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage); // 完成配置後進行跳轉 String oauth2buildAuthorizationUrl = wxMpService.oauth2buildAuthorizationUrl(login_url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null); System.out.println(oauth2buildAuthorizationUrl); return "redirect:" + oauth2buildAuthorizationUrl; } /** * 通過code拿到資料openid * @param code * @param returnUrl * @return 進行網站跳轉 */ @GetMapping("/login") public String login(@RequestParam("code") String code, @RequestParam("state") String returnUrl,HttpServletRequest request,HttpServletResponse response) { System.out.println("code=="+code); // 2.根據code換取AccessToken WxMpOAuth2AccessToken wxMpOAuth2AccessToken = null; try { wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); } catch (WxErrorException e) { e.printStackTrace(); } // 3.進一步獲取使用者資訊 String openId = wxMpOAuth2AccessToken.getOpenId(); System.out.println("openid="+openId); // 拿到使用者的基本資訊 WxMpUser wxMpUser=null; try { wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null); } catch (WxErrorException e) { e.printStackTrace(); } // 後面的內容根據需要編寫,微信授權到此已經結束了。 // 判斷這個使用者是否已經註冊 User user=newFromWechat(wxMpUser); User loginedUser = userService.registerOrLogin(user); String url_add = "user_id="+loginedUser.getUser_id()+"&portrait_url="+loginedUser.getPortrait_url(); return "redirect:" + success_login_url+"?"+url_add; } }
基本步驟也是非常簡單的,核心配置類WxMpInMemoryConfigStorage,在這個類的物件中設定appid和appsecret,然後設定一下回調地址,然後就是微信授權了。