在我们生活中无时无刻都在使用第三方登录,如QQ登录、微信登录等,今天教你如何快速使用springboot整合第三方登录,下面教程以Gitee为例
Justauth官网:https://www.justauth.cn/
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${latest.version}</version> ///视频使用的1.16.5
</dependency>
hutool官网:Hutool — A set of tools that keep Java sweet.
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
网址:第三方应用 - Gitee.com
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
.clientId("Client ID") //我的应用里面的Client ID
.clientSecret("Client Secret")//我的应用里面的Client Secret
.redirectUri("应用回调地址")//我的应用里面的应用回调地址
.build());
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGiteeRequest;
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(HttpServletResponse response) throws IOException {
AuthRequest authRequest = getAuthRequest();
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
//回调地址
@RequestMapping("/callback")
public Object login(AuthCallback callback) {
AuthRequest authRequest = getAuthRequest();
return authRequest.login(callback);
}
//配置信息
private AuthRequest getAuthRequest() {
return new AuthGiteeRequest(AuthConfig.builder()
.clientId("Client ID")
.clientSecret("Client Secret")
.redirectUri("应用回调地址")
.build());
}
}
//回调地址
@RequestMapping("/callback")
public Object login(AuthCallback callback) {
AuthRequest authRequest = getAuthRequest();
//false表示包含null
JSONObject json = JSONUtil.parseObj(authRequest.login(callback),false);
JSONObject jsonObject = JSONUtil.parseObj(json.get("data"));
return "我的昵称ID是"+jsonObject.get("uuid")+"昵称为"+jsonObject.get("nickname")+"个性签名为:"+jsonObject.get("remark");
}
b站
快手
使用第三方登录,无论是QQ登录还是微信登录或者是Gitee登录,采取的思路是一样,有关于代码和学习上的问题可以添加QQ群907751626,谢谢大家 希望可以一键三连。