Path: blob/master/test/jdk/java/net/Authenticator/B4921848.java
41149 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 492184826* @library /test/lib27* @run main/othervm -Dhttp.auth.preference=basic B492184828* @run main/othervm -Djava.net.preferIPv6Addresses=true29* -Dhttp.auth.preference=basic B492184830* @summary Allow user control over authentication schemes31*/3233import java.io.IOException;34import java.io.InputStream;35import java.io.PrintWriter;36import java.net.Authenticator;37import java.net.InetAddress;38import java.net.InetSocketAddress;39import java.net.PasswordAuthentication;40import java.net.ProxySelector;41import java.net.URL;42import java.net.URLConnection;43import java.util.concurrent.Executors;4445import com.sun.net.httpserver.HttpExchange;46import com.sun.net.httpserver.HttpHandler;47import com.sun.net.httpserver.HttpServer;48import jdk.test.lib.net.URIBuilder;4950public class B4921848 implements HttpHandler {5152static int count = 0;5354public void handle (HttpExchange req) {55try {56if (count == 0 ) {57req.getResponseHeaders().set("Connection", "close");58req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"foo\"");59req.getResponseHeaders().add("WWW-Authenticate", "Digest realm=\"bar\" domain=/biz nonce=\"hereisanonce\"");60req.sendResponseHeaders(401, -1);61} else {62String authheader = req.getRequestHeaders().get("Authorization").get(0);63if (authheader.startsWith ("Basic")) {64req.sendResponseHeaders(200, 0);65try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {66pw.print("Hello .");67}68} else {69req.sendResponseHeaders(400, -1);70}71}72count ++;73} catch (IOException e) {74e.printStackTrace();75}76}7778static void read (InputStream is) throws IOException {79int c;80System.out.println ("reading");81while ((c=is.read()) != -1) {82System.out.write (c);83}84System.out.println ("");85System.out.println ("finished reading");86}878889static void client (String u) throws Exception {90URL url = new URL (u);91System.out.println ("client opening connection to: " + u);92URLConnection urlc = url.openConnection ();93InputStream is = urlc.getInputStream ();94read (is);95is.close();96}9798static HttpServer server;99100public static void main (String[] args) throws Exception {101B4921848 b4921848 = new B4921848();102MyAuthenticator auth = new MyAuthenticator ();103Authenticator.setDefault (auth);104ProxySelector.setDefault(ProxySelector.of(null)); // no proxy105try {106InetAddress loopback = InetAddress.getLoopbackAddress();107server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);108server.createContext("/", b4921848);109server.setExecutor(Executors.newSingleThreadExecutor());110server.start();111String serverURL = URIBuilder.newBuilder()112.scheme("http")113.loopback()114.port(server.getAddress().getPort())115.path("/")116.build()117.toString();118System.out.println("Server: listening at: " + serverURL);119client(serverURL + "d1/d2/d3/foo.html");120} catch (Exception e) {121if (server != null) {122server.stop(1);123}124throw e;125}126server.stop(1);127}128129public static void except (String s) {130server.stop(1);131throw new RuntimeException (s);132}133134static class MyAuthenticator extends Authenticator {135MyAuthenticator () {136super ();137}138139public PasswordAuthentication getPasswordAuthentication () {140return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));141}142143}144145}146147148