前言
在如今的数字化时代,用户体验至关重要。微信作为中国最大的社交平台之一,其扫码登录功能为用户提供了便捷且安全的登录方式。作为开发者,我们可以通过JustAuth和Spring Boot轻松实现这一功能,为我们的应用带来更好的用户体验。接下来,让我们一起探索这条便捷之路,轻松实现微信扫码登录。
前提
注意📢:使用此方式进行扫码登录的前提是注册了微信公众平台(企业)
springboot整合微信(公众号)实现扫码登录(两种方式,两种实现)
什么是JustAuth
JustAuth 是一个开源的第三方登录授权 SDK,用于简化和统一多个第三方平台(如微博、微信、GitHub、Google 等)的 OAuth 2.0 授权登录流程。它旨在通过提供一致的 API 和便捷的集成方式,使开发者能够快速实现多平台的第三方登录功能。
JustAuth官网
JustAuth 的特点
-
支持多平台:JustAuth 支持常见的社交平台和一些开发者平台的 OAuth 2.0 授权登录,包括但不限于微博、微信、QQ、GitHub、Google、Facebook、Twitter 等。
-
统一接口:通过统一的接口,开发者可以方便地集成多个第三方平台的授权登录功能,无需针对不同平台编写不同的代码。
-
简单易用:JustAuth 提供了简洁明了的 API,使得第三方登录的实现变得非常简单,只需几行代码即可完成集成。
-
开源免费:JustAuth 是一个开源项目,开发者可以免费使用和修改源代码,根据自己的需求进行定制化开发。
JustAuth 支持的第三方平台
JustAuth 支持的第三方平台包括但不限于:
-
GitHub -
Google -
Facebook -
Twitter -
微博 -
微信 -
QQ -
淘宝 -
支付宝 -
钉钉
逻辑图

项目实现
下面代码仅为基础代码,需要考虑自身业务,比如登录前要绑定平台用户
maven实现
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.4.0</version>
</dependency>
配置文件实现
justauth:
wechat:
client-id: your-wechat-appid
client-secret: your-wechat-appsecret
redirect-uri: http://your-domain.com/callback
# 这里有一个参数为ignoreCheckRedirectUri,设置为true
创建配置类
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthWeChatRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JustAuthConfig {
@Value("${justauth.wechat.client-id}")
private String clientId;
@Value("${justauth.wechat.client-secret}")
private String clientSecret;
@Value("${justauth.wechat.redirect-uri}")
private String redirectUri;
@Bean
public AuthWeChatRequest authWeChatRequest() {
return new AuthWeChatRequest(AuthConfig.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.redirectUri(redirectUri)
.build());
}
}
controller
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthWeChatRequest;
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;
@Controller
@RequestMapping("/oauth")
public class OAuthController {
@Autowired
private AuthWeChatRequest authWeChatRequest;
@GetMapping("/wechat")
public String renderAuth() {
return "redirect:" + authWeChatRequest.authorize();
}
@RequestMapping("/callback")
@ResponseBody
public AuthResponse<AuthUser> login(AuthCallback callback) {
return authWeChatRequest.login(callback);
}
}
常用方法解释
// 返回微信用户信息
AuthResponse login = authWeChatRequest.login(authCallback);
AuthUser user = (AuthUser) login.getData();
// 用户详细信息
JSONObject rawUserInfo = user.getRawUserInfo();
// 获取openid
user.getUuid()
// 获取unionid
rawUserInfo.getString("unionid")
原文始发于微信公众号(一只牛博):微信公众平台扫码登录:Spring Boot与JustAuth的完美结合
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/299243.html