Implementing Third-Party OAuth2 Login with Gitee in Spring Boot
The article walks through registering a Gitee OAuth2 application, building a Spring Boot project with a login page, implementing a controller that exchanges the authorization code for an access token, retrieves user information, and displays the authenticated user's name and avatar on a success page.
This article explains how to implement third‑party OAuth2 login using Gitee in a Spring Boot application.
What is OAuth2.0 – OAuth provides a secure, open standard for authorizing user resources without exposing passwords. OAuth2.0 focuses on developer simplicity.
Registering an application on Gitee – Create an app on Gitee’s Open Platform, obtain client_id , set redirect_uri , and choose response_type=code . The authorization URL looks like:
https://gitee.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fsuccess&response_type=codeAfter the user authorizes, Gitee redirects to the redirect_uri with an authorization code, which can be exchanged for an access token:
https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}The token response contains access_token , token_type , expires_in , etc.
Creating the Spring Boot project – Add a simple index.html with a link to the Gitee authorization URL.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form>
Username:<input type="text"/><br/>
Password:<input type="password"/><br/>
<a href="https://gitee.com/oauth/authorize?client_id=...&redirect_uri=http://localhost:8080/success&response_type=code">Gitee Login</a>
<input type="submit" value="Login"/>
</form>
</body>
</html>Controller implementation – A Spring MVC controller handles the callback, extracts the code , exchanges it for an access token, fetches user info, and forwards the data to a success page.
@Controller
public class LoginController {
@GetMapping("/success")
public String login(@RequestParam("code") String code, Map<String,String> map) {
String accessKey = getAccessKey(code);
String userInfo = getUserInfo(accessKey);
map.put("name", (String)JSONObject.parseObject(userInfo).get("name"));
map.put("avatar_url", (String)JSONObject.parseObject(userInfo).get("avatar_url"));
return "success";
}
// methods getAccessKey and getUserInfo use OkHttp to call Gitee APIs
}The success.html page displays the retrieved username and avatar.
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Success</title></head>
<body>
<h1>登录成功!</h1>
<h1>用户名:<span th:text="${#request.getAttribute('name')}"></span></h1>
<img th:src="${#request.getAttribute('avatar_url')}" />
</body>
</html>Running the application and clicking the Gitee login link completes the OAuth flow, prints the authorization code in the console, and shows the user’s name and avatar on the success page.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.