在使用OpenFeign時進行安全認證,可以通過以下步驟實現:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@FeignClient
注解,并在該注解中配置認證信息,例如:@FeignClient(name = "service-name", url = "http://service-url", configuration = FeignClientConfiguration.class)
public interface MyFeignClient {
// 定義Feign接口方法
}
public class FeignClientConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("username", "password");
}
}
@Service
public class MyService {
@Autowired
private MyFeignClient myFeignClient;
public void callFeignService() {
myFeignClient.callMethod();
}
}
通過以上步驟,就可以在使用OpenFeign時進行安全認證。