我看你都在问同样的事情。到目前为止,我还没有在JDK上找到这样做的方法。
没有为增强对Java的错误数据库的请求。看看report,看看是否得到了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
,您的自定义验证码不会被称为。见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
源代码,我用this Java decompiler。打开位于JDK安装文件夹中的rt.jar并复制所需的类代码。
然后,我只是改变supportsTransparentAuth
返回false。但是,如果此方法首先检查一个系统属性,然后基于此属性返回true或false,则将非常合意。
要编译它,我只是放置在太阳/网/网络/协议/ HTTP文件夹结构中的java文件并运行:
javac NTLMAuthentication.java
然后使用运行我的应用程序:
java -Xbootclasspath:"path/to/your/sun/net/www/protocol/http/classes;normal/JDK/boot/directories"
那会告诉JVM在rt.jar之前加载我们的NTLMAuthentication
实现。你必须小心,不要错过任何默认的类加载路径-Xbootclasspath
,否则会有ClassNotFound
错误。
之后,一切正常。
这种方法有很重要的缺点,您应该注意。
- 存在安全隐患。任何人都可以在引导文件夹中放置不同的.class文件,并窃取用户凭据或其他重要信息。
- Sun软件包中的代码可能会在没有通知的情况下更改,因此与您的更改不符。
- 如果您部署此代码,您将违反Sun代码许可证。从documentation:
-Xbootclasspath:启动类路径指定目录,JAR档案的分号分隔的列表,和ZIP文件搜索引导类 文件。这些用于代替包含在 Java 2 SDK中的引导类文件。注意:使用此选项的应用程序用于覆盖rt.jar中的类 不应该这样部署,因为 与Java 2运行时环境二进制代码许可证相冲突。
所以,这绝对不适合生产环境。
最后,这是关于引导类路径选项和Java类加载器的极好来源:PDF
希望这有助于。
您可以在'sun.net.www.protocol.http.HttpURLConnection'类上找到验证使用Windows凭据的代码。看看创建'NTLMAuthentication'实例和标志'tryTransparentNTLMServer'。我期待着和你一样做同样的事情,但我不知道该怎么做。 – jmend 2012-01-25 16:33:41