SpringBoot2.0 (IX): Implement micro-channel user authorized to log on and get information

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xiaofeivip_top/article/details/88724413

Step 1: Configure domain

Since I am a local test, you need a domain name mapping tool, change the tool very simple to use, if they have a domain name you can use your own domain name (domain name must own the record) if not you can enter your own domain name prefix in the first input box inside! Micro-channel authorization must login port 80, and then click either the background map!
Here Insert Picture Description
Tools Download: point I downloaded
Here is how to configure the domain name, the domain name needs to complete the configuration after a MP_verify_xK4DtJdHxlgIHIju.txt into the project root directory

/**
  * 配置微信文件
  */
 @GetMapping(value = "/MP_verify_xK4DtJdHxlgIHIju.txt")
 @ResponseBody
 public String config() {
     return "xK4DtJdHxlgIHIju121";
 };

Specific configuration domain view the following connection:
https://jingyan.baidu.com/article/3c48dd34a2e52ee10be358f0.html

Step Two: Configure yml file

# 项目相关配置
XF:
  # 微信授权登录配置
  # 凭证获取GET
  wx_token_url: https://open.weixin.qq.com/connect/oauth2/authorize?
  # 用户同意授权,回调url----你的域名后面再加上/getcode
  wx_redirect_url: XXXX.XXX/getcode
  # 获取openid
  wx_openid_url: https://api.weixin.qq.com/sns/oauth2/access_token?
  # 拉取用户信息
  wx_userinfo_url: https://api.weixin.qq.com/sns/userinfo?
  # 微信appid-----换成自己的
  wx_appid: XXXXX
  # 微信appSecret-----换成自己的
  wx_secret: XXXX

The third step: Jump micro-channel acquisition code and

@GetMapping(value = "/wxLogin")
    public String get() {
        String url = wx_token_url + "appid=" + wx_appid + "&redirect_uri=" + wx_redirect_url + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redi";
        return "redirect:" + url;
    }

Step Four: Jump micro-channel acquisition code and


Importing Ali cloud JSON

<!-- 阿里云json -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.47</version>
</dependency>

Which used the doPost () method, please click here to go to copy;
Code:

   /**
     * 获取Code
     * @param code Code
     * @return
     */
    @RequestMapping(value = "/getcode")
    @ResponseBody
    public String getCode(String code) {
        // 根据Code获取Openid
        String openidUrl = wx_openid_url + "appid=" + wx_appid + "&secret=" + wx_secret + "&code=" + code + "&grant_type=authorization_code";
        String openidMsg = MyHttpUtils.doPost(openidUrl, "", CharsetKit.UTF_8);
        // 解析返回信息
        JSONObject result = JSON.parseObject(openidUrl);
        // 网页授权接口调用凭证
        String access_token = result.getString("access_token");
        // 用户刷新access_token
        String refresh_token = result.getString("refresh_token");
        // 用户唯一标识
        String openid = result.getString("openid");

        System.err.println("code:" + code);
        System.err.println("网页授权接口调用凭证:" + access_token);
        System.err.println("用户刷新access_token:" + refresh_token);
        System.err.println("用户唯一标识:" + openid);

        // 拉取用户信息
        String userInfoUrl = wx_userinfo_url + "access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
        String userInfoMsg = MyHttpUtils.doPost(userInfoUrl, "", CharsetKit.UTF_8);
        // 解析返回信息
        JSONObject userInfo = JSON.parseObject(userInfoMsg);

        System.err.println("用户openid:" + userInfo.getString("openid"));
        System.err.println("名字:" + userInfo.getString("nickname"));
        // 值为1时是男性,值为2时是女性,值为0时是未知
        System.err.println("性别:" + userInfo.getString("sex"));
        System.err.println("省份:" + userInfo.getString("province"));
        System.err.println("城市:" + userInfo.getString("city"));
        System.err.println("国家:" + userInfo.getString("country"));
        System.err.println("头像:" + userInfo.getString("headimgurl"));
        System.err.println("特权:" + userInfo.getString("privilege"));
        System.err.println("unionid:" + userInfo.getString("unionid"));
        return code;
    }

The official document: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

Welcome to join the Java mutual exchange group: 470 765 097

Guess you like

Origin blog.csdn.net/xiaofeivip_top/article/details/88724413