【微信扫码登录】生成授权URL

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


# 微信开放平台 appid
wx.open.app_id=你的appid
# 微信开放平台 appsecret
wx.open.app_secret=你的appsecret
# 微信开放平台 重定向url
wx.open.redirect_url=http://你的服务器名称/api/ucenter/wx/callback

在这里插入图片描述

@Component
//@PropertySource("classpath:application.properties")

public class ConstantPropertiesUtil implements InitializingBean {


    @Value("${wx.open.app_id}")
    private String appId;


    @Value("${wx.open.app_secret}")
    private String appSecret;

    @Value("${wx.open.redirect_url}")
    private String redirectUrl;

    public static String WX_OPEN_APP_ID;

    public static String WX_OPEN_APP_SECRET;

    public static String WX_OPEN_REDIRECT_URL;


    @Override
    public void afterPropertiesSet() throws Exception {

        WX_OPEN_APP_ID = appId;

        WX_OPEN_APP_SECRET = appSecret;

        WX_OPEN_REDIRECT_URL = redirectUrl;

    }

}

在这里插入图片描述


@CrossOrigin
@Controller//注意这里没有配置 @RestController
@RequestMapping("/api/ucenter/wx")
public class WxApiController {


    @GetMapping("login")
    public String genQrConnect(HttpSession session) {


        // 微信开放平台授权baseUrl

        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 redirectUrl = ConstantPropertiesUtil.WX_OPEN_REDIRECT_URL; //获取业务服务器重定向地址

        try {

            redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url编码

        } catch (UnsupportedEncodingException e) {

            throw new GuliException(20001, e.getMessage());

        }

        // 防止csrf攻击(跨站请求伪造攻击)

        //String state = UUID.randomUUID().toString().replaceAll("-", "");//一般情况下会使用一个随机数

        String state = "imhelen";//为了让大家能够使用我搭建的外网的微信回调跳转服务器,这里填写你在ngrok的前置域名

        System.out.println("state = " + state);

        // 采用redis等进行缓存state 使用sessionId为key 30分钟后过期,可配置

        //键:"wechar-open-state-" + httpServletRequest.getSession().getId()

        //值:satte

        //过期时间:30分钟

        

        //生成qrcodeUrl

        String qrcodeUrl = String.format(

                baseUrl,

                ConstantPropertiesUtil.WX_OPEN_APP_ID,

                redirectUrl,

                state);


        return "redirect:" + qrcodeUrl;

    }

}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr_树先森

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

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

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

打赏作者

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

抵扣说明:

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

余额充值