解码和验证 JWT
了解资源服务器如何自动解码和验证授权服务器签发的 JWT。
解码和验证 JWT 是 CoddyKit 上的免费 Spring Security 6 & JWT Authentication 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Security 6 & JWT Authentication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Resource Server & JWTs
Welcome to this lesson! We'll explore how a Spring Security OAuth2 Resource Server automatically handles JSON Web Tokens (JWTs).
You'll learn about the decoding and validation processes that protect your API endpoints.
The Resource Server's Role
A Resource Server is an application that hosts protected resources (like API endpoints or data) and needs to verify who is trying to access them.
It relies on an Authorization Server to issue tokens (like JWTs) that grant access. The Resource Server then validates these tokens.
Receiving JWTs in Requests
When a client wants to access a protected resource, it sends the JWT in the Authorization header of its HTTP request. This is typically done using the Bearer Token scheme.
- Bearer Token: A credential that grants access to anyone who possesses it.
The Resource Server extracts this token for processing.
Automatic JWT Processing
When you configure a Spring Boot application as an OAuth2 Resource Server, Spring Security handles much of the JWT processing for you automatically.
It detects the Bearer token in the header and initiates its internal decoding and validation pipeline.
Decoding the Token
The first step is decoding the JWT. A JWT has three parts: Header, Payload, and Signature, separated by dots.
Decoding means parsing the base64-encoded Header and Payload into readable JSON. This step doesn't verify the token's authenticity yet; it just makes its content readable.
The Validation Process
After decoding, the Resource Server performs crucial validation checks to ensure the JWT is legitimate and hasn't been tampered with. These checks include:
- Signature Verification: Is the token authentic?
- Expiration (
exp): Is the token still valid? - Not Before (
nbf): Is the token active yet? - Issuer (
iss): Was it issued by the expected Authorization Server? - Audience (
aud): Is it intended for this Resource Server?
Verifying the Signature
Signature verification is the most critical step. It ensures the token's integrity and authenticity.
The Resource Server uses the public key provided by the Authorization Server (or a shared secret for symmetric algorithms) to re-compute and compare the signature. If they don't match, the token is rejected.
The `JwtDecoder` Interface
Spring Security uses the JwtDecoder interface to perform the decoding and signature verification of a JWT. It takes the raw JWT string and returns a Jwt object, which contains the decoded headers and claims.
The most common implementation for JWS (JSON Web Signature) tokens is NimbusJwtDecoder.
Resource Server Configuration
To enable this automatic decoding and validation, you configure your Spring Boot application as an OAuth2 Resource Server. You typically provide the issuer URI or the JWK Set URI of your Authorization Server.
Spring Security will then fetch the public keys needed to verify incoming JWTs.
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(jwt -> jwt
.jwkSetUri("http://localhost:9000/.well-known/jwks.json") // Or issuerUri
)
);
return http.build();
}
}JWT to Authentication Object
After a JWT is successfully decoded and validated, Spring Security converts its claims into an Authentication object, typically a JwtAuthenticationToken.
This object is then stored in the Security Context, making the user's principal (their identity) and granted authorities (permissions) available throughout the application.
JWT Validation Checks
Let's check your understanding of JWT validation. A Spring Security OAuth2 Resource Server performs several important checks to ensure a JWT is valid and trustworthy.
Recap & Next Steps
You've learned how a Spring Security OAuth2 Resource Server automatically decodes and validates JWTs!
- We covered the Resource Server's role in protecting resources.
- Explored how JWTs are received and automatically processed.
- Understood the critical steps of decoding, signature verification, and claim validation (expiration, issuer, audience).
- Saw how to configure the Resource Server to use an Authorization Server's JWK Set URI.
- Learned how validated JWTs populate the Security Context.
Next, we'll dive deeper into enforcing specific scopes and claims within incoming JWTs to control access to different parts of your API.
常见问题解答
「解码和验证 JWT」课时是免费的吗?
是的 — 「解码和验证 JWT」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Security 6 & JWT Authentication 课程的其余内容,请升级到 CoddyKit PRO。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
「解码和验证 JWT」这节课中我会学到什么?
了解资源服务器如何自动解码和验证授权服务器签发的 JWT。 你通过在浏览器中直接运行的动手代码来练习 Spring Security 6 & JWT Authentication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Security 6 & JWT Authentication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Security 6 & JWT Authentication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「解码和验证 JWT」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Security 6 & JWT Authentication 课中编写并运行代码吗?
能。每节 Spring Security 6 & JWT Authentication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。