springboot实现企业微信授权登陆

  1. 在企业微信后台中添加应用


     

     


    配置回调域
  2. 编辑
  3. 记录下应用的相关信息,在后文要用到

  4. controller
@Controller
public class QiYeWxLoginController {

    private static final String agentId = "AgentId";

    private static final String ClienSecret = "Secret";

    private static final String ClientID = "企业ID";



    private static final Logger log = LoggerFactory.getLogger(QiYeWxLoginController.class);

    @GetMapping("/index")
    public String index(ModelMap modelMap){
        modelMap.put("clientID",ClientID);
        modelMap.put("agentId",agentId);
        return "index";
    }

    /**
     * 获取扫码登录地址
     */
    @GetMapping(value = "login")
    @ResponseBody
    public Object login( @RequestParam("code") String code) throws IOException {
        try {
            Map<String,Object> params = new HashMap<>(2);
            params.put("corpid",ClientID);
            params.put("corpsecret",ClienSecret);
            String accessToken = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", params,30000);
            log.info("accessToken>>>>>>>>>>>>>" +accessToken);
            JSONObject jsonObjects = JSONObject.parseObject(accessToken);
            Integer errcodes = (Integer) jsonObjects.get("errcode");
            if(errcodes == 0){
                Map<String,Object> param = new HashMap<>(2);
                param.put("access_token",jsonObjects.getString("access_token"));
                param.put("code",code);
                String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo", param,30000);
                JSONObject jsonObject = JSON.parseObject(result);
                Integer errcode = (Integer) jsonObject.get("errcode");
                log.info("userid>>>>>>>>>>>>>" +result);
                if (errcode == 0) {
                    String userId = (String) jsonObject.get("userid");
                    Map<String,Object> paramUser = new HashMap<>(2);
                    paramUser.put("access_token",jsonObjects.getString("access_token"));
                    paramUser.put("userid",userId);
                    String userinfo = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/user/get",
                            paramUser,30000);
                    JSONObject jsonObjectUser = JSONObject.parseObject(userinfo);
                    Integer errorUsercode = (Integer) jsonObjectUser.get("errcode");
                    if(errorUsercode == 0){
                        log.info("获取用户信息>>>>>>>>>>>>>" + jsonObjectUser);
                        return jsonObjectUser;
                    }

                }
            }

            // 请求重定向
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

4.index.html

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>企业微信扫码登录demo</title>
    <!-- 生产环境版本,优化了尺寸和速度 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://wwcdn.weixin.qq.com/node/wework/wwopen/js/wwLogin-1.2.7.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            width: 100%;
            background: #fff;
        }

    </style>
</head>

<body style="background-color: #e9eaeb;">
    <div style="width: 432px;min-height: 366px;margin-top: 28px;margin: 30px auto 0;background-color: white">
        <div id="wx_reg" style="padding-top: 42px;text-align: center;overflow: hidden;min-height: 438px;">
        </div>
    </div>
</body>

<script>
    var appid = '[[${clientID}]]'
    var agentid = [[${agentId}]]
    var wwLogin = new WwLogin({
        "id": "wx_reg",
        "appid": appid, // 填你的企业微信企业id
        "agentid": agentid,// 填你的自建应用id
        "redirect_uri": "https://26d978f657.oicp.vip/login", // 填你的可信域名里的跳转链接,一定要有http或者https
        "state": "",
        "href": "",
        "lang": "zh",
    });
</script>

</html>

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于简化Spring应用程序开发的框架,它提供了快速启动和开发的能力。企业微信机器人是企业微信中的一个功能,它可以向指定的群聊或个人发送消息,用于自动化通知和交互。 要使用Spring Boot实现企业微信机器人消息,首先需要在企业微信后台获取到机器人的Webhook地址,并在Spring Boot应用程序中编写发送消息的逻辑。可以使用Spring Boot的RestTemplate来发送HTTP请求将消息发送到企业微信机器人的Webhook地址,同时也可以通过实现定时任务或事件监听来自动化消息的发送。 在Spring Boot应用程序中可以编写一个Controller来接收需要发送的消息内容,然后调用RestTemplate发送HTTP POST请求将消息内容发送给企业微信机器人。另外,还可以编写定时任务或事件监听,当满足一定条件时自动发送消息给指定的群聊或个人,实现自动化通知功能。 除了发送文本消息外,还可以通过企业微信机器人的Webhook接口发送图文消息、链接消息、Markdown消息等不同类型的消息。在Spring Boot应用程序中可以根据需求来封装不同类型消息的发送逻辑,以满足企业内部不同场景下的消息通知需求。 总的来说,通过Spring Boot实现企业微信机器人消息功能可以简化开发流程,提高开发效率,同时也可以满足企业内部自动化消息发送的需求,为企业内部的协作和通知提供便利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值