使用JustAuth 生成 qq 登录地址,用于网站跳转到qq登录,进行第三方登录
使用场景:
当网站需要加入第三方登录的时候, 使用 JustAuth 可以提高开发速度.
使用步骤:
1. 导入依赖
<!--JustAuth第三方登录模块-->
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.13.1</version>
</dependency>
2. 配置application.yml
#============================第三方登录配置信息=================================== qq: app-id: 101995894 app-secret: 238689485c7d930b7da82545505471aa redirect-url: http://www.shiyit.com/shiyi/oauth/callback/qq weibo: app-id: 2285022919 app-secret: a380fef0743e38cad12125902b204fab redirect-url: http://www.shiyit.com/callback/weibo
3. 编写工具类,能够获取第三方登录的url;返回给前端,通过window.open进行页面打开,window.open函数有三个参数,可自行查阅,达到自己想要的结果。
public class JustAuthUtils {
public static String getAuthUrl(QqConfigProperties qqProperties,String source) {
//1. 获取AuthRequest
AuthRequest authRequest = getAuthRequest( qqProperties,source);
String authUrl = authRequest.authorize(AuthStateUtils.createState();
return R.ok().data("url",authUrl);
}
public static AuthRequest getAuthRequest(QqConfigProperties qqProperties,String source) {
AuthRequest authRequest = null;
switch (source) {
case "qq":
authRequest = new AuthQqRequest(AuthConfig.builder()
.clientId(qqConfigProperties.getAppId())
.clientSecret(qqConfigProperties.getAppSecret())
.redirectUri(qqConfigProperties.getRedirectUrl())
.build());
break;
default:
break;
}
if(null == authRequest) {
throw new GuliException(20001,"授权地址无效");
}
return authRequest;
}
}
204

被折叠的 条评论
为什么被折叠?



