vertx Session don't create cookies in my browser

1k Views Asked by At

My vertx server is in Java and when i want to create a Session with Js my browser doesn't have a cookie with the key. But when I create a session directly with the URL, my browser has a cookie. My server:

    router.route().handler(CookieHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx));
    router.get("/log/:name/:password").handler(this::verifId);

public void verifId(RoutingContext routingContext) {
        HttpServerRequest request = routingContext.request();
        HttpServerResponse response = routingContext.response();
        try {
            Session s = routingContext.session();
            DataBase.connect();
            String name = request.getParam("name");
            String password = request.getParam("password");
            Boolean returnValue = DataBase.exist(name, password);
            DataBase.disconnect();
            if (returnValue) {
                s.put("user", request.getParam("name"));
                System.out.println(s.id());
                response.end(Json.encodePrettily(s.id().toString()));
            } else {
                s.destroy();
                response.end("null");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

And My Js code:

function tryToLogin(login, password){

    var urlBuilder = 'https://localhost:8090/log/' + login + '/' + password;

     $.ajax({
        url : urlBuilder,
        type: 'get',
        dataType: 'json',

        success : function(code_json, statut){

            console.log(" il a repondu " + code_json);
        }

    });
}
1

There are 1 best solutions below

0
On

The only cookie Vertx sets that way is "vertx-web.session". You're not setting any other cookies in your example.

To check that the cookie is valid you can use the following code:

    Vertx vertx = Vertx.vertx();

    Router router = Router.router(vertx);
    router.route().handler(CookieHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));

    router.get("/login").handler((r) -> {
        System.out.println("Got request");
        System.out.println("Session cookie " + r.getCookie("vertx-web.session").getValue());
       r.response().end("OK");
    });
    router.route("/*").handler(StaticHandler.create());

    vertx.createHttpServer().requestHandler(router::accept).listen(8888);

And JS client:

    var urlBuilder = '/login';

    $.ajax({
        url : urlBuilder,
        type: 'GET',

        success : function(code_json, statut){

            console.log("got" + code_json);
        }

    });

You'll get:

Session cookie <UUID>

And :

gotOK 

In JavaScript console