-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathServer.java
More file actions
70 lines (59 loc) · 2.45 KB
/
Server.java
File metadata and controls
70 lines (59 loc) · 2.45 KB
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
package io.vertx.example.web.proxy;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.AuthenticationHandler;
import io.vertx.ext.web.handler.BasicAuthHandler;
import io.vertx.ext.web.proxy.handler.ProxyHandler;
import io.vertx.httpproxy.HttpProxy;
import io.vertx.launcher.application.VertxApplication;
/*
* @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
*/
public class Server extends VerticleBase {
public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}
@Override
public Future<?> start() throws Exception {
// deploy a dummy HTTP server that will be our backend
return vertx.deployVerticle(new Backend())
.compose(id -> {
Router router = Router.router(vertx);
router
// any request to "/foo" will in fact return the backend "/foo"
.get("/foo")
.handler(ProxyHandler.create(
// create a reverse proxy
HttpProxy.reverseProxy(vertx.createHttpClient())
// to the dummy backend
.origin(7070, "localhost")));
// Example #2 (Security before Proxying)
// Simple auth service which uses a properties file for user/role info
PropertyFileAuthentication authn = PropertyFileAuthentication.create(vertx, "vertx-users.properties");
AuthenticationHandler basicAuthHandler = BasicAuthHandler.create(authn);
// All requests to paths starting with '/private/' will be protected
router
.get("/private")
// the security is now handled before the proxy call executes
.handler(basicAuthHandler)
// any request to "/private" will in fact return the backend "/private"
.handler(ProxyHandler.create(
// create a reverse proxy
HttpProxy.reverseProxy(vertx.createHttpClient())
// to the dummy backend
.origin(7070, "localhost")));
router
.route()
.respond(ctx -> Future.succeededFuture("Hello World!"));
return vertx.createHttpServer()
.requestHandler(router)
.listen(8080)
.onSuccess(r -> {
System.out.println("Open http://127.0.0.1:8080/foo");
System.out.println("Protected (tim:sausages) http://127.0.0.1:8080/private");
});
});
}
}