1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8087112
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.common.HttpServerAdapters jdk.test.lib.net.SimpleSSLContext
* @run main/othervm BasicAuthTest
* @summary Basic Authentication Test
*/
import com.sun.net.httpserver.BasicAuthenticator;
import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.test.lib.net.SimpleSSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLContext;
import static java.nio.charset.StandardCharsets.US_ASCII;
public class BasicAuthTest implements HttpServerAdapters {
static volatile boolean ok;
static final String RESPONSE = "Hello world";
static final String POST_BODY = "This is the POST body 123909090909090";
public static void main(String[] args) throws Exception {
test(Version.HTTP_1_1, false);
test(Version.HTTP_2, false);
test(Version.HTTP_1_1, true);
test(Version.HTTP_2, true);
}
public static void test(Version version, boolean secure) throws Exception {
ExecutorService e = Executors.newCachedThreadPool();
Handler h = new Handler();
SSLContext sslContext = secure ? new SimpleSSLContext().get() : null;
HttpTestServer server = HttpTestServer.create(version, sslContext, e);
HttpTestContext serverContext = server.addHandler(h,"/test/");
ServerAuth sa = new ServerAuth("foo realm");
serverContext.setAuthenticator(sa);
server.start();
System.out.println("Server auth = " + server.serverAuthority());
ClientAuth ca = new ClientAuth();
var clientBuilder = HttpClient.newBuilder();
if (sslContext != null) clientBuilder.sslContext(sslContext);
HttpClient client = clientBuilder.authenticator(ca).build();
try {
String scheme = sslContext == null ? "http" : "https";
URI uri = new URI(scheme + "://" + server.serverAuthority() + "/test/foo/"+version);
var builder = HttpRequest.newBuilder(uri);
HttpRequest req = builder.copy().GET().build();
System.out.println("\n\nSending request: " + req);
HttpResponse resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
var count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);
// repeat same request, should succeed but no additional authenticator calls
System.out.println("\n\nRepeat request: " + req);
resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);
// try a POST
req = builder.copy().POST(BodyPublishers.ofString(POST_BODY))
.build();
System.out.println("\n\nSending POST request: " + req);
resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200;
count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);
} finally {
server.stop();
e.shutdownNow();
}
System.out.println("OK");
}
static class ServerAuth extends BasicAuthenticator {
ServerAuth(String realm) {
super(realm);
}
@Override
public boolean checkCredentials(String username, String password) {
if (!"user".equals(username) || !"passwd".equals(password)) {
return false;
}
return true;
}
}
static class ClientAuth extends java.net.Authenticator {
volatile int count = 0;
@Override
protected PasswordAuthentication getPasswordAuthentication() {
count++;
return new PasswordAuthentication("user", "passwd".toCharArray());
}
}
static class Handler implements HttpTestHandler {
static volatile boolean ok;
@Override
public void handle(HttpTestExchange he) throws IOException {
String method = he.getRequestMethod();
InputStream is = he.getRequestBody();
if (method.equalsIgnoreCase("POST")) {
String requestBody = new String(is.readAllBytes(), US_ASCII);
if (!requestBody.equals(POST_BODY)) {
he.sendResponseHeaders(500, 0);
ok = false;
} else {
he.sendResponseHeaders(200, 0);
ok = true;
}
} else { // GET
he.sendResponseHeaders(200, RESPONSE.length());
OutputStream os = he.getResponseBody();
os.write(RESPONSE.getBytes(US_ASCII));
os.close();
ok = true;
}
}
}
}
|