-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathServer.java
57 lines (43 loc) · 1.88 KB
/
Server.java
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
package io.vertx.example.web.auth;
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.*;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.launcher.application.VertxApplication;
/*
* @author <a href="http://tfox.org">Tim Fox</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 {
Router router = Router.router(vertx);
// We need sessions and request bodies
router.route().handler(BodyHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
// Simple auth service which uses a properties file for user/role info
PropertyFileAuthentication authn = PropertyFileAuthentication.create(vertx, "vertx-users.properties");
// Any requests to URI starting '/private/' require login
router.route("/private/*").handler(RedirectAuthHandler.create(authn, "/loginpage.html"));
// Serve the static private pages from directory 'private'
router.route("/private/*").handler(StaticHandler.create("io/vertx/example/web/auth/private").setCachingEnabled(false));
// Handles the actual login
router.route("/loginhandler").handler(FormLoginHandler.create(authn));
// Implement logout
router.route("/logout").handler(context -> {
context.user().clear();
// Redirect back to the index page
context.response().putHeader("location", "/").setStatusCode(302).end();
});
// Serve the non private static pages
router.route().handler(StaticHandler.create("io/vertx/example/web/auth/webroot"));
return vertx
.createHttpServer()
.requestHandler(router)
.listen(8080);
}
}