微信OAuth.授权登录让微信用户使用微信身份安全登录第三方应用或网站,在微信用户授权登录已接入微信OAuth.的第三方应用后,第三方可以获取到用户的接口调用凭证(access_token),通过access_token可以进行微信开放平台授权关系接口调用,从而可实现获取微信用户基本开放信息和帮助用户实现基础开放功能等。
微信OAuth.授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:
① 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
② 通过code参数加上AppID和AppSecret等,通过API换取access_token;
③ 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
第三方使用网站应用授权登录前请注意已获取相应网页授权作用域(scope=snsapi_login),则可以通过在PC端打开以下链接:
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
返回说明
用户允许授权后,将会重定向到redirect_uri的网址上,并且带上code和state参数
redirect_uri?code=CODE&state=STATE
若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数
redirect_uri?state=STATE
例如:登录一号店网站应用
https://passport.yhd.com/wechat/login.do 打开后,一号店会生成state参数,跳转到
https://open.weixin.qq.com/connect/qrconnect?appid=wxbdcccc&redirect_uri=
https%A%F%Fpassport.yhd.com%Fwechat%Fcallback.do&response_type=code&scope=snsapi_login&state=
dbeadbae#wechat_redirect 微信用户使用微信扫描二维码并且确认登录后,PC端会跳转到
https://passport.yhd.com/wechat/callback.do?code=CODE&state=
dbeadbae
通过code获取access_token
https://api.weixin.qq.com/sns/oauth/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
返回说明
正确的返回:
{ "access_token":"ACCESS_TOKEN", "expires_in":, "refresh_token":"REFRESH_TOKEN","openid":"OPENID", "scope":"SCOPE","unionid": "o_bmasdasdsad_sgVthMZOPfL"}
错误返回样例:
{"errcode":,"errmsg":"invalid code"}
Appsecret 是应用接口使用密钥,泄漏后将可能导致应用数据泄漏、应用的用户数据泄漏等高风险后果;存储在客户端,极有可能被恶意窃取(如反编译获取Appsecret);
access_token 为用户授权第三方应用发起接口调用的凭证(相当于用户登录态),存储在客户端,可能出现恶意获取access_token 后导致的用户数据泄漏、用户微信相关接口功能被恶意发起等行为;
refresh_token 为用户授权第三方应用的长效凭证,仅用于刷新access_token,但泄漏后相当于access_token 泄漏,风险同上。
建议将secret、用户数据(如access_token)放在App云端服务器,由云端中转接口调用请求。
学习资料:Java进阶视频资源
获取access_token后,进行接口调用,有以下前提:
access_token有效且未超时;
微信用户已授权给第三方应用帐号相应接口作用域(scope)。
对于接口作用域(scope),能调用的接口有以下:
因为微信开放平台的AppiD和APPSecret和微信公众平台的AppiD和AppSecret都是不同的,因此需要配置一下:
# 开放平台wechat.open-app-id=wxadeafdwechat.open-app-secret=affdabbccfbefee
@Data@Component@ConfigurationProperties(prefix = "wechat") public class WechatAccountConfig { //公众号appid private String mpAppId; //公众号appSecret private String mpAppSecret; //商户号 private String mchId; //商户秘钥 private String mchKey; //商户证书路径 private String keyPath; //微信支付异步通知 private String notifyUrl; //开放平台id private String openAppId; //开放平台秘钥 private String openAppSecret; }
@Configurationpublic class WechatOpenConfig { @Autowired private WechatAccountConfig accountConfig; @Bean public WxMpService wxOpenService() { WxMpService wxOpenService = new WxMpServiceImpl(); wxOpenService.setWxMpConfigStorage(wxOpenConfigStorage()); return wxOpenService; } @Bean public WxMpConfigStorage wxOpenConfigStorage() { WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage(); wxMpInMemoryConfigStorage.setAppId(accountConfig.getOpenAppId()); wxMpInMemoryConfigStorage.setSecret(accountConfig.getOpenAppSecret()); return wxMpInMemoryConfigStorage; } }
@Controller@RequestMapping("/wechat")@Slfjpublic class WeChatController { @Autowired private WxMpService wxMpService; @Autowired private WxMpService wxOpenService; @GetMapping("/qrAuthorize") public String qrAuthorize() { //returnUrl就是用户授权同意后回调的地址 String returnUrl = "http://heng.nat.top/sell/wechat/qrUserInfo"; //引导用户访问这个链接,进行授权 String url = wxOpenService.buildQrConnectUrl(returnUrl, WxConsts.QRCONNECT_SCOPE_SNSAPI_LOGIN, URLEncoder.encode(returnUrl)); return "redirect:" + url; } //用户授权同意后回调的地址,从请求参数中获取code @GetMapping("/qrUserInfo") public String qrUserInfo(@RequestParam("code") String code) { WxMpOAuthAccessToken wxMpOAuthAccessToken = new WxMpOAuthAccessToken(); try { //通过code获取access_token wxMpOAuthAccessToken = wxOpenService.oauthgetAccessToken(code); } catch (WxErrorException e) { log.error("【微信网页授权】{}", e); throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } //从token中获取openid String openId = wxMpOAuthAccessToken.getOpenId(); //这个地址可有可无,反正只是为了拿到openid,但是如果没有会报错误,为了好看随便返回一个百度的地址 String returnUrl = "http://www.baidu.com"; log.info("openid={}", openId); return "redirect:" + returnUrl + "?openid="+openId; } }
请求路径:在浏览器打开
https://open.weixin.qq.com/connect/qrconnect?appid=wxadeafd&redirect_uri=http%A%F%Fsell.springboot.cn%Fsell%Fqr%FoTgZpwenClwOeTDDf_-UYyFtqI&response_type=code&scope=snsapi_login&state=http%a%f%fheng.nat.top%fsell%fwechat%fqrUserInfo
获取了openid:openid=
oAREvXrZUkBtVqwbbAFk
@Controller@RequestMapping("/seller")public class SellerUserController { @Autowired private SellerService sellerService; @Autowired private StringRedisTemplate redisTemplate; @Autowired private ProjectUrlConfig projectUrlConfig; @GetMapping("/login") public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response, Map<String, Object> map) { //. openid去和数据库里的数据匹配 SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid); if (sellerInfo == null) { map.put("msg", ResultEnum.LOGIN_FAIL.getMessage()); map.put("url", "/sell/seller/order/list"); return new ModelAndView("common/error"); } //. 设置token至redis String token = UUID.randomUUID().toString(); //设置token的过期时间 Integer expire = RedisConstant.EXPIRE; redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS); //. 设置token至cookie CookieUtil.set(response, CookieConstant.TOKEN, token, expire); return new ModelAndView("redirect:" + "http://heng.nat.top/sell/seller/order/list"); } @GetMapping("/logout") public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) { //. 从cookie里查询 Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN); if (cookie != null) { //. 清除redis redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue())); //. 清除cookie CookieUtil.set(response, CookieConstant.TOKEN, null, ); } map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage()); map.put("url", "/sell/seller/order/list"); return new ModelAndView("common/success", map); } }
① 将上一步获取到的openid存入数据库
② 将授权后跳转的地址改为登录地址
//用户授权同意后回调的地址,从请求参数中获取code @GetMapping("/qrUserInfo") public String qrUserInfo(@RequestParam("code") String code) { WxMpOAuthAccessToken wxMpOAuthAccessToken = new WxMpOAuthAccessToken(); try { //通过code获取access_token wxMpOAuthAccessToken = wxOpenService.oauthgetAccessToken(code); } catch (WxErrorException e) { log.error("【微信网页授权】{}", e); throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } //从token中获取openid String openId = wxMpOAuthAccessToken.getOpenId(); //授权成功后跳转到卖家系统的登录地址 String returnUrl = "http://heng.nat.top/sell/seller/login"; log.info("openid={}", openId); return "redirect:" + returnUrl + "?openid="+openId; }
③ 在浏览器请求这个链接:
https://open.weixin.qq.com/connect/qrconnect?appid=wxadeafd&redirect_uri=
http%A%F%Fsell.springboot.cn%Fsell%Fqr%FoTgZpwenClwOeTDDf_-UYyFtqI&response_type=code&scope=snsapi_login&state=
http%a%f%fheng.nat.top%fsell%fwechat%fqrUserInfo
第三应用请求使用微信扫码登录,而不是使用本网站的密码:
用户同意授权后登入第三方应用的后台管理系统:
@Aspect@Component@Slfjpublic class SellerAuthorizeAspect { @Autowired private StringRedisTemplate redisTemplate; @Pointcut("execution(public * com.hh.controller.Seller*.*(..))" + "&& !execution(public * com.hh.controller.SellerUserController.*(..))") public void verify() {} @Before("verify()") public void doVerify() { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //查询cookie Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN); //如果cookie中没有token说明已经登出或者根本没有登录 if (cookie == null) { log.warn("【登录校验】Cookie中查不到token"); //校验不通过,抛出异常 throw new SellerAuthorizeException(); } //去redis里查询 String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue())); //如果redis中没有对应的openid,同样表示登出或者根本没有登录 if (StringUtils.isEmpty(tokenValue)) { log.warn("【登录校验】Redis中查不到token"); throw new SellerAuthorizeException(); } } }
拦截及登录校验不通过的异常,让其跳转到登录页面,扫码登录
@ControllerAdvicepublic class SellExceptionHandler { //拦截登录异常 @ExceptionHandler(value = SellerAuthorizeException.class) public ModelAndView handlerAuthorizeException() { //拦截异常后,跳转到登录界面 return new ModelAndView("redirect:".concat("https://open.weixin.qq.com/connect/qrconnect?" + "appid=wxadeafd" + "&redirect_uri=http%A%F%Fsell.springboot.cn%Fsell%Fqr%F" + "oTgZpwenClwOeTDDf_-UYyFtqI" + "&response_type=code&scope=snsapi_login" + "&state=http%a%f%fheng.nat.top%fsell%fwechat%fqrUserInfo")); } @ExceptionHandler(value = SellException.class) @ResponseBody public ResultVO handlerSellerException(SellException e) { return ResultVOUtil.error(e.getCode(), e.getMessage()); } @ExceptionHandler(value = ResponseBankException.class) @ResponseStatus(HttpStatus.FORBIDDEN) public void handleResponseBankException() { } }
来源:
hengheng.blog.csdn.net/article/details/