赞
踩
https://open.weixin.qq.com/
重点:微信开放平台申请应用是需要认证开发者资质。(300元)
申请资质的时候要注意:微信授权公函需要用一笔一划的正楷字去写(其实也就是一个企业的主体,主体就是对应开放平台账号的企业主体信息),不然的话,会出现审核无法通过的情况,并且只有4次机会可以申请。
个人无法做测试,如果需要使用,要么自己有公司或者使用其他公司的信息作资质认证。
注意:其实开放平台可以绑定很多应用,比如移动APP、网站、公众号、或者其他第三方平台,但是这里只讲网站应用实现微信开放平台的授权登录。
认证通过后切换到“网站应用”标签页,点击“创建网站应用”按钮
创建完成后需要等待审核,大概1-3个工作日内就会通过审核,审核通过后,就能看到网站对应的信息。
点击“查看”即可看到oauth:AppId、AppSecret、授权回调域,分别对应了JustAuth的 Client ID、Client Secret、 Callback URL
注意:
”授权回调域“填写对应域名即可,比如我要给http://www.xxxx.cn/增加扫码登录功能,那么我只要在”授权回调域“中填写www.xxxx.cn即可,那么我在程序中配置的回调地址为:https://www.xxxx.cn/oauth/callback/wechat
一定要确保应用详情页中的”接口信息“中的”微信登录“接口的状态为”以获得“。
”应用密钥“要确保不要泄露。
第四步:集成JustAuth,引入Jar包或者依赖
1、当项目为Maven项目,在pom.xml中引入以下依赖
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${latest.version}</version>
</dependency>
2、项目为一般的WEB项目
在Maven仓库中下载JustAuth的jar包
原Maven仓库:https://mvnrepository.com/
Maven阿里巴巴的国内镜像仓库https://maven.aliyun.com/
simple-http
httpclient
httpcore
1、创建request
AuthRequest authRequest = new AuthWeChatRequest(AuthConfig.builder() .clientId("Client ID") .clientSecret("Client Secret") .redirectUri("https://www.xxxx.cn/oauth/callback/wechat") .build());
2、生成授权地址
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
拿到地址链接我们可以在后端重定向,也可以先返回前端,前端控制跳转,前端控制的好处是可以将第三方授权页嵌套到登录页面上,适配网站的设计。
3、完整 Controller代码
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.request.AuthWeChatOpenRequest; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.request.AuthRequest; import me.zhyd.oauth.utils.AuthStateUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse;import java.io.IOException; @RestController @RequestMapping("/oauth") public class RestAuthController { @RequestMapping("/render") public void renderAuth(String source, HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(); response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } @RequestMapping("/callback") public Object login(String source, AuthCallback callback) { AuthUser authUser = null; AuthRequest authRequest = getAuthRequest(); AuthResponse response = authRequest.login(callback); if(response.getData() instanceof AuthUser) { authUser = (AuthUser)response.getData(); //插入数据 } return authUser; } private AuthRequest getAuthRequest(String source) { String clientId = Global.getProperty("oauth2." + source + ".clientId"); if (StringUtils.isBlank(clientId)) { throw new AuthException("非法操作,未知未授权类型:" + source); } AuthConfig.AuthConfigBuilder config = AuthConfig.builder().clientId(clientId); config.clientSecret(Global.getProperty("oauth2." + source + ".clientSecret")); config.redirectUri(Global.getProperty("oauth2." + source + ".redirectUri")); AuthRequest authRequest = null; switch (source) { case "gitee": authRequest = new AuthGiteeRequest(config.build()); break; case "qq": authRequest = new AuthQqRequest(config.build()); break; case "weixin": authRequest = new AuthWeChatOpenRequest(config.build()); break; case "weixin_mp": authRequest = new AuthWeChatMpRequest(config.build()); break; case "weixin_qy": authRequest = new AuthWeChatEnterpriseRequest(config.build()); break; case "ding_talk": authRequest = new AuthDingTalkRequest(config.build()); } return authRequest; } }
4、创建第三授权登录表
CREATE TABLE `oauth2_user` ( `id` varchar(64) NOT NULL COMMENT '编号', `user_source` varchar(100) DEFAULT NULL COMMENT '用户来源', `user_uuid` varchar(128) DEFAULT NULL COMMENT '第三方用户ID', `username` varchar(200) DEFAULT NULL COMMENT '用户名', `nickname` varchar(200) DEFAULT NULL COMMENT '用户昵称', `avatar` varchar(500) DEFAULT NULL COMMENT '用户头像', `blog` varchar(500) DEFAULT NULL COMMENT '用户网址', `company` varchar(200) DEFAULT NULL COMMENT '所在公司', `location` varchar(200) DEFAULT NULL COMMENT '所在位置', `email` varchar(500) DEFAULT NULL COMMENT '用户邮箱', `remark` varchar(500) DEFAULT NULL COMMENT '用户备注', `gender` varchar(10) DEFAULT NULL COMMENT '用户性别', `create_date` datetime DEFAULT NULL COMMENT '创建时间', `update_date` datetime DEFAULT NULL COMMENT '更新时间', `user_code` varchar(64) DEFAULT NULL COMMENT '绑定用户编码', PRIMARY KEY (`id`), KEY `idx_sys_oauth2_user_uuid` (`user_uuid`), KEY `idx_sys_oauth2_user_sou` (`user_source`), KEY `idx_sys_oauth2_user_uc` (`user_code`), KEY `idx_sys_oauth2_user_cd` (`create_date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='第三方授权登录';
最后重要的事情说三遍:开发的时候要看文档!!!开发的时候要看文档!!!开发的时候要看文档!!!
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。