当前位置: 首页 > 知识库问答 >
问题:

Spring防尘套启动器-RestTemplate

惠诚
2023-03-14

我目前正在尝试编写一个Spring Boot启动器,它将使用API网关自动验证我们的微服务,并在所有传出请求(针对网关)的标头中包含访问令牌。
我正在创建一个RestTemboard bean并为其提供我们的自定义拦截器,但我的问题是,通过这样做,我阻止其他团队(将使用此启动器)使用他们自己的RestTemboard配置,因为他们必须定义导致多个bean存在的相同bean。

@Bean
public RestTemplate restTemplate(){
  RestTemplate restTemplate = new RestTemplate();
  List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
  if (interceptors.isEmpty()){
    interceptors = new ArrayList<>();
  }
  interceptors.add(clientCredentialsAuthInterceptor());
  restTemplate.setInterceptors(interceptors);
  return restTemplate;
}

是否有其他方法可以拦截所有传出请求或进一步配置RestTemplate?

共有1个答案

哈骞仕
2023-03-14

未测试,但可能会给您一个起点:

// Create an interface that users of your dependency 
// can implement which provides interceptors
public interface RestTemplateAuthInterceptorProvider {

  // This interface provides interceptors, so they can add as many as they want
  List<ClientHttpRequestInterceptor> provideInterceptor();
}

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
// define a conditional default implementation of your interceptor provider
@Bean
@ConditionalOnMissingBean(RestTemplateAuthInterceptorProvider.class)
public RestTemplateAuthInterceptorProvider restTemplateAuthInterceptorProvider() {
  return () -> ... // implement your auth interceptor and return
}

// In your actual rest template creation use method argument injection
// If other teams implement the RestTemplateAuthInterceptorProvider interface
// conditional case above will be false and your implementation will not interfere
// If they dont implement RestTemplateAuthInterceptorProvider
// your default implementation will be here
@Bean
public RestTemplate restTemplate(RestTemplateAuthInterceptorProvider provider) {
  RestTemplate restTemplate = new RestTemplate();
  List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
  if (interceptors == null){
    interceptors = new ArrayList<>();
  }
  interceptors.addAll(provider.provideInterceptor()); // from argument 
  restTemplate.setInterceptors(interceptors);
  return restTemplate;
}

编辑:另一种黑客方法是操作已定义的RESTTemplatebean

@Component
@ConditionalOnBean(RestTemplate.class)
public class RestTemplateBeanCustomizer {

  private List<RestTemplate> restTemplateBeans;

  // This injects all restTemplate bean definitions to your bean as a list
  @Autowired
  public RestTemplateBeanCustomizer(List<RestTemplate> restTemplateBeans) {
    this.restTemplateBeans = restTemplateBeans;
  }

  @PostConstruct
  public void customizeRestTemplateBeans() {
      for (RestTemplate restTemplate : restTemplateBeans) {
        // Add your interceptors message handlers etc
        // restTemplate.set...
      }
  }
}
 类似资料:
  • 我正在尝试使用现有的Gradle Spring MVC项目设置Spring执行器。我无法使用@EnableAutoConfiguration。不幸的是,我无法到达执行器endpoint,我想我遗漏了一些东西。 项目中的Spring依赖项包括: 我正在尝试使用以下内容配置project: 在属性文件中,我添加了: 没有启用执行器endpoint,当尝试访问它们时,我得到404。我经历了许多相关问题,

  • 我正在尝试使用spring boot和hibernate。当我使用存储库时,它工作得很好,但我正在尝试使用Hibernate会话来创建DAO,而这个DAO不是事务的一部分。 这是测试代码: 应用Java语言 UserBusinessImpl。java: 用户存储库。Java语言 用户DAO: 当我尝试getCurrentSession()时,它抛出了一个错误。openSession()与我的事务分

  • 我想将Spring启动执行器添加到我的应用程序,但当我添加此依赖项时 我得到以下错误 java.lang.reflect.InvocationTargetException at_NativeMethodAccessorImpl.invoke0(Native Method)at(...)由以下原因引起:org.springframework.beans.factory.不满意DependencyE

  • 我试图通过遵循这里的教程,使用Spring boot在Java中创建一个RESTful应用程序。我想修改它,以便可以从URL中提取标识符,并使用它来服务请求。 所以

  • 我在本地系统上创建了一个带有Rest控制器和Oracle的spring boot应用程序,通过IDE它运行良好,mvn build was fine包也很好,但如果我将其作为可执行jar运行,我会得到以下错误。我有申请表。我提供的所有spring的属性。数据源,但这里我得到了错误。请告知。 这是我的pom.xml: 我正在尝试Spring Boot并尝试将其作为可执行的jar运行。

  • 我使用了不同端口的Spring启动执行器,如下所示 在应用程序中,我想在执行器端口中使用启用csrf=true,但我不想使用csrf。因为我想对jolokia使用批量POST请求。 只排除并不聪明。 下面的属性对我很好(bt管理。安全。启用csrf不存在)。 有什么好的解决办法吗?