groovy (java) simple http server with basic auth
Run a simple web server from groovy:
def handler = new ScriptHttpHandler()
def server = HttpServer.create(new InetSocketAddress(8080), 0)
HttpContext context = server.createContext("/", handler)
context.setAuthenticator(new BasicAuthAuthenticator())
server.start()
Request handler:
class ScriptHttpHandler implements HttpHandler {
@Override
void handle(HttpExchange exchange) {
exchange.responseHeaders.set("Content-Type", "text/plain")
exchange.sendResponseHeaders(200, 0)
exchange.responseBody.withStream { OutputStream stream ->
doExecute(exchange.requestBody, stream)
}
}
protected void doExecute(InputStream input, OutputStream output) {
....
}
}
Authentication:
class BasicAuthAuthenticator extends BasicAuthenticator {
RemoteControlAuthenticator() {
super("enter password")
}
@Override
boolean checkCredentials(String user, String pwd) {
return user.equals("user") && pwd.equals("password")
}
}
Written by Vladimir Polyakov
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Groovy
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#