Spring Boot Security如何允许携带过期JWT令牌的请求通过校验
你之前查阅的ClientHttpRequestInterceptor属于OAuth2客户端侧组件,用于拦截客户端向外发送的请求,和资源服务器的JWT校验逻辑无关,不需要从该方向调整。
Spring Security OAuth2 Resource Server的JWT默认校验逻辑中,过期时间校验由JwtTimestampValidator实现,只要令牌签名合法、格式正确,仅跳过过期校验即可实现你的需求,提供两种可行实现方案:
方案1:全局关闭JWT过期时间校验
适合所有接口都需要接受合法过期令牌的场景,直接自定义JwtDecoder,移除校验链中的时间校验规则即可:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.jwt.*; import java.util.Arrays; @Configuration public class JwtConfig { @Bean public JwtDecoder jwtDecoder() { // 替换为你的授权服务器JWK Set地址 NimbusJwtDecoder decoder = NimbusJwtDecoder.withJwkSetUri("https://your-auth-server/.well-known/jwks.json").build(); // 自定义校验规则:仅保留签名、签发者、受众校验,移除过期时间校验 JwtValidator customValidator = new DelegatingOAuth2TokenValidator<>( new JwtIssuerValidator("https://your-auth-server-issuer"), // 替换为授权服务器issuer值 new JwtAudienceValidator(Arrays.asList("your-resource-id")) // 按需添加受众校验 ); decoder.setJwtValidator(customValidator); return decoder; } }
方案2:仅部分接口接受过期令牌
更符合「部分API放行」的业务要求,通过配置多套过滤链,给不同路径绑定不同的校验规则:
- 定义两个
JwtDecoder实例:一个保留默认完整校验逻辑给普通接口使用,一个移除过期校验给白名单接口使用 - 配置两条优先级不同的Security过滤链,分别匹配对应路径绑定对应的
JwtDecoder
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.oauth2.jwt.*; import org.springframework.security.web.SecurityFilterChain; import java.util.Arrays; @Configuration @EnableWebSecurity public class SecurityConfig { // 普通接口用JwtDecoder:包含完整校验,会校验过期时间 @Bean public JwtDecoder defaultJwtDecoder() { NimbusJwtDecoder decoder = NimbusJwtDecoder.withJwkSetUri("https://your-auth-server/.well-known/jwks.json").build(); decoder.setJwtValidator(JwtValidators.createDefaultWithIssuer("https://your-auth-server-issuer")); return decoder; } // 白名单接口用JwtDecoder:跳过过期时间校验 @Bean public JwtDecoder ignoreExpJwtDecoder() { NimbusJwtDecoder decoder = NimbusJwtDecoder.withJwkSetUri("https://your-auth-server/.well-known/jwks.json").build(); JwtValidator customValidator = new DelegatingOAuth2TokenValidator<>( new JwtIssuerValidator("https://your-auth-server-issuer"), new JwtAudienceValidator(Arrays.asList("your-resource-id")) ); decoder.setJwtValidator(customValidator); return decoder; } // 优先级更高的过滤链:匹配白名单接口路径,使用跳过过期校验的解码器 @Bean @Order(1) public SecurityFilterChain ignoreExpSecurityFilterChain(HttpSecurity http) throws Exception { http .securityMatcher("/api/allow-expired/**") // 替换为你的白名单接口路径 .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.decoder(ignoreExpJwtDecoder()))); return http.build(); } // 普通接口过滤链:使用默认校验逻辑 @Bean @Order(2) public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.decoder(defaultJwtDecoder()))); return http.build(); } }
优化建议
如果不需要完全放开过期校验,仅允许令牌过期不超过固定时长(比如7天),可以自定义JwtTimestampValidator调整允许的时钟偏移量,比完全跳过过期校验安全性更高:
import java.time.Duration; // 允许令牌最多过期7天(604800秒) JwtTimestampValidator expValidator = new JwtTimestampValidator(Duration.ofSeconds(604800)); // 将该校验器添加到你的自定义校验链中即可
内容的提问来源于stack exchange,提问作者Adil Karaöz
相关产品推荐
相关产品推荐

