Java 类com.sun.net.httpserver.BasicAuthenticator 实例源码

项目:NoraUi    文件:AuthUT.java   
public void start() {
    try {
        server = HttpServer.create(new InetSocketAddress(8000), 0);
    } catch (Exception e) {
        Assert.fail("Exception thrown: " + e.getMessage());
    }
    server.createContext("/unprotected", new TestHttpHandler());

    HttpContext protectedContext = server.createContext("/protected", new TestHttpHandler());
    protectedContext.setAuthenticator(new BasicAuthenticator("get") {

        @Override
        public boolean checkCredentials(String user, String pwd) {
            return user.equals("admin") && pwd.equals("admin");
        }

    });
    server.setExecutor(null);
    server.start();
}
项目:iotplatform    文件:RestApiCallDemoClient.java   
public static void main(String[] args) throws IOException {
  HttpServer server = HttpServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 0);

  HttpContext secureContext = server.createContext(DEMO_REST_BASIC_AUTH, new RestDemoHandler());
  secureContext.setAuthenticator(new BasicAuthenticator("demo-auth") {
    @Override
    public boolean checkCredentials(String user, String pwd) {
      return user.equals(USERNAME) && pwd.equals(PASSWORD);
    }
  });

  server.createContext(DEMO_REST_NO_AUTH, new RestDemoHandler());
  server.setExecutor(null);
  System.out.println("[*] Waiting for messages.");
  server.start();
}
项目:jdk8u-jdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:openjdk-jdk10    文件:MultiAuthTest.java   
static HttpServer createServer(ExecutorService e, BasicAuthenticator sa) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
    Handler h = new Handler();
    HttpContext serverContext = server.createContext("/test", h);
    serverContext.setAuthenticator(sa);
    server.setExecutor(e);
    server.start();
    return server;
}
项目:openjdk-jdk10    文件:HTTPTestServer.java   
static void setContextAuthenticator(HttpContext ctxt,
                                    HttpTestAuthenticator auth) {
    final String realm = auth.getRealm();
    com.sun.net.httpserver.Authenticator authenticator =
        new BasicAuthenticator(realm) {
            @Override
            public boolean checkCredentials(String username, String pwd) {
                return auth.getUserName().equals(username)
                       && new String(auth.getPassword(username)).equals(pwd);
            }
    };
    ctxt.setAuthenticator(authenticator);
}
项目:openjdk-jdk10    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:openjdk9    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:jdk8u_jdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:lookaside_java-1.8.0-openjdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:oreva    文件:BasicAuthenticationTest.java   
private Authenticator getAuthenticator() {
  Authenticator authenticator = new BasicAuthenticator(REALM) {
    public boolean checkCredentials(String username, String password) {
      return USERNAME.equals(username) && PASSWORD.equals(password);
    }
  };
  return authenticator;
}
项目:infobip-open-jdk-8    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:jdk8u-dev-jdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:ant-http    文件:AbstractHttpServerTest.java   
protected BasicAuthenticator getBasicAuthenticator() {
  return new BasicAuthenticator("Test Realm") {
    @Override
    public boolean checkCredentials(final String username, final String password) {
      return USERNAME.equals(username) && PASSWORD.equals(password);
    }
  };
}
项目:ant-ivy    文件:TestHelper.java   
/**
 * Creates a HTTP server, backed by a local file system, which can be used as a repository to
 * serve Ivy module descriptors and artifacts. The context within the server will be backed by
 * {@code BASIC} authentication mechanism with {@code realm} as the realm and
 * {@code validCredentials} as the credentials that the server will recognize. The server will
 * allow access to resources, only if the credentials that are provided by the request, belong
 * to these credentials.
 * <p>
 * NOTE: This is supposed to be used only in test cases and only a limited functionality is
 * added in the handler(s) backing the server
 *
 * @param serverAddress           The address to which the server will be bound
 * @param webAppContext           The context root of the application which will be handling
 *                                the requests to the server
 * @param localFilesystemRepoRoot The path to the root directory containing the module
 *                                descriptors and artifacts
 * @param realm                   The realm to use for the {@code BASIC} auth mechanism
 * @param validCredentials        A {@link Map} of valid credentials, the key being the user
 *                                name and the value being the password, that the server will
 *                                use during the authentication process of the incoming requests
 * @return AutoCloseable
 * @throws IOException if something goes wrong
 */
public static AutoCloseable createBasicAuthHttpServerBackedRepo(final InetSocketAddress serverAddress, final String webAppContext,
                                                                final Path localFilesystemRepoRoot, final String realm,
                                                                final Map<String, String> validCredentials) throws IOException {
    final LocalFileRepoOverHttp handler = new LocalFileRepoOverHttp(webAppContext, localFilesystemRepoRoot);
    final HttpServer server = HttpServer.create(serverAddress, -1);
    // setup the handler
    final HttpContext context = server.createContext(webAppContext, handler);
    // setup basic auth on this context
    final com.sun.net.httpserver.Authenticator authenticator = new BasicAuthenticator(realm) {
        @Override
        public boolean checkCredentials(final String user, final String pass) {
            if (validCredentials == null || !validCredentials.containsKey(user)) {
                return false;
            }
            final String expectedPass = validCredentials.get(user);
            return expectedPass != null && expectedPass.equals(pass);
        }
    };
    context.setAuthenticator(authenticator);
    // setup a auth filter backed by the authenticator
    context.getFilters().add(new AuthFilter(authenticator));
    // start the server
    server.start();
    return new AutoCloseable() {
        @Override
        public void close() throws Exception {
            final int delaySeconds = 0;
            server.stop(delaySeconds);
        }
    };
}
项目:jdk7-jdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:openjdk-source-code-learn    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:OLD-OpenJDK8    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:JAVA_UNIT    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:openjdk-jdk7u-jdk    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}
项目:openjdk-icedtea7    文件:BasicLongCredentials.java   
public static void main (String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);

        BasicAuthenticator a = new BasicAuthenticator(REALM) {
            public boolean checkCredentials (String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) { c ++; }

        if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
        if (error) { throw new RuntimeException("Test failed: error"); }

        System.out.println ("OK");
    } finally {
        server.stop(0);
    }
}