Search in sources :

Example 1 with BasicAuthHandler

use of io.vertx.ext.web.handler.BasicAuthHandler in project vertx-swagger by bobxwang.

the class RestApiVerticle method configRoute.

private void configRoute(final Router router) {
    router.get("/logout").handler(context -> {
        context.clearUser();
        context.response().setStatusCode(302).putHeader("Location", "/").end();
    });
    AuthProvider authProvider = ChainAuth.create();
    AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
    router.route("/private/*").handler(basicAuthHandler);
    Route route = router.route(HttpMethod.GET, "/private/path/");
    route.handler(ctx -> ctx.response().end(new JsonObject().put("rs", new Date().toString()).encodePrettily()));
    router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        // 由于我们会在不同的处理器里写入响应,因此需要启用分块传输,仅当需要通过多个处理器输出响应时才需要
        response.setChunked(true);
        response.write("route1\n");
        // 1 秒后调用下一个处理器
        routingContext.vertx().setTimer(1000, tid -> routingContext.next());
    });
    router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response.write("route2\n");
        // 1 秒后调用下一个处理器
        routingContext.vertx().setTimer(1000, tid -> routingContext.next());
    });
    router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response.write("route3");
        // 结束响应
        routingContext.response().end();
    });
    router.route(HttpMethod.GET, "/long/time").blockingHandler(ctx -> {
        // call other service maybe long time, so here we using block pattern
        try {
            Thread.sleep(5000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ctx.response().end(new JsonObject().put("rs", new Date().toString()).encodePrettily());
    });
    // 如果您需要在一个阻塞处理器中处理一个 multipart 类型的表单数据,您需要首先使用一个非阻塞的处理器来调用 setExpectMultipart(true)
    router.post("/some/longtime/endpoint").handler(ctx -> {
        ctx.request().setExpectMultipart(true);
        ctx.next();
    }).blockingHandler(ctx -> {
    // 执行某些阻塞操作
    });
}
Also used : AuthHandler(io.vertx.ext.web.handler.AuthHandler) BasicAuthHandler(io.vertx.ext.web.handler.BasicAuthHandler) Route(io.vertx.ext.web.Route) AuthHandler(io.vertx.ext.web.handler.AuthHandler) ChainAuth(io.vertx.ext.auth.ChainAuth) Date(java.util.Date) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) Router(io.vertx.ext.web.Router) Autowired(org.springframework.beans.factory.annotation.Autowired) RoutingContext(io.vertx.ext.web.RoutingContext) Scope(org.springframework.context.annotation.Scope) AuthProvider(io.vertx.ext.auth.AuthProvider) BasicAuthHandler(io.vertx.ext.web.handler.BasicAuthHandler) Component(org.springframework.stereotype.Component) BBRouter(com.bob.vertx.swagger.BBRouter) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) JsonObject(io.vertx.core.json.JsonObject) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) AuthProvider(io.vertx.ext.auth.AuthProvider) Route(io.vertx.ext.web.Route) Date(java.util.Date)

Aggregations

BBRouter (com.bob.vertx.swagger.BBRouter)1 ApiImplicitParam (io.swagger.annotations.ApiImplicitParam)1 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 ChainAuth (io.vertx.ext.auth.ChainAuth)1 Route (io.vertx.ext.web.Route)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 AuthHandler (io.vertx.ext.web.handler.AuthHandler)1 BasicAuthHandler (io.vertx.ext.web.handler.BasicAuthHandler)1 Date (java.util.Date)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 Scope (org.springframework.context.annotation.Scope)1 Component (org.springframework.stereotype.Component)1