Springboot整合微信登录

本文介绍了如何在Springboot项目中集成微信登录功能,包括所需的Maven依赖、配置YAML文件、实现Controller层处理、创建字符集工具类以及利用MyHttpUtils工具类进行HTTP请求操作。通过这些步骤,你可以构建起一个基础的微信登录系统。
摘要由CSDN通过智能技术生成

微信登录

1.所需maven依赖

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.38</version>
</dependency>

2.yml文件

XF:
  # 微信授权登录配置
  # 凭证获取GET
  wx_token_url: https://open.weixin.qq.com/connect/oauth2/authorize?
  # 用户同意授权,回调url----你的域名后面再加上/getcode
  wx_redirect_url: 回调地址
  # 获取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: 你的appid
  # 微信appSecret-----换成自己的
  wx_secret: appSecret

3.controller层

// An highlighted block
@Value("${XF.wx_token_url}")
private String wx_token_url;
@Value("${XF.wx_appid}")
private String wx_appid;
@Value("${XF.wx_redirect_url}")
private String wx_redirect_url;
@Value("${XF.wx_openid_url}")
private String wx_openid_url;
@Value("${XF.wx_secret}")
private String wx_secret;
@Value("${XF.wx_userinfo_url}")
private String wx_userinfo_url;

  /**
     * 获取Code
     * @param code Code
     * @return
     */
    @CrossOrigin(origins = "*",maxAge = 3600)//跨域
    @PostMapping(value = "/getCode")
    @ResponseBody
    public Map<String,Object> getCode(String code, HttpServletResponse response) {
   
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> p=new HashMap<String,Object>();
        // 根据Code获取Openid
        String openidUrl = wx_openid_url + "appid=" + wx_appid + "&secret=" + wx_secret + "&code=" + code + "&grant_type=authorization_code";
        String openidMsg = MyHttpUtils.doPost
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值