小编典典
我一直在寻找与您要求相同的内容。到目前为止,我还没有在JDK上找到实现此目的的方法。
要求对Java
Bug数据库进行增强。查看该报告以了解是否得到了Sun的答复(对报告进行投票,以期尽快解决该问题)。
我最终要做的是覆盖sun.net.www.protocol.http.NTLMAuthentication类。通过查看sun.net.www.protocol.http.HttpURLAuthentication,我发现您唯一需要修改的就是以下结果:
NTLMAuthentication.supportsTransparentAuth()
true在Windows平台和false其他平台上,该方法具有硬编码的返回值。此代码是从Windows 7上安装的JDK中提取的:
static boolean supportsTransparentAuth()
{
return true;
}
该方法说明的是默认情况下是否应使用Windows凭据。如果设置为true, 则不会调用您自定义的Authenticator代码
。参见以下HttpURLConnection课程片段:
//Declared as a member variable of HttpURLConnection
private boolean tryTransparentNTLMServer = NTLMAuthentication.supportsTransparentAuth();
//Inside of getServerAuthentication method.
PasswordAuthentication a = null;
if (!tryTransparentNTLMServer) {
//If set to false, this will call Authenticator.requestPasswordAuthentication().
a = privilegedRequestPasswordAuthentication(url.getHost(), addr, port, url.getProtocol(), "", scheme, url, RequestorType.SERVER);
}
/* If we are not trying transparent authentication then
* we need to have a PasswordAuthentication instance. For
* transparent authentication (Windows only) the username
* and password will be picked up from the current logged
* on users credentials.
*/
if (tryTransparentNTLMServer || (!tryTransparentNTLMServer && a != null)) {
//If set to true or if Authenticator did not return any credentials, use Windows credentials.
//NTLMAuthentication constructor, if receives a == null will fetch current looged user credentials.
ret = new NTLMAuthentication(false, url1, a);
}
为了获得NTLMAuthentication源代码,我使用了此Java反编译器。打开位于JDK安装文件夹中的rt.jar,并复制所需的类代码。
然后,我只是更改supportsTransparentAuth为返回false。但是,非常希望此方法首先检查系统属性,然后根据该属性返回true或false。
要编译它,我只是将java文件放在sun / net / www / protocol / http文件夹结构下并运行:
javac NTLMAuthentication.java
然后使用以下命令运行我的应用程序:
java -Xbootclasspath:"path/to/your/sun/net/www/protocol/http/classes;normal/JDK/boot/directories"
这将告诉JVM
NTLMAuthentication在rt.jar中加载我们的实现。您必须注意不要错过带有的任何默认类加载路径-Xbootclasspath,否则会ClassNotFound出错。
之后,一切正常。
这种方法具有您应注意的重要缺点。
存在安全风险。任何人都可以在启动文件夹中放置其他.class文件,并窃取用户凭据或其他重要信息。
Sun软件包中的代码可能会更改,恕不另行通知,因此与您的更改不兼容。
如果部署此代码,则将违反Sun代码许可证。从文档中:
-Xbootclasspath:bootclasspath指定用分号分隔的目录,JAR存档和ZIP存档列表,以搜索引导类文件。这些代替Java 2
SDK中包含的引导类文件。注意:不应部署使用此选项以覆盖rt.jar中的类的应用程序,因为这样做会违反Java 2 Runtime
Environment二进制代码许可证。
因此,这绝对不适合生产环境。
最后,这是有关引导类路径选项和Java类加载器的绝佳资源:PDF
希望这可以帮助。
2020-09-24