I Implemented a very simple HTTP Server as described here. I managed to get the authentication working by using the Authentication header but I cannot figure out how to take the credentials from a form and use them to authenticate with the server. How is this usually done?
Code:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.BasicAuthenticator;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class SimpleHttpServer3 {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/info", new InfoHandler());
HttpContext hc1 = server.createContext("/get", new GetHandler());
hc1.setAuthenticator(new BasicAuthenticator("get") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals("admin") && pwd.equals("password");
}
});
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("The server is running");
}
// http://localhost:8000/info
static class InfoHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Use /get to authenticate (user:admin pwd:password)";
SimpleHttpServer3.writeResponse(httpExchange, response.toString());
}
}
static class GetHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder response = new StringBuilder();
response.append("<html><body>");
response.append("hello " + httpExchange.getPrincipal().getUsername());
response.append("</body></html>");
SimpleHttpServer3.writeResponse(httpExchange, response.toString());
}
}
public static void writeResponse(HttpExchange httpExchange, String response) throws IOException {
httpExchange.sendResponseHeaders(200, response.length());
OutputStream os = httpExchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}