【问题标题】:Intercept the RestTemplate by getting already created RestTemplate通过获取已经创建的 RestTemplate 来拦截 RestTemplate
【发布时间】:2018-02-05 14:55:19
【问题描述】:

我有拦截 RestTemplate 的逻辑,我在配置文件中添加/注册 RestTemplate (SecurityConfiguration.java) 但我想通过获取已注册的 RestTemplate 对象从另一个配置文件添加该拦截器:

public class TranslogRestTemplateCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        boolean isRestTemplate = false;
        try {
            if (context.getBeanFactory() != null) {
                isRestTemplate = (context.getBeanFactory().getBean(RestTemplate.class) != null);
            }
        } catch (BeansException e) {
            return false;
        }
        return isRestTemplate;
    }
}

配置类:

@Configuration
public class RestTemplateConfig {

    private final MyInterceptor myInterceptor;

    @Value("${com.pqr.you.rest.enabled:true}")
    private boolean transEnabled;

    @Autowired
    public RestTemplateConfig(MyInterceptor myInterceptor) {
        this.myInterceptor = myInterceptor;
    }

    @Autowired
    private ApplicationContext appContext;

    // The logic added below is not working for me
    @Bean
    @Conditional(TranslogRestTemplateCondition.class)
    public RestTemplate addInterceptor(ApplicationContext appContext) {
        RestTemplate restTemplate = appContext.getBean(RestTemplate.class);
        if (transEnabled) {
            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            interceptors.add(myInterceptor);
            restTemplate.setInterceptors(interceptors);
        }
        return restTemplate;
    }
}

RestTemplate 的实际逻辑,它将返回所需的拦截器和一些其他值(在返回这个 restTemplate 时,我的拦截器也需要在这里添加,而不是覆盖现有值) 或者 通过获取以下 restTempalte 对象并将 MyInterceptor 添加到 restTemplate。

@Configuration
public class SecurityConfiguration {

    @Bean
    public AbcInterceptor abcRequestInterceptor(XyzService xyzService) {
        return new AbcInterceptor("abc-app", null, xyzService);
    }

    // I dont want to create bean here 
    /*@Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }*/ 

    @Bean
    public RestTemplate restTemplate(AbcRequestInterceptor abcRequestInterceptor) {
        RestTemplate restTemplate = new RestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(abcRequestInterceptor);
        //interceptors.add(myInterceptor); // I dont want to add this interceptor here
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }
}

【问题讨论】:

  • addInterceptor() 方法中删除@Conditional 注释会发生什么情况?
  • 添加@Condition 类的原因是,如果我将该 RestTemplateConfig 类移动到其他常见项目,那么我确保目标应用程序应该有 RestTemplate bean 可用
  • 好的,但是您确定为您创建了@Bean(调用了addInterceptor() 方法)?
  • 不,我想调用 addInterceptor()
  • 如果有什么想法?????? @gajos

标签: java spring interceptor resttemplate


【解决方案1】:

我认为在这种情况下您需要的是 BeanPostProcessor 而不是 Condition 类。如果 bean 存在,它将允许您修改它。您可以创建一个如下所示的,而不是 RestTemplateConfiguration 类和 Condition

@Configuration
public class RestTemplatePostProcessor implements BeanPostProcessor {

    @Value("${com.pqr.you.rest.enabled:true}")
    private boolean transEnabled;

    @Bean
    MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof RestTemplate) { // or you can check by beanName if you like
            final RestTemplate restTemplate = (RestTemplate) bean;

            if (transEnabled) {
                List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                interceptors.add(myInterceptor());
                restTemplate.setInterceptors(interceptors);
            }
            return restTemplate;
        }
        return bean;
    }
}

请注意,在您的代码中,您正在尝试创建另一个 RestTemplate 实例(名为“addInterceptor”),这似乎是不受欢迎的。

【讨论】:

    猜你喜欢
    • 2019-12-22
    • 1970-01-01
    • 2015-11-08
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多