You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Web 6.0.x与OAuth2 2.4兼容问题:HttpStatus与HttpStatusCode不匹配报错

Spring Boot 3.x 迁移后 OAuth2 与 Spring Web 6.x 类型兼容问题解决方案

方案1:升级 OAuth2 依赖到适配 Spring Boot 3.x 的版本

  • 问题核心是当前使用的spring-security-oauth2 2.4版本仅适配Spring Web 5.x,而Spring Boot 3.x配套的OAuth2组件已整合到Spring Security 6.x体系中,完全兼容HttpStatusCode类型。
  • 替换依赖:移除旧的spring-security-oauth2依赖,引入Spring Boot官方OAuth2 starter:
<!-- Maven -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
// Gradle
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
  • 代码调整:新体系中的DefaultOAuth2ErrorHandler已原生适配HttpStatusCode,无需额外修改错误处理逻辑。

方案2:自定义错误处理器做类型转换(不升级依赖时)

如果暂时无法升级依赖,可通过包装ClientHttpResponse实现类型兼容:

  1. 编写兼容型错误处理器:
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
import java.io.IOException;
import java.io.InputStream;

public class CompatibleOAuth2ErrorHandler extends OAuth2ErrorHandler {
    @Override
    protected boolean hasError(ClientHttpResponse response) throws IOException {
        HttpStatusCode statusCode = response.getStatusCode();
        HttpStatus httpStatus = HttpStatus.resolve(statusCode.value());
        return httpStatus != null && super.hasError(new StatusConvertingResponseWrapper(response, httpStatus));
    }

    private static class StatusConvertingResponseWrapper implements ClientHttpResponse {
        private final ClientHttpResponse delegate;
        private final HttpStatus convertedStatus;

        public StatusConvertingResponseWrapper(ClientHttpResponse delegate, HttpStatus convertedStatus) {
            this.delegate = delegate;
            this.convertedStatus = convertedStatus;
        }

        @Override
        public HttpStatus getStatusCode() {
            return convertedStatus;
        }

        // 以下方法直接委托给原响应对象
        @Override
        public int getRawStatusCode() throws IOException {
            return delegate.getRawStatusCode();
        }

        @Override
        public String getStatusText() throws IOException {
            return delegate.getStatusText();
        }

        @Override
        public void close() {
            delegate.close();
        }

        @Override
        public InputStream getBody() throws IOException {
            return delegate.getBody();
        }

        @Override
        public org.springframework.http.HttpHeaders getHeaders() {
            return delegate.getHeaders();
        }
    }
}
  1. 注册到RestClient:
RestClient restClient = RestClient.builder()
        .errorHandler(new CompatibleOAuth2ErrorHandler())
        .build();

方案3:手动处理状态码转换

若不想继承原错误处理器,可在RestClient的错误处理逻辑中直接转换状态码:

RestClient restClient = RestClient.builder()
        .errorHandler((request, response) -> {
            HttpStatusCode statusCode = response.getStatusCode();
            HttpStatus httpStatus = HttpStatus.resolve(statusCode.value());
            if (httpStatus != null) {
                new OAuth2ErrorHandler().handleError(request, new StatusConvertingResponseWrapper(response, httpStatus));
            } else {
                throw new org.springframework.web.client.HttpClientErrorException(statusCode);
            }
        })
        .build();

内容的提问来源于stack exchange,提问作者Abhi Ram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:22:52