admin管理员组文章数量:1559102
前言
本文主要实现目的目标需求为:企业微信网页授权登录
借鉴于:企业微信官方文档网页授权登录
概述
企业微信提供了 oauth 的授权登录方式,可以让从企业微信终端打开的网页获取成员的身份信息,从而免去登录的环节。
企业应用中的 url 链接(包括自定义菜单或者消息中的链接),均可通过 oauth2.0 验证接口来获取成员的 userid 身份信息。
使用前说明
关于配置可信域名可以参考我的第一篇文章:企业微信开发:自建应用配置可信域名
oauth2.0接入流程说明
请求连接:
https://open.weixin.qq/connect/oauth2/authorize?appid=corpid&redirect_uri=redirect_uri&response_type=code&scope=scope&agentid=agentid&state=state#wechat_redirect
参数说明:
参数 | 必须 | 说明 |
---|---|---|
appid | 是 | 企业的corpid |
redirect_uri | 是 | 授权后重定向的回调链接地址,请使用 urlencode 对链接进行处理 |
response_type | 是 | 返回类型,此时固定为:code |
scope | 是 | 应用授权作用域。 snsapi_base:静默授权,可获取成员的的基础信息(userid与deviceid); snsapi_userinfo:静默授权,可获取成员的详细信息,但不包含手机、邮箱; snsapi_privateinfo:手动授权,可获取成员的详细信息,包含手机、邮箱 注意:企业自建应用可以根据userid获取成员详情,无需使用snsapi_userinfo和snsapi_privateinfo两种scope。 |
agentid | 是 | 企业应用的id。 当scope是snsapi_userinfo或snsapi_privateinfo时,该参数必填 注意redirect_uri的域名必须与该应用的可信域名一致。 |
state | 是 | 重定向后会带上state参数,企业可以填写a-za-z0-9的参数值,长度不可超过128个字节 |
#wechat_redirect | 是 | 终端使用此参数判断是否需要带上身份信息 |
注意事项:
跳转的域名须完全匹配 access_token 对应应用的可信域名,否则会返回 50001 错误
代码片段
获取 code 信息,也就是我们登录是需要调用的方法,例如:http://localhost:端口号/项目名称/oauth2
// 定义变量
private string corpid = "xxxxxx";
/*企业微信 oauth2 认证*/
@requestmapping("/oauth2")
public void oauth2(httpservletresponse response) throws ioexception {
string url = oauth2api.getoauth2;
//重定向请求
response.sendredirect(url);
return;
}
工具类:对应上方 getoauth2url 方法
/**
* @classname:oauth2 api
* @description:todo
* @author linluochen
* @date 2020/8/13/15:55
* @version v1.0
**/
public class oauth2api {
// 获取 oauth2 api
private final static string qy_oauth2_url = "https://open.weixin.qq/connect/oauth2/authorize?appid=corpid&redirect_uri=redirect_uri&response_type=code&scope=scope#wechat_redirect";
// 获取 oauth2 api
public static string getoauth2{
// 替换回调地址
return qy_oauth2_url.replaceall("corpid", corpid).replace("redirect_uri","跳转到你配置的可信域名的地方").replace("scope","snsapi_base");
}
}
回调方法:执行成功之后回调条转的地址,用来接收 code 值
/*企业微信进入回调*/
@requestmapping("/getusercode")
public string getusercode(httpservletrequest request, httpservletresponse response, httpsession session, model model) throws ioexception, wxerrorexception {
// 获取 code 值
string code = request.getparameter("code");
}
这样就可以成功的拿到我们的 code 参数了
j9九游会老哥俱乐部交流区的版权声明:本文标题:企业微信开发:网页授权登录(三) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1727387449a1112420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。