欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

企业微信 网页授权登入

程序员文章站 2023-12-22 16:11:28
...

企业微信网页授权登入

1:用得工具包

https://github.com/Wechat-Group/WxJava

企业微信demo(我是在这个demo上改造实现得)

https://github.com/binarywang/weixin-java-cp-demo

2:企业微信 网页授权登入

​ 下载以上企业微信demo,并进行修改启动

1:在resources文件夹中新建application.yml配置文件

logging:
    level:
        org.springframework.web: INFO
        com.github.binarywang.demo.wx.cp: DEBUG
        me.chanjar.weixin: DEBUG
wechat:
    cp:
        corpId: 111111111
        appConfigs:
            - agentId: 1000002
              secret: 2222222222
              token: 78545454545454
              aesKey: yyyyyyyy
server:
    port: 8000

如果只是为了做网页授权登入得话,配置文件中得token和aesKey是可以不用填写,你只需填写corpId,agentId,secret,这样就能启动成功了。

2: 新建一个WxOauthController

package com.github.binarywang.demo.wx.cp.controller;

import com.github.binarywang.demo.wx.cp.config.WxCpConfiguration;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Abe
 * Date: 2020/11/10 9:56
 */
@Controller
@RequestMapping("/wx")
@Slf4j
public class WxOauthController {


    /**
     * 授权入口
     * @return
     * @throws Exception
     */
    @GetMapping("")
    public Object init() throws Exception{
        // agentId 填写自建应用得id
        final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
        // 该链接就是回调地址得链接redirect_uri
        // 因为是本地测试得原因,该链接需要使用内网穿透工具得到(下面有教怎么使用内网穿透)
        String url = "http://fdsfddd.free.idcfengye.com/wx/outh";
        //url即回调地址
        String aNull = wxCpService.getOauth2Service().buildAuthorizationUrl(url, null, WxConsts.OAuth2Scope.SNSAPI_BASE);
        log.info("" + aNull);
        //注意这边需要重定向转发(这边重定向得地址就是上面的url的地址)
        return "redirect:" + aNull;
    }

    /**
     * 回调地址
     * @param code
     * @return
     * @throws Exception
     */
    @GetMapping("/outh")
    @ResponseBody
    public Object outh(String code) throws Exception{
        log.info(code + "code得值");
        final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
        // 根据code获取 访问用户身份
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        log.info(userInfo + "用户信息");
        // 根据userId获取 访问用户的详细信息
        WxCpUser user = wxCpService.getUserService().getById(userInfo.getUserId());
        log.info(user + "用户得详细信息");
        return code;
    }
}

3: 测试

使用微信开发者工具访问 http://localhost:8000/wx

查看后天打印信息,即知道是否登入成功

3:ngrok内网穿透

1: 百度搜索ngrok,进入官网进行注册登入

2: 登入成功,点击隧道管理,开通隧道,找到免费服务器,立即购买

企业微信 网页授权登入

3:填写信息

企业微信 网页授权登入

隧道协议:我这边用的是http (可以根据你的实际情况)

隧道名称:可以随意填

前置域名:服务器免费赠送的域名,请不要带上后缀,如果要 sunny.free.idcfengye.com 只需要填写 sunny 即可

本地端口:可以为同一个局域网内任意一台机器进行映射,只需要填对ip和端口就行,例如:192.168.1.1:80

http验证用户名:非必填项,在需要的时候填写,否则可以不填

http验证密码:非必填项,在需要的时候填写,否则可以不填
4:下载ngrok客户端

5: 客户端启动
企业微信 网页授权登入
点击打开启动工具,输入隧道id(隧道id在隧道管理 中可以查看),一切正常,即启动成功。

相关标签: 微信公众平台

上一篇:

下一篇: