package com.vincent.rsf.httpaudit.config; import com.vincent.rsf.httpaudit.web.OutboundHttpAuditInterceptor; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.web.client.RestTemplate; /** * 为所有 RestTemplate 注册出站审计拦截器 */ @RequiredArgsConstructor public class HttpAuditRestTemplateBeanPostProcessor implements BeanPostProcessor { private final OutboundHttpAuditInterceptor outboundHttpAuditInterceptor; @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RestTemplate) { RestTemplate rt = (RestTemplate) bean; boolean exists = rt.getInterceptors().stream().anyMatch(i -> i instanceof OutboundHttpAuditInterceptor); if (!exists) { rt.getInterceptors().add(0, outboundHttpAuditInterceptor); } } return bean; } }