Spring Boot OpenFeign 配置 HTTPS 客户端证书

Spring Boot OpenFeign HTTPS About 927 words

配置代码

其中证书文件使用base64加密,以字符串形式配置在application.yaml文件中。

import lombok.RequiredArgsConstructor;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContextBuilder;

@RequiredArgsConstructor
public class MyHttpsFeignConfig {

    @Bean
    public Client client(MyHttpsConfig config) throws Exception {
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(config.getClientCertificateBase64EncodedString()))) {
            char[] pwd = config.getClientCertificatePwd().toCharArray();
            KeyStore clientStore = KeyStore.getInstance("PKCS12");
            clientStore.load(inputStream, pwd);
            SSLContext sslContext = SSLContextBuilder.create().loadKeyMaterial(clientStore, pwd).build();
            return new Client.Default(sslContext.getSocketFactory(), SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        }
    }

}
Views: 166 · Posted: 2024-04-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh