【发布时间】:2022-11-04 10:06:58
【问题描述】:
我正在使用 Spring WebClient 来访问需要 NTLM 身份验证的 Rest 服务。它在 Postman 中工作,如下所示:
- 点击 URL - http://example.com:83/api/auth/token,将身份验证作为 NTLM 身份验证,并提供用户名和密码。当点击此服务时,它会返回一个令牌。
- 此令牌必须作为实际邮政服务的不记名令牌在标头中传递 -
http://example.com:89/api/v1/employee
但是当我使用 Spring WebClient 尝试相同的操作时,我面对的是
401 - Unauthorized error。在我正在使用的代码 sn-p 下面。BasicCredentialsProvider tokenProvider = new BasicCredentialsProvider(); tokenProvider.setCredentials( new AuthScope("http", "example.com", 83, "/api/auth/token", StandardAuthScheme.NTLM), new NTCredentials("testuser", "pwd".toCharArray(), null, null) ); webClient = WebClient.builder() .clientConnector(new HttpComponentsClientHttpConnector (HttpAsyncClients .custom() .setDefaultCredentialsProvider(tokenProvider) .setTargetAuthenticationStrategy(DefaultAuthenticationStrategy.INSTANCE) .setDefaultRequestConfig( RequestConfig.custom() .setAuthenticationEnabled(true) .setTargetPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.NTLM)) .setExpectContinueEnabled(true) .build()) .build())) .build(); ParameterizedTypeReference<LinkedHashMap<String, Object>> result = new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {}; Map<String, Object> body = new HashMap<>(); body.put("test-key", "value"); webClient.post().uri("http://example.com:89/api/v1/employee").contentType(MediaType.APPLICATION_JSON).accept(MediaType.ALL).bodyValue(body).retrieve().bodyToMono(result).block();这是正确的方法吗?
【问题讨论】:
-
请注意,我们更喜欢这里的技术写作风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,聊天材料和缩写 txtspk,恳求,你多久了被卡住,投票建议,元评论等。只需解释您的问题,并展示您尝试过的内容,预期的内容以及实际发生的情况。
标签: java spring-boot spring-webclient