author | weijun |
Thu, 18 Mar 2010 18:26:37 +0800 | |
changeset 5154 | 07af3c279166 |
parent 4236 | 02f52c723b79 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
1 |
/* |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
2 |
* Copyright 2009-2010 Sun Microsystems, Inc. All Rights Reserved. |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
4 |
* |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
8 |
* |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
13 |
* accompanied this code). |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
14 |
* |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
18 |
* |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
21 |
* have any questions. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
22 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
23 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
24 |
/* |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
25 |
* @test |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
26 |
* @bug 6578647 6829283 |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
27 |
* @summary Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication() |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
28 |
* @summary HTTP/Negotiate: Authenticator triggered again when user cancels the first one |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
29 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
30 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
31 |
import com.sun.net.httpserver.Headers; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
32 |
import com.sun.net.httpserver.HttpContext; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
33 |
import com.sun.net.httpserver.HttpExchange; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
34 |
import com.sun.net.httpserver.HttpHandler; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
35 |
import com.sun.net.httpserver.HttpServer; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpPrincipal; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
37 |
import com.sun.security.auth.module.Krb5LoginModule; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
38 |
import java.io.BufferedReader; |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
39 |
import java.io.File; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
40 |
import java.io.FileOutputStream; |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
41 |
import java.io.IOException; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
42 |
import java.io.InputStreamReader; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
43 |
import java.net.HttpURLConnection; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
44 |
import java.net.InetSocketAddress; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
45 |
import java.net.PasswordAuthentication; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
46 |
import java.net.Proxy; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
47 |
import java.net.URL; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
48 |
import java.security.PrivilegedExceptionAction; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
49 |
import java.util.HashMap; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
50 |
import java.util.Map; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
51 |
import javax.security.auth.Subject; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
52 |
import org.ietf.jgss.GSSContext; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
53 |
import org.ietf.jgss.GSSCredential; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
54 |
import org.ietf.jgss.GSSManager; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
55 |
import sun.security.jgss.GSSUtil; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
56 |
import sun.security.krb5.Config; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
57 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
58 |
/** |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
59 |
* Basic JGSS/krb5 test with 3 parties: client, server, backend server. Each |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
60 |
* party uses JAAS login to get subjects and executes JGSS calls using |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
61 |
* Subject.doAs. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
62 |
*/ |
3046 | 63 |
public class HttpNegotiateServer { |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
64 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
65 |
// Two realm, web server in one, proxy server in another |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
66 |
final static String REALM_WEB = "WEB.DOMAIN"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
67 |
final static String REALM_PROXY = "PROXY.DOMAIN"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
68 |
final static String KRB5_CONF = "web.conf"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
69 |
final static String KRB5_TAB = "web.ktab"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
70 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
71 |
// user principals |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
72 |
final static String WEB_USER = "web"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
73 |
final static char[] WEB_PASS = "webby".toCharArray(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
74 |
final static String PROXY_USER = "pro"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
75 |
final static char[] PROXY_PASS = "proxy".toCharArray(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
76 |
final static int WEB_PORT = 17840; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
77 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
78 |
final static String WEB_HOST = "host.web.domain"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
79 |
final static String PROXY_HOST = "host.proxy.domain"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
80 |
final static int PROXY_PORT = 17841; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
81 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
82 |
// web page content |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
83 |
final static String CONTENT = "Hello, World!"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
84 |
|
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
85 |
// For 6829283, count how many times the Authenticator is called. |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
86 |
static int count = 0; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
87 |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
88 |
// URLs for web test, proxy test. The proxy server is not a real proxy |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
89 |
// since it fakes the same content for any URL. :) |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
90 |
final static URL webUrl, proxyUrl; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
91 |
static { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
92 |
URL u1 = null, u2 = null; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
93 |
try { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
94 |
u1 = new URL("http://" + WEB_HOST +":" + WEB_PORT + "/a/b/c"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
95 |
u2 = new URL("http://nosuchplace/a/b/c"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
96 |
} catch (Exception e) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
97 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
98 |
webUrl = u1; proxyUrl = u2; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
99 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
100 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
101 |
/** |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
102 |
* This Authenticator checks everything: |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
103 |
* scheme, protocol, requestor type, host, port, and url |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
104 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
105 |
static class KnowAllAuthenticator extends java.net.Authenticator { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
106 |
public PasswordAuthentication getPasswordAuthentication () { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
107 |
if (!getRequestingScheme().equalsIgnoreCase("Negotiate")) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
108 |
throw new RuntimeException("Bad scheme"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
109 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
110 |
if (!getRequestingProtocol().equalsIgnoreCase("HTTP")) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
111 |
throw new RuntimeException("Bad protocol"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
112 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
113 |
if (getRequestorType() == RequestorType.SERVER) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
114 |
if (!this.getRequestingHost().equalsIgnoreCase(webUrl.getHost())) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
115 |
throw new RuntimeException("Bad host"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
116 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
117 |
if (this.getRequestingPort() != webUrl.getPort()) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
118 |
throw new RuntimeException("Bad port"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
119 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
120 |
if (!this.getRequestingURL().equals(webUrl)) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
121 |
throw new RuntimeException("Bad url"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
122 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
123 |
return new PasswordAuthentication( |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
124 |
WEB_USER+"@"+REALM_WEB, WEB_PASS); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
125 |
} else if (getRequestorType() == RequestorType.PROXY) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
126 |
if (!this.getRequestingHost().equalsIgnoreCase(PROXY_HOST)) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
127 |
throw new RuntimeException("Bad host"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
128 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
129 |
if (this.getRequestingPort() != PROXY_PORT) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
130 |
throw new RuntimeException("Bad port"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
131 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
132 |
if (!this.getRequestingURL().equals(proxyUrl)) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
133 |
throw new RuntimeException("Bad url"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
134 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
135 |
return new PasswordAuthentication( |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
136 |
PROXY_USER+"@"+REALM_PROXY, PROXY_PASS); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
137 |
} else { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
138 |
throw new RuntimeException("Bad requster type"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
139 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
140 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
141 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
142 |
|
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
143 |
/** |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
144 |
* This Authenticator knows nothing |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
145 |
*/ |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
146 |
static class KnowNothingAuthenticator extends java.net.Authenticator { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
147 |
@Override |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
148 |
public PasswordAuthentication getPasswordAuthentication () { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
149 |
HttpNegotiateServer.count++; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
150 |
return null; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
151 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
152 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
153 |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
154 |
public static void main(String[] args) |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
155 |
throws Exception { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
156 |
|
3046 | 157 |
KDC kdcw = KDC.create(REALM_WEB); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
158 |
kdcw.addPrincipal(WEB_USER, WEB_PASS); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
159 |
kdcw.addPrincipalRandKey("krbtgt/" + REALM_WEB); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
160 |
kdcw.addPrincipalRandKey("HTTP/" + WEB_HOST); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
161 |
|
3046 | 162 |
KDC kdcp = KDC.create(REALM_PROXY); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
163 |
kdcp.addPrincipal(PROXY_USER, PROXY_PASS); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
164 |
kdcp.addPrincipalRandKey("krbtgt/" + REALM_PROXY); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
165 |
kdcp.addPrincipalRandKey("HTTP/" + PROXY_HOST); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
166 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
167 |
KDC.saveConfig(KRB5_CONF, kdcw, kdcp, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
168 |
"default_keytab_name = " + KRB5_TAB, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
169 |
"[domain_realm]", |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
170 |
"", |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
171 |
".web.domain="+REALM_WEB, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
172 |
".proxy.domain="+REALM_PROXY); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
173 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
174 |
System.setProperty("java.security.krb5.conf", KRB5_CONF); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
175 |
Config.refresh(); |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
176 |
KDC.writeMultiKtab(KRB5_TAB, kdcw, kdcp); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
177 |
|
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
178 |
// Write a customized JAAS conf file, so that any kinit cache |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
179 |
// will be ignored. |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
180 |
System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
181 |
File f = new File(OneKDC.JAAS_CONF); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
182 |
FileOutputStream fos = new FileOutputStream(f); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
183 |
fos.write(( |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
184 |
"com.sun.security.jgss.krb5.initiate {\n" + |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
185 |
" com.sun.security.auth.module.Krb5LoginModule required;\n};\n" |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
186 |
).getBytes()); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
187 |
fos.close(); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
188 |
f.deleteOnExit(); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
189 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
190 |
HttpServer h1 = httpd(WEB_PORT, "Negotiate", false, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
191 |
"HTTP/" + WEB_HOST + "@" + REALM_WEB, KRB5_TAB); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
192 |
HttpServer h2 = httpd(PROXY_PORT, "Negotiate", true, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
193 |
"HTTP/" + PROXY_HOST + "@" + REALM_PROXY, KRB5_TAB); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
194 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
195 |
try { |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
196 |
Exception e1 = null, e2 = null; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
197 |
try { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
198 |
test6578647(); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
199 |
} catch (Exception e) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
200 |
e1 = e; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
201 |
e.printStackTrace(); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
202 |
} |
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
203 |
try { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
204 |
test6829283(); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
205 |
} catch (Exception e) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
206 |
e2 = e; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
207 |
e.printStackTrace(); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
208 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
209 |
if (e1 != null || e2 != null) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
210 |
throw new RuntimeException("Test error"); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
211 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
212 |
} finally { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
213 |
// Must stop. Seems there's no HttpServer.startAsDaemon() |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
214 |
if (h1 != null) h1.stop(0); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
215 |
if (h2 != null) h2.stop(0); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
216 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
217 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
218 |
|
5154
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
219 |
static void test6578647() throws Exception { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
220 |
BufferedReader reader; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
221 |
java.net.Authenticator.setDefault(new KnowAllAuthenticator()); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
222 |
|
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
223 |
reader = new BufferedReader(new InputStreamReader( |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
224 |
webUrl.openConnection().getInputStream())); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
225 |
if (!reader.readLine().equals(CONTENT)) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
226 |
throw new RuntimeException("Bad content"); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
227 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
228 |
|
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
229 |
reader = new BufferedReader(new InputStreamReader( |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
230 |
proxyUrl.openConnection( |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
231 |
new Proxy(Proxy.Type.HTTP, |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
232 |
new InetSocketAddress(PROXY_HOST, PROXY_PORT))) |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
233 |
.getInputStream())); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
234 |
if (!reader.readLine().equals(CONTENT)) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
235 |
throw new RuntimeException("Bad content"); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
236 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
237 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
238 |
|
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
239 |
static void test6829283() throws Exception { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
240 |
BufferedReader reader; |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
241 |
java.net.Authenticator.setDefault(new KnowNothingAuthenticator()); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
242 |
try { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
243 |
new BufferedReader(new InputStreamReader( |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
244 |
webUrl.openConnection().getInputStream())); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
245 |
} catch (IOException ioe) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
246 |
// Will fail since no username and password is provided. |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
247 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
248 |
if (count > 1) { |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
249 |
throw new RuntimeException("Authenticator called twice"); |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
250 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
251 |
} |
07af3c279166
6829283: HTTP/Negotiate: Autheticator triggered again when user cancels the first one
weijun
parents:
4236
diff
changeset
|
252 |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
253 |
/** |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
254 |
* Creates and starts an HTTP or proxy server that requires |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
255 |
* Negotiate authentication. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
256 |
* @param scheme "Negotiate" or "Kerberos" |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
257 |
* @param principal the krb5 service principal the server runs with |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
258 |
* @return the server |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
259 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
260 |
public static HttpServer httpd(int port, String scheme, boolean proxy, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
261 |
String principal, String ktab) throws Exception { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
262 |
MyHttpHandler h = new MyHttpHandler(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
263 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
264 |
HttpContext hc = server.createContext("/", h); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
265 |
hc.setAuthenticator(new MyServerAuthenticator( |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
266 |
proxy, scheme, principal, ktab)); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
267 |
server.start(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
268 |
return server; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
269 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
270 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
271 |
static class MyHttpHandler implements HttpHandler { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
272 |
public void handle(HttpExchange t) throws IOException { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
273 |
t.sendResponseHeaders(200, 0); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
274 |
t.getResponseBody().write(CONTENT.getBytes()); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
275 |
t.close(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
276 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
277 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
278 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
279 |
static class MyServerAuthenticator |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
280 |
extends com.sun.net.httpserver.Authenticator { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
281 |
Subject s = new Subject(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
282 |
GSSManager m = null; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
283 |
GSSCredential cred = null; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
284 |
String scheme = null; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
285 |
String reqHdr = "WWW-Authenticate"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
286 |
String respHdr = "Authorization"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
287 |
int err = HttpURLConnection.HTTP_UNAUTHORIZED; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
288 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
289 |
public MyServerAuthenticator(boolean proxy, String scheme, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
290 |
String principal, String ktab) throws Exception { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
291 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
292 |
this.scheme = scheme; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
293 |
if (proxy) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
294 |
reqHdr = "Proxy-Authenticate"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
295 |
respHdr = "Proxy-Authorization"; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
296 |
err = HttpURLConnection.HTTP_PROXY_AUTH; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
297 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
298 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
299 |
Krb5LoginModule krb5 = new Krb5LoginModule(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
300 |
Map<String, String> map = new HashMap<String, String>(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
301 |
Map<String, Object> shared = new HashMap<String, Object>(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
302 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
303 |
map.put("storeKey", "true"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
304 |
map.put("isInitiator", "false"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
305 |
map.put("useKeyTab", "true"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
306 |
map.put("keyTab", ktab); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
307 |
map.put("principal", principal); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
308 |
krb5.initialize(s, null, shared, map); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
309 |
krb5.login(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
310 |
krb5.commit(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
311 |
m = GSSManager.getInstance(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
312 |
cred = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
313 |
@Override |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
314 |
public GSSCredential run() throws Exception { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
315 |
System.err.println("Creating GSSCredential"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
316 |
return m.createCredential( |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
317 |
null, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
318 |
GSSCredential.INDEFINITE_LIFETIME, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
319 |
MyServerAuthenticator.this.scheme.equalsIgnoreCase("Negotiate")? |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
320 |
GSSUtil.GSS_SPNEGO_MECH_OID: |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
321 |
GSSUtil.GSS_KRB5_MECH_OID, |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
322 |
GSSCredential.ACCEPT_ONLY); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
323 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
324 |
}); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
325 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
326 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
327 |
@Override |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
328 |
public Result authenticate(HttpExchange exch) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
329 |
// The GSContext is stored in an HttpContext attribute named |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
330 |
// "GSSContext" and is created at the first request. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
331 |
GSSContext c = null; |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
332 |
String auth = exch.getRequestHeaders().getFirst(respHdr); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
333 |
try { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
334 |
c = (GSSContext)exch.getHttpContext().getAttributes().get("GSSContext"); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
335 |
if (auth == null) { // First request |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
336 |
Headers map = exch.getResponseHeaders(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
337 |
map.set (reqHdr, scheme); // Challenge! |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
338 |
c = Subject.doAs(s, new PrivilegedExceptionAction<GSSContext>() { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
339 |
@Override |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
340 |
public GSSContext run() throws Exception { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
341 |
return m.createContext(cred); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
342 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
343 |
}); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
344 |
exch.getHttpContext().getAttributes().put("GSSContext", c); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
345 |
return new com.sun.net.httpserver.Authenticator.Retry(err); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
346 |
} else { // Later requests |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
347 |
byte[] token = new sun.misc.BASE64Decoder() |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
348 |
.decodeBuffer(auth.split(" ")[1]); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
349 |
token = c.acceptSecContext(token, 0, token.length); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
350 |
Headers map = exch.getResponseHeaders(); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
351 |
map.set (reqHdr, scheme + " " + new sun.misc.BASE64Encoder() |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
352 |
.encode(token).replaceAll("\\s", "")); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
353 |
if (c.isEstablished()) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
354 |
return new com.sun.net.httpserver.Authenticator.Success( |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
355 |
new HttpPrincipal(c.getSrcName().toString(), "")); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
356 |
} else { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
357 |
return new com.sun.net.httpserver.Authenticator.Retry(err); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
358 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
359 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
360 |
} catch (Exception e) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
361 |
throw new RuntimeException(e); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
362 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
363 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
364 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
diff
changeset
|
365 |
} |