微信登录

1.微信登录线上操作
  1. 注册微信开放者平台
  2. 进行企业认证
  3. 创建网页应用
  4. 查看APPID
2.导入依赖
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.50</version>
 </dependency>
3.创建微信登陆配置文件
 wechat.openUrl=https://open.weixin.qq.com/connect/qrconnect
 wechat.appid=此处填写APPID
 wechat.redirectUri=http://localhost:8080/wechat/callback
 wechat.responseType=code
 wechat.scope=snsapi_login
 wechat.state=STATE#wechat_redirect
 wechat.accessTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
 wechat.secret=此处填写SECRET
 wechat.grantType=authorization_code
 wechat.userInfoUrl=https://api.weixin.qq.com/sns/userinfo
4.创建微信配置文件对象类
 package com.example.wxlogin.config;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.stereotype.Component;
 @Component
 @ConfigurationProperties(prefix = "wechat")
 @PropertySource("classpath:wxlogin.properties")
 public class WechatConfig {
     private String appid;
     private String redirectUri;
     private String responseType;
     private String scope;
     private String state;
     private String openUrl;
     private String accessTokenUrl;
     private String secret;
     private String grantType;
     private String userInfoUrl;
     // 省略getter/setter
 }
5.创建URL工具类
     package com.example.wxlogin.utils;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 public class UrlUtils {
     /**
     * 获取url网址返回的数据内容
     *
     * @param urlStr
     * @return
     */
     public static String loadURL(String urlStr) {
         try {
             URL url = new URL(urlStr);
             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
             urlConnection.setRequestMethod("GET");
             urlConnection.connect();
             InputStream inputStream = urlConnection.getInputStream();
             String responseStr = ConvertToString(inputStream);
             return responseStr;
         } catch (IOException e) {
             e.printStackTrace();
             return null;
         }
     }
     private static String ConvertToString(InputStream inputStream) {
         InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
         BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
         StringBuilder result = new StringBuilder();
         String line = null;
         try {
             while ((line = bufferedReader.readLine()) != null) {
                 result.append(line + "\n");
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             try {
                 inputStreamReader.close();
                 inputStream.close();
                 bufferedReader.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
         return result.toString();
     }
 }
6.添加微信登录控制器方法
package com.example.wxlogin.controller;
 import com.alibaba.fastjson.JSONObject;
 import com.example.wxlogin.config.WechatConfig;
 import com.example.wxlogin.utils.UrlUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 @RestController
 public class TestController {
     @Resource
     private WechatConfig wechatConfig;
     /**
     * 显示微信登陆扫码界面
     *
     * @param response
     * @throws Exception
     */
     @RequestMapping("/wechat/login")
     public void wechat(HttpServletResponse response) throws Exception {
         StringBuffer wxsb = new StringBuffer(wechatConfig.getOpenUrl());
         wxsb.append("?appid=" + wechatConfig.getAppid());
         wxsb.append("&redirect_uri=" + wechatConfig.getRedirectUri());
         wxsb.append("&response_type=" + wechatConfig.getResponseType());
         wxsb.append("&scope=" + wechatConfig.getScope());
         wxsb.append("&state=" + wechatConfig.getState());
         response.sendRedirect(wxsb.toString());
     }
     /**
     * 用户手机确认后回调函数
     *
     * @param code
     * @throws Exception
     */
     @RequestMapping("/wechat/callback")
     public Object callback(String code) throws Exception {
         // 构造请求URL
         StringBuffer wxsb = new StringBuffer(wechatConfig.getAccessTokenUrl());
         wxsb.append("?appid=" + wechatConfig.getAppid());
         wxsb.append("&secret=" + wechatConfig.getSecret());
         wxsb.append("&code=" + code);
         wxsb.append("&grant_type=" + wechatConfig.getGrantType());
         // 发送请求并获取accessToken和opendId
         String resp = UrlUtils.loadURL(wxsb.toString());
         JSONObject jsonObject = JSONObject.parseObject(resp);
         String accessToken = jsonObject.getString("access_token");
         String openId = jsonObject.getString("openid");
         // 构造获取用户信息的URL
         StringBuffer usb = new StringBuffer(wechatConfig.getUserInfoUrl());
         usb.append("?access_token=" + accessToken);
         usb.append("&openid=" + openId);
         // 发送请求并获取用户信息
         String userInfo = UrlUtils.loadURL(usb.toString());
         JSONObject userObject = JSONObject.parseObject(userInfo);
         return userObject;
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值