微信公众号网页授权步骤详细步骤介绍和整合springboot开发

微信公众号网页授权(springboot)

第一步在微信公众平台中找到网页授权,若没有微信公众号,可以用测试号来进行

官方链接: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
在这里插入图片描述
其中Token可以随便写,可能一开始配置的URL无法配置成功,别着急,按照我下面的步骤操作

在“网页授权获取用户基本信息”那一栏,点击修改
在这里插入图片描述

然后再填写授权回调页面,这里的页面如同手册里面一样 以xxx.xxxxx.com 即可 而且此处的域名一定要和代码中回调地址在同一个域名之下。自己测试可以去找花生壳等申请一个域名做内网穿透,详情请自己百度.

[不要包含http://或https://] 比如在下面填写的网址是在花生壳中申请的: http://abc123.ticp.io, 那么在下面应该填写abc123.ticp.io, 这个url在接下来的代码中会用到。
在这里插入图片描述

springboot整合微信公众号授权

1. 依赖pom.xml
<!--微信授权-->
<dependency>
	   <groupId>com.github.binarywang</groupId>
	   <artifactId>weixin-java-mp</artifactId>
	   <version>2.9.0</version>
</dependency>
2. application.yml
wechat:
  appId: xxxxxxxxxxxxxxx
  appSecret: xxxxxxxxxxxxxxxxxxxxxxxxx
3. 加载配置文件,获取appId和appSecret
package com.wechat.auth;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

    private String appId;

    private String appSecret;
}
package com.wechat.auth;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class WeChatMpConfig {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getAppId());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getAppSecret());
        return wxMpConfigStorage;
    }

}
4.controller授权
package com.wechat.auth;

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.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.net.URLEncoder;

@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {

    @Autowired
    private WxMpService wxMpService;

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl) {
        String url = "http://abc123.ticp.io/wechat/userInfo";   //可以看到,这里的url即为上面申请并授权的域名
        String redirectURL = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
        log.info("微信网页授权:获取code,redirectURL={}", redirectURL);
        return "redirect:" + redirectURL;
    }

    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) throws Exception {
        log.info("微信网页授权: code={}", code);
        log.info("微信网页授权: state={}",returnUrl);	//即authorize中的returnUrl
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        }catch (WxErrorException e) {
            log.info("微信网页授权: {}", e);
            throw new Exception(e.getError().getErrorMsg());
        }
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("微信网页授权: openId={}", openId);
        return "redirect:" + returnUrl;
    }
}


5.以下功能可根据需求来用
  • 获得用户基本信息
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
  • 刷新access token
wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
  • 验证access token
boolean valid = wxMpService.oauth2validateAccessToken(wxMpOAuth2AccessToken);
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信公众号网页授权是指用户在微信客户端内访问第三方网页时,第三方网页可以通过微信授权获取用户基本信息的功能。下面是实现微信公众号网页授权的代码开发步骤: 1. 准备工作 在微信公众平台上,开发者需要先获取到自己的公众号AppID和AppSecret,并且在公众号后台设置授权回调域名。 2. 获取授权链接 在第三方网页中,用户点击授权按钮后,需要跳转到微信授权面,获取用户授权。在获取授权链接时,需要拼接以下参数: ``` appid:公众号AppID redirect_uri:授权回调面 response_type:固定值code scope:snsapi_base或snsapi_userinfo state:可以是任意字符串,用于维护请求状态,防止CSRF攻击 ``` 其中,scope有两个取值: - snsapi_base:静默授权,只能获取到用户的openid。 - snsapi_userinfo:需要用户手动同意授权,可以获取到用户的基本信息。 拼接完参数后,通过urlEncode进行编码,得到授权链接。 3. 用户授权 用户跳转到微信授权面后,需要手动同意授权授权成功后,微信会将code参数传回授权回调面。 4. 获取access_token和openid 通过code参数获取access_token和openid。拼接以下链接,使用GET方法请求即可: ``` https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code ``` 其中,APPID和APPSECRET为公众号的AppID和AppSecret,CODE为授权回调面传回的code值。 5. 获取用户信息 如果scope取值为snsapi_userinfo,可以通过access_token和openid获取用户基本信息。拼接以下链接,使用GET方法请求即可: ``` https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN ``` 其中,ACCESS_TOKEN为上一步获取的access_token,OPENID为上一步获取的openid。 以上就是微信公众号网页授权的代码开发步骤

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值