Authenticator#getPasswordAuthentication returns 基本而不是 Weblogic 中的 ntlm

Authenticator#getPasswordAuthentication returns basic instead of ntlm in Weblogic

我们有来自 Java 的 NTLM 身份验证,针对 MS Sharepoint 在所有环境中工作,但来自 Weblogic Server 内部。

在 WLS 中,我们看到 Authenticator#getPasswordAuthentication returns 'basic' 而不是 'ntlm'。这种行为的原因是什么?如果 运行 独立或来自 Tomcat(使用相同的 JVM),相同的代码工作得很好。

相关代码如下:

NtlmAuthenticator authenticator = new NtlmAuthenticator(configParameters.getNtlmUsername(),
    configParameters.getNtlmPassword(), configParameters.getNtlmDomain());

log.info("JVM running with security manager enabled: {}", System.getSecurityManager() != null);
// requires NetPermission 'setDefaultAuthenticator' if security manager enabled
Authenticator.setDefault(authenticator);


public class NtlmAuthenticator extends Authenticator {

  private char[] password;
  private String userAuthentication;

  public NtlmAuthenticator(String username, String password, String domain) {
    userAuthentication = username;
    if (StringUtils.isNotBlank(domain)) {
      // According to
      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx
      userAuthentication = domain + "\" + username;
    }
    this.password = password.toCharArray();
  }

  @Override
  public PasswordAuthentication getPasswordAuthentication() {
    log.debug("Scheme: '{}'", getRequestingScheme());
    return new PasswordAuthentication(userAuthentication, password);
  }
}

简答1

使用补丁版本的 Apache httpclient:https://issues.apache.org/jira/browse/HTTPCLIENT-1881

简答2

启动 WebLogic Server 时设置 -DUseSunHttpHandler=true。一些参考文献:https://docs.oracle.com/en/cloud/paas/javase-cloud/csjsu/you-should-now-set-sun-http-handlers-property-value-true-when-making-outbound-http-s-calls.html, & (都比较老)。

在(远程)调试过程中的某个时候,我注意到堆栈上没有 java.net.http.* 个对象,但是 weblogic.net.http.*. WTF 我想……是的,WLS 确实取代了标准的 Sun 网络堆栈 默认