【问题标题】:NTLM authentication using spring使用 spring 的 NTLM 身份验证
【发布时间】:2019-07-22 11:58:28
【问题描述】:

我想在我的应用程序中使用一项服务。该服务使用 NTLM 身份验证进行身份验证,我正在尝试使用 NTLM Auth 进行 REST 调用。

我尝试了这个帖子How to set NTLM authentication in rest template Header in Spring中的建议

但它给了我一个 500 错误(虽然 401 消失了)

只能从脚本调用类定义中具有 [ScriptService] 属性的 Web 服务。

【问题讨论】:

标签: java spring spring-boot spring-mvc ntlm


【解决方案1】:

@豆 公共 RestTemplate restTemplate(RestTemplateBuilder builder) 抛出 NoSuchAlgorithmException,KeyManagementException { // 如果你想禁用 ssl 认证验证

    TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
    };
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    String user = "username";
    String password = "password";
    String domain = "domain";  // optional 
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials(user, password, null, domain));

    CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLContext(sslContext)
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .setDefaultCredentialsProvider(credentialsProvider)
            .build();
    HttpComponentsClientHttpRequestFactory customRequestFactory = new HttpComponentsClientHttpRequestFactory();
    customRequestFactory.setHttpClient(httpClient);
    RestTemplate build = builder.requestFactory(() -> customRequestFactory).build();
    return build;
}

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2018-04-29
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多