Does vert.x have centralized filtering?

强颜欢笑 提交于 2019-12-13 14:55:33

问题


I am new to Vert.X.

Does Vert.x have a built in facility for centralized filters? What I mean are the kind of filters that you would use on a J2EE application.

For instance, all pages have to go through the auth filter, or something like that.

Is there a standardized way to achieve this in Vert.x?


回答1:


I know this question is quite old, but for those still looking for filter in Vertx 3, the solution is to use subRouter as a filter:

    // Your regular routes
    router.route("/").handler((ctx) -> {
        ctx.response().end("...");
    });
    // Have more routes here...

    Router filterRouter = Router.router(vertx);

    filterRouter.get().handler((ctx)->{
        // Do something smart

        // Forward to your actual router
        ctx.next();
    });
    filterRouter.mountSubRouter("/", router);



回答2:


Filtering is an implementation of the chain of responsibility in the servlet container. Vert.x does not have this kind of concept but with yoke (or apex in the new release) you are able to easily reproduce this behavior.

Give a look here in the routing section.

HTH,
Carlo




回答3:


Vert.x is unopinionated about how many things should be handled. But generally speaking, these types of features are typically implemented as "bus mods" (i.e. modules/verticles which receive input and produce output over the event bus) in Vert.x 2. In fact, the auth manager module may help you get a better understanding of how this is done: https://github.com/vert-x/mod-auth-mgr

In Vert.x 3 the module system will be/is gone, but the pattern will remain the same. It's possible that some higher level framework built on Vert.x could support these types of filters, but Vert.x core will not.

If also recommend you poke around in Vert.x Apex if you're getting started building web applications on Vert.x: https://github.com/vert-x3/vertx-apex




回答4:


From my POV, this is exactly the opposite to what vert.x tries to achieve. A verticle being the core building block of the framework is supposed to keep the functions distributed, rather than centralized.

For the multithreaded (cluster) async environment that makes sense, because as soon as you start introducing something "centralized" (and usually synchronous), you would lose the async ability.

One of the options is to implement auth in your case would be to exchange the messages with respecive auth-verticle over the event bus. In this case you would have to handle the async aspect of such a request.




回答5:


Vert.x is more similar to node.js than any other java based frameworks. Vert.x depends on middlewares. You can define them and attach them to a route. Depending on the order they are defined in they will get called.

For example lets say you have a user application where you would like to run logging and request verification before the controller is called. You can do something like follows:

Router router = Router.router(vertx);
router.route("/*").handler(BodyHandler.create()); // Allows Body to available in post calls
router.route().handler(new Handler<RoutingContext>() {
        @Override
        public void handle(RoutingContext event) {
            //Handle logs
        }
    })
router.route("/user").handler(new Handler<RoutingContext>() {
        @Override
        public void handle(RoutingContext event) {
            // handle verification for all apis starting with /user
        }
    });

Here depending on the route set of middleware will get called.



来源:https://stackoverflow.com/questions/29132464/does-vert-x-have-centralized-filtering

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!