OkHttp + SpringBoot相互ssl身份验证问题

时间:2019-12-17 10:35:48

标签: java android spring-boot okhttp

我面临以下问题:

我正在OkHttp中像这样初始化ssl上下文:

 SSLContext sslContext = SSLContext.getInstance("TLS");
            trustManager = getTrustedManager();
            keyManager = (X509KeyManager) getKeyManager().getKeyManagers()[0];
            sslContext.init(new KeyManager[]{keyManager}, new TrustManager[] {trustManager}, new SecureRandom());
            sslSocketFactory = sslContext.getSocketFactory();
  return new OkHttpClient.Builder()
            .sslSocketFactory(sslSocketFactory, trustManager)
            .build();

但是,在服务器端,我正在尝试获取客户端证书:

 UserDetails userDetails = null;
        FailureResponse failureResponse = null;
        try{
            final X509Certificate[] certificates = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");

        } catch (SecurityException ex){
            logger.error(ex.getMessage());
            failureResponse = new FailureResponse(ex.getMessage());
        }

但是证书始终为空。不知道如何解决。

0 个答案:

没有答案