OAuth2 客户端设置
将您的应用配置为 OAuth2 客户端,并为各种提供程序定义注册信息。
OAuth2 客户端设置 是 CoddyKit 上的免费 Spring Security 6 & JWT Authentication 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Security 6 & JWT Authentication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is an OAuth2 Client?
In OAuth2, an OAuth2 Client is an application that wants to access resources on behalf of a user from a Resource Server. Think of it as your app asking permission to use another service (like Google or GitHub) on your behalf.
It's not the user, but an application acting for the user.
Why Configure Our App?
To use an external service's API (e.g., getting user profiles, posting updates), your application needs to be recognized by that service. This recognition process is called client registration.
- Your app gets a unique identity.
- The service knows who is requesting access.
- It enables secure communication and authorization.
Key Client Registration Details
When you register your application with an OAuth2 Authorization Server (the service that grants access), you'll typically provide and receive:
- Client ID: A public identifier for your application.
- Client Secret: A confidential key used to authenticate your app.
- Redirect URI(s): Where the Authorization Server sends the user back after authorization.
Spring Security as an OAuth2 Client
Spring Security makes it incredibly easy to configure your Spring Boot application to act as an OAuth2 Client. It handles much of the complex OAuth2 flow automatically.
All you need to do is provide the necessary registration details for the external service you want to connect to.
Client Configuration File
Spring Security's OAuth2 client configuration lives primarily in your application.yml or application.properties file.
You'll use two main prefixes:
spring.security.oauth2.client.registration: Defines details about your app's registration with a specific provider.spring.security.oauth2.client.provider: Defines details about the OAuth2 provider itself (e.g., its authorization endpoint).
Setting Up a Provider
Under spring.security.oauth2.client.provider.[provider-name], you define the endpoints of the external OAuth2 service. For example, for a provider named github:
authorization-uri: Where users go to authorize your app.token-uri: Where your app exchanges codes for tokens.user-info-uri: Where your app fetches user details.
Often, Spring Boot can auto-configure these for popular providers if you just provide the client-id and client-secret.
Registering Your Client App
Under spring.security.oauth2.client.registration.[registration-id], you specify your app's unique registration details for a given provider. The registration-id acts as a unique name for your specific client configuration.
provider: Links to a defined provider (e.g.,github).client-id: Your app's public ID.client-secret: Your app's secret key.redirect-uri: The callback URL.scope: Permissions your app requests (e.g.,read:user).authorization-grant-type: The OAuth2 flow used (e.g.,authorization_code).
Example: GitHub Client Setup
Let's look at a concrete example using GitHub. You would first register your application on GitHub's developer settings to get a Client ID and Client Secret.
Then, configure your application.yml:
spring:
security:
oauth2:
client:
registration:
github:
client-id: your-github-client-id
client-secret: your-github-client-secret
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
scope: read:user,user:email
provider:
github:
authorization-uri: https://github.com/login/oauth/authorize
token-uri: https://github.com/login/oauth/access_token
user-info-uri: https://api.github.com/userMinimal Spring Boot App
To enable OAuth2 client functionality, ensure you have the spring-boot-starter-oauth2-client dependency. Spring Boot will automatically detect the configuration and set up the necessary filters.
Here's a basic Spring Boot application entry point:
package com.coddykit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@SpringBootApplication
@RestController
public class Oauth2ClientApp {
public static void main(String[] args) {
SpringApplication.run(Oauth2ClientApp.class, args);
}
@GetMapping("/")
public String welcome(Principal principal) {
if (principal != null) {
return "Hello, " + principal.getName() + "! You are logged in with OAuth2.";
}
return "Hello! Please log in with OAuth2.";
}
}The Automated OAuth2 Flow
Once configured, Spring Security automatically:
- Redirects unauthenticated users to the configured Authorization Server's login page.
- Handles the authorization code exchange after the user grants permission.
- Fetches user details from the
user-info-uri. - Populates the
SecurityContextwith the authenticated user.
This significantly simplifies implementing OAuth2 client logic.
Quick Check: Client Config
You're setting up a Spring Boot application to act as an OAuth2 client for an external service. Which of the following properties is primarily used to identify your application to the external service, and should be kept confidential?
Recap: OAuth2 Client Setup
You've learned how to configure your Spring Boot application as an OAuth2 Client. We covered:
- The role of an OAuth2 Client.
- Key configuration properties like
client-idandclient-secret. - How to use
application.ymlfor client and provider registration. - Spring Security's automatic handling of the OAuth2 flow.
Next, we'll dive into integrating specific social login providers like Google and GitHub in more detail!
常见问题解答
「OAuth2 客户端设置」课时是免费的吗?
是的 — 「OAuth2 客户端设置」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Security 6 & JWT Authentication 课程的其余内容,请升级到 CoddyKit PRO。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
「OAuth2 客户端设置」这节课中我会学到什么?
将您的应用配置为 OAuth2 客户端,并为各种提供程序定义注册信息。 你通过在浏览器中直接运行的动手代码来练习 Spring Security 6 & JWT Authentication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Security 6 & JWT Authentication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Security 6 & JWT Authentication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「OAuth2 客户端设置」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Security 6 & JWT Authentication 课中编写并运行代码吗?
能。每节 Spring Security 6 & JWT Authentication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- OAuth2 客户端设置
- 社交登录集成
- 自定义 OAuth2 成功处理程序
- 访问已验证身份的 OAuth2 用户