Search in sources :

Example 16 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    // the load function just populates some data on the storage
    loadData(mongo);
    // the app works 100% realtime
    vertx.eventBus().consumer("vtoons.listAlbums", this::listAlbums);
    vertx.eventBus().consumer("vtoons.placeOrder", this::placeOrder);
    Router router = Router.router(vertx);
    // We need cookies and sessions
    router.route().handler(CookieHandler.create());
    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
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, new JsonObject());
    // We need a user session handler too to make sure the user is stored in the session between requests
    router.route().handler(UserSessionHandler.create(authProvider));
    router.post("/login").handler(ctx -> {
        JsonObject credentials = ctx.getBodyAsJson();
        if (credentials == null) {
            // bad request
            ctx.fail(400);
            return;
        }
        // use the auth handler to perform the authentication for us
        authProvider.authenticate(credentials, login -> {
            // error handling
            if (login.failed()) {
                // forbidden
                ctx.fail(403);
                return;
            }
            ctx.setUser(login.result());
            ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json").end("{}");
        });
    });
    router.route("/eventbus/*").handler(ctx -> {
        // we need to be logged in
        if (ctx.user() == null) {
            ctx.fail(403);
        } else {
            ctx.next();
        }
    });
    // Allow outbound traffic to the vtoons addresses
    BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("vtoons.listAlbums")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.login")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.placeOrder").setRequiredAuthority("place_order")).addOutboundPermitted(new PermittedOptions());
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
    sockJSHandler.bridge(options);
    router.route("/eventbus/*").handler(sockJSHandler);
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) AuthProvider(io.vertx.ext.auth.AuthProvider) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 17 with AuthProvider

use of io.vertx.ext.auth.AuthProvider in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // We need cookies, sessions and request bodies
    router.route().handler(CookieHandler.create());
    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
    AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, new JsonObject());
    // We need a user session handler too to make sure the user is stored in the session between requests
    router.route().handler(UserSessionHandler.create(authProvider));
    // Any requests to URI starting '/private/' require login
    router.route("/private/*").handler(RedirectAuthHandler.create(authProvider, "/loginpage.html"));
    // Serve the static private pages from directory 'private'
    router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
    // Handles the actual login
    router.route("/loginhandler").handler(FormLoginHandler.create(authProvider));
    // Implement logout
    router.route("/logout").handler(context -> {
        context.clearUser();
        // Redirect back to the index page
        context.response().putHeader("location", "/").setStatusCode(302).end();
    });
    // Serve the non private static pages
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider)

Aggregations

AuthProvider (io.vertx.ext.auth.AuthProvider)17 JsonObject (io.vertx.core.json.JsonObject)15 SessionStore (io.vertx.ext.web.sstore.SessionStore)7 Test (org.junit.Test)7 HttpMethod (io.vertx.core.http.HttpMethod)5 ShiroAuth (io.vertx.ext.auth.shiro.ShiroAuth)5 RoutingContext (io.vertx.ext.web.RoutingContext)5 AsyncResult (io.vertx.core.AsyncResult)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 Buffer (io.vertx.core.buffer.Buffer)4 ClusterSerializable (io.vertx.core.shareddata.impl.ClusterSerializable)4 PRNG (io.vertx.ext.auth.PRNG)4 ShiroAuthRealmType (io.vertx.ext.auth.shiro.ShiroAuthRealmType)4 Router (io.vertx.ext.web.Router)4 Session (io.vertx.ext.web.Session)4 SessionImpl (io.vertx.ext.web.sstore.impl.SessionImpl)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4