如何从其他Spring项目调用受Spring Security保护的API
解决方案
你要调用的接口受Spring Security的HTTP Basic认证保护,和你在Postman里选择Basic Auth填入用户名密码的逻辑一致,Spring项目中可以通过以下两种常用方式实现调用:
方案1:使用RestTemplate实现调用
RestTemplate是Spring提供的同步HTTP客户端,适合常规非响应式项目使用:
- 先配置带认证信息的RestTemplate实例
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.support.BasicAuthenticationInterceptor; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); // 在此处传入和Postman一致的用户名、密码,自动注入到所有请求头中 restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("foo", "foo")); return restTemplate; } }
- 在本地控制器中注入RestTemplate,发起调用
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class YourLocalController { private final RestTemplate restTemplate; // 构造器注入配置好的RestTemplate public YourLocalController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @PostMapping("/your-local-api-path") public ResponseEntity<?> localMethod(@RequestBody String username) { // 替换为你实际的远程服务地址+接口路径 String remoteApiUrl = "http://远程服务IP:端口/mypath"; // 发起POST请求,自动携带认证信息 ResponseEntity<?> remoteResponse = restTemplate.postForEntity(remoteApiUrl, username, Object.class); // 可对远程返回结果做二次处理后再返回给前端 return remoteResponse; } }
方案2:使用WebClient实现调用(Spring 5+推荐)
WebClient是Spring官方推荐的新一代HTTP客户端,支持同步、异步、响应式场景,性能更优:
- 配置带认证信息的WebClient实例
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; @Configuration public class WebClientConfig { @Bean public WebClient webClient() { return WebClient.builder() // 配置远程服务的基础地址,后续调用可直接写接口路径 .baseUrl("http://远程服务IP:端口") // 在此处传入用户名密码,自动注入到所有请求头中 .defaultHeaders(headers -> headers.setBasicAuth("foo", "foo")) .build(); } }
- 在本地控制器中注入WebClient,发起调用
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @RestController public class YourLocalController { private final WebClient webClient; public YourLocalController(WebClient webClient) { this.webClient = webClient; } @PostMapping("/your-local-api-path") public Mono<ResponseEntity<?>> localMethod(@RequestBody String username) { return webClient.post() .uri("/mypath") .bodyValue(username) .retrieve() .toEntity(Object.class); } }
注意事项
- 上述方案默认适配Spring Security默认的HTTP Basic认证模式,如果你对远程服务的认证逻辑做过自定义调整(比如改为表单登录、OAuth2认证),需要对应修改认证信息的传入逻辑。
- 不要把用户名密码硬编码在代码中,建议放到
application.yml/application.properties配置文件中,通过@Value或者@ConfigurationProperties注解注入,方便后续维护修改。 - 如果远程接口返回的是特定类型的结果,可以把代码中的
Object.class替换为你自定义的实体类类型,直接完成结果反序列化。
内容的提问来源于stack exchange,提问作者timo
相关产品推荐
相关产品推荐

