Spring boot ClientHttpRequestInterceptor 在 401 上重新发送

作者:编程家 分类: spring 时间:2025-10-03

使用Spring Boot的ClientHttpRequestInterceptor在401状态码下重新发送请求

在使用Spring Boot开发Web应用程序时,我们经常需要与外部的API进行交互。在这个过程中,我们可能会遇到一些需要进行身份验证的情况。当我们向API发送请求时,如果我们的身份验证凭证过期或无效,API很可能会返回401状态码(未授权)。在这种情况下,我们需要重新发送带有新身份验证凭证的请求。为了实现这个功能,我们可以使用Spring Boot的ClientHttpRequestInterceptor。

ClientHttpRequestInterceptor是一个接口,它允许我们在发送请求之前或之后对请求进行修改。通过实现这个接口,我们可以在请求返回401状态码时,捕获这个状态码并重新发送请求。

实现ClientHttpRequestInterceptor接口

首先,我们需要创建一个类来实现ClientHttpRequestInterceptor接口。我们可以在这个类中实现intercept方法,该方法接收一个HttpRequest对象和一个byte[]数组作为参数。

java

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpRequest;

import org.springframework.http.HttpStatus;

import org.springframework.http.client.ClientHttpRequestExecution;

import org.springframework.http.client.ClientHttpRequestInterceptor;

import org.springframework.http.client.ClientHttpResponse;

import org.springframework.util.StreamUtils;

import java.io.IOException;

import java.nio.charset.Charset;

public class RetryInterceptor implements ClientHttpRequestInterceptor {

@Override

public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

ClientHttpResponse response = execution.execute(request, body);

if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {

HttpHeaders headers = response.getHeaders();

// 获取新的身份验证凭证

String newToken = getNewToken();

headers.set("Authorization", newToken);

// 重新发送请求

response = execution.execute(request, body);

}

return response;

}

private String getNewToken() {

// 实现获取新的身份验证凭证的逻辑

return "newToken";

}

}

在上面的代码中,我们首先执行原始的请求,并获取响应。然后,我们检查响应的状态码是否为401。如果是,我们通过调用getNewToken方法来获取新的身份验证凭证,并将其添加到请求的头部中。然后,我们再次发送请求,并返回响应。

将Interceptor应用到RestTemplate

接下来,我们需要将我们刚刚实现的Interceptor应用到我们的RestTemplate中。RestTemplate是Spring Framework中用于发送HTTP请求的核心类。

java

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import org.springframework.http.client.ClientHttpRequestInterceptor;

import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@Bean

public RestTemplate restTemplate() {

RestTemplate restTemplate = new RestTemplate();

restTemplate.setInterceptors(Collections.singletonList(retryInterceptor()));

return restTemplate;

}

@Bean

public ClientHttpRequestInterceptor retryInterceptor() {

return new RetryInterceptor();

}

}

在上面的代码中,我们创建了一个名为restTemplate的Bean,并将RetryInterceptor添加为其拦截器。这样,每当我们使用restTemplate发送请求时,Interceptor都会被调用,并在需要时重新发送请求。

使用RestTemplate发送请求

现在,我们可以使用我们的RestTemplate来发送请求,并在需要时自动重新发送请求。

java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

@RestController

public class MyController {

@Autowired

private RestTemplate restTemplate;

@GetMapping("/api/data")

public ResponseEntity getData() {

String url = "https://api.example.com/data";

ResponseEntity response = restTemplate.getForEntity(url, String.class);

return response;

}

}

在上面的代码中,我们使用restTemplate发送了一个GET请求到https://api.example.com/data,并将响应返回给调用者。如果响应的状态码为401,Interceptor会自动重新发送请求,并返回新的响应。

在本文中,我们介绍了如何使用Spring Boot的ClientHttpRequestInterceptor来在401状态码下重新发送请求。通过实现ClientHttpRequestInterceptor接口,并将其应用到RestTemplate中,我们可以在需要时自动重新发送带有新身份验证凭证的请求。这个功能对于与需要身份验证的API进行交互的Web应用程序非常有用,可以提高开发效率和用户体验。

以上就是使用Spring Boot的ClientHttpRequestInterceptor在401状态码下重新发送请求的案例代码和解释。希望本文对你理解和使用ClientHttpRequestInterceptor有所帮助。