0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【spring-webflux】WebClientでproxyヘッダをカスタマイズする

Posted at

概要

  • 認証があるproxyに対してWebClientを使って接続するときの実装方法をまとめてみました
  • basic認証はデフォルトで対応してくれていそうだったが、それ以外の認証方式だと自分で定義してあげる必要がありそうだった

依存

build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux:2.4.0'
}

実装

認証ヘッダの部分はUUIDで代用しているので、用途に合わせて書き換えをお願いしますm(_ _)m

@Configuration
public class ProxyAutoConfiguration {
    @Bean
    public WebClient proxyWebClient() {
        return WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(HttpClient.create()
                        .proxy(typeSpec -> {
                            typeSpec.type(ProxyProvider.Proxy.HTTP)
                                    .host("localhost")
                                    .port(8080)
                                    // basic認証は対応している
                                    //.username("username")
                                    //.password(s -> "password")
                                    .httpHeaders(headers -> {
                                        // ここで認証用のヘッダを乗せる
                                        headers.add("uuid", UUID.randomUUID().toString());
                                    });
                        })
                ))
                .build();
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?