微信扫码登录

解决网址微信扫码登录

文章目录

一、添加配置

二、代码

1.ConstantWxUtils

2.WxApiController

总结


一、添加配置

application.properties。

wx.open.app_id=微信开发平台APPid
wx.open.app_secret=微信开发平台APPsecret
wx.open.redirect_url=回调地址

二、代码

1.ConstantWxUtils

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

@Component
@PropertySource("classpath:application.properties") //读取配置文件
@ConfigurationProperties(prefix="wx.open") //读取wx.open节点
@Data
public class ConstantWxUtils{

    private String appId;

    private String appSecret;

    private String redirectUrl;

}

2.WxApiController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller //这里不能用RestController,会重定向失败
@RequestMapping("/api/wx")
public class WxApiController {

    @Resource
    private ConstantWxUtils utils;

    @GetMapping("login")
    public String getWxCode(){
        //使用 urlEncode 对链接进行处理
        String redirectUri = utils.getRedirectUrl();
        try {
            redirectUri = URLEncoder.encode(redirectUri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //根据%s来替换参数
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect?appId=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect";
        String url = String.format(
                baseUrl,
                utils.getAppId(),
                redirectUri,
                "随便你填的数"
        );
        //redirect重定向到指定地址
        return "redirect:"+url;
    }

    //这个就是回调地址
    @GetMapping("callback")
    public String callback(String code,String state){
        //拼接请求URL
            String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
                    "?appid=%s" +
                    "&secret=%s" +
                    "&code=%s" +
                    "&grant_type=authorization_code";

            String accessTokenUrl = String.format(
                    baseAccessTokenUrl,
                    utils.getAppId(),
                    utils.getAppSecret(),
                    code
            );
            //使用httpclient发送请求,获取accsess_token和openid
            String accessTokenInfo = HttpClientUtils.get(accessTokenUrl);
            Gson gson = new Gson();
            HashMap mapAccessToken = gson.fromJson(accessTokenInfo, HashMap.class);
            String access_token = mapAccessToken.get("access_token").toString();
            String openid = mapAccessToken.get("openid").toString();
            //判断用户是否存在
            QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
            wrapper.eq("openid",openid);
            UcenterMember member = memberService.getOne(wrapper);
            if(ObjectUtils.isEmpty(member)){
                //不存在,则拼接URL,请求用户信息
                String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
                        "?access_token=%s" +
                        "&openid=%s";
                String userInfoUrl = String.format(
                        baseUserInfoUrl,
                        access_token,
                        openid
                );
                String userInfo = HttpClientUtils.get(userInfoUrl);
                HashMap userInfoMap = gson.fromJson(userInfo, HashMap.class);
                String nickname = userInfoMap.get("nickname").toString();//昵称
                String headimgurl = userInfoMap.get("headimgurl").toString();//头像
                Integer sex = (Integer) userInfoMap.get("sex");//性别
                //保存用户信息
                member = new UcenterMember();
                member.setOpenid(openid);
                member.setNickname(nickname);
                member.setAvatar(headimgurl);
                member.setSex(sex);
                memberService.save(member);
            }
            //把用户信息加密,你们自己写一个加密和解密的接口
            String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
            //这边改成你的前端地址就好,把加密的信息发送的前端页面的路径上,我这里是前端获取到加密信息调用我的解密接口解密,再得到用户信息
            return "redirect:https://baidu.com?token="+jwtToken;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PKException(500,"登录失败");
        }
    }
}

总结

里面使用jwt的部分根据自己验证用户的流程修改即可。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

予风北

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值