Vert.x Session Mangement

877 views
Skip to first unread message

Sumit Sinha

unread,
Aug 14, 2015, 2:06:06 AM8/14/15
to vert.x

I am using vertx in backend and angular.js for my frontend. Angular.js is running in tomcat. Vertx server takes action based on post and get request. The problem is I am getting different session id for each request. Following is the code snippet from my LoginFormHandler class handle routine.

            authProvider.authenticate(authInfo, res -> {
          if (res.succeeded()) {
            Session session = context.session();
            io.vertx.ext.auth.User user = res.result();
            session.put("user", user);
            req.response().setStatusCode(204).end("user Login success");

I am putting user object inside the current session. Then I move to new page and send a post request to the vertx server. Inside that post handler, I am trying to get the session object.

                Session session = context.session();
                io.vertx.ext.auth.User user = session.get("user");

I am not getting the user. Also when I print session ID. I get different values for both session. I have following code in start routine for the thread.

router.route().handler(CookieHandler.create());
router.route().handler(
                SessionHandler.create(LocalSessionStore.create(vertx)));
AuthProvider ap = new MyAuthProvier();
router.route().handler(UserSessionHandler.create(ap));
AuthHandler basicAuthHandler = BasicAuthHandler.create(ap);
router.route("/Services/rest/user/auth").handler(MyFormLoginHandler.create(ap));
router.route("/Services/*").handler(basicAuthHandler);

Tim Fox

unread,
Aug 14, 2015, 2:58:01 AM8/14/15
to ve...@googlegroups.com
Can you provider a complete reproducer please?

Also.... how many servers are you running?
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/eab4e0fb-cda4-4705-939a-cf455aa4205c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Fox

unread,
Aug 14, 2015, 3:00:51 AM8/14/15
to ve...@googlegroups.com
BTW - there is an example here:

https://github.com/vert-x3/vertx-examples/blob/master/web-examples/src/main/java/io/vertx/example/web/auth/Server.java

I'm a little confused why you're manually calling authenticate and adding the user to the session - this should be done automatically for you.

Sumit Sinha

unread,
Aug 14, 2015, 7:22:28 AM8/14/15
to vert.x
Hi Tim,

If I don't use MyFormLoginHandler I get following error. Also, I am trying to authenticate from MongoDB. So I crated MyAuthProvider class. I am new to vertx. Let me know if I am missing something.
java.lang.IllegalStateException: Form body not parsed - do you forget to include a BodyHandler?
at io.vertx.ext.web.handler.impl.FormLoginHandlerImpl.handle(FormLoginHandlerImpl.java:86)
at io.vertx.ext.web.handler.impl.FormLoginHandlerImpl.handle(FormLoginHandlerImpl.java:35)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:218)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:67)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:96)
at io.vertx.ext.web.handler.impl.UserSessionHandlerImpl.handle(UserSessionHandlerImpl.java:63)
at io.vertx.ext.web.handler.impl.UserSessionHandlerImpl.handle(UserSessionHandlerImpl.java:29)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:218)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:67)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:96)
at io.vertx.ext.web.handler.impl.SessionHandlerImpl.handle(SessionHandlerImpl.java:100)
at io.vertx.ext.web.handler.impl.SessionHandlerImpl.handle(SessionHandlerImpl.java:30)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:218)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:67)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:96)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.doEnd(BodyHandlerImpl.java:136)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.end(BodyHandlerImpl.java:122)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl.lambda$handle$105(BodyHandlerImpl.java:51)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$$Lambda$22/1158771039.handle(Unknown Source)
at io.vertx.core.http.impl.HttpServerRequestImpl.handleEnd(HttpServerRequestImpl.java:394)
at io.vertx.core.http.impl.ServerConnection.handleEnd(ServerConnection.java:288)
at io.vertx.core.http.impl.ServerConnection.processMessage(ServerConnection.java:403)
at io.vertx.core.http.impl.ServerConnection.handleMessage(ServerConnection.java:137)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:516)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:425)
at io.vertx.core.http.impl.VertxHttpHandler.lambda$channelRead$17(VertxHttpHandler.java:80)
at io.vertx.core.http.impl.VertxHttpHandler$$Lambda$16/705234202.run(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:312)
at io.vertx.core.impl.ContextImpl$$Lambda$5/1393931310.run(Unknown Source)
at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:217)
at io.vertx.core.http.impl.VertxHttpHandler.channelRead(VertxHttpHandler.java:80)
at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:124)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:244)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Thread.java:745)

My Vertx Start is here. UserPersister is for storing user to Database. MyAuthProvider is for authenticating user.

public void start(Future<Void> startFuture) throws Exception {
Router router = Router.router(vertx);
// Serve the static pages
HttpServer server = vertx.createHttpServer();
router.route().handler(CorsHandler.create("*")
     .allowedMethod(HttpMethod.GET)
     .allowedMethod(HttpMethod.POST)
     .allowedMethod(HttpMethod.OPTIONS)
     .allowedHeader("X-PINGARUNER")
     .allowedHeader("Content-Type"));

router.route().handler(CookieHandler.create());
router.route().handler(BodyHandler.create());
router.route().handler(
SessionHandler.create(LocalSessionStore.create(vertx)));
    AuthProvider ap = new MyAuthProvier();
router.route().handler(UserSessionHandler.create(ap));
router.route("/Services/rest/user/auth").handler(FormLoginHandler.create(ap));
router.post("/Services/rest/user/register").handler(new UserPersister());
router.route().handler(StaticHandler.create("webroot"));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
System.out.println("Server is started");
}

Tim Fox

unread,
Aug 14, 2015, 7:26:48 AM8/14/15
to ve...@googlegroups.com
We're almost there.

If you can you provide a complete reproducer (by that I mean a runnable class), like in the example I referenced below, then I can take a look.

Gist is fine for this.

Also please provide instructions for running. I.e. what you want me to do with the browser and what urls to hit.

bytor99999

unread,
Aug 14, 2015, 2:59:01 PM8/14/15
to vert.x
I have a stupid question. Are you sure both "servers" are on the same Hazelcast cluster. So that putting something in in one server is also there in the other.

Mark

shau...@kastar.co

unread,
Jul 12, 2016, 6:12:22 AM7/12/16
to vert.x
Hi Tim,

Please help me how can I use authprovider at web authentication level where i have to interact with my DB for authentication.
I can't use Shiro Auth so what is the best option or approach I should go through...

Thanks in advance!!!

Paulo Lopes

unread,
Jul 12, 2016, 6:28:31 AM7/12/16
to vert.x
Hi,

You should have a look at JDBC auth provider: http://vertx.io/docs/vertx-auth-jdbc/java/

shau...@kastar.co

unread,
Jul 12, 2016, 6:53:20 AM7/12/16
to vert.x
Ya but I am using mongodb & vertx-mongo client mongodb communication so I am not able to make out how should I use the AuthProviders and do the authentication and set the session for user.

Thanks in advance!!!!

Paulo Lopes

unread,
Jul 12, 2016, 7:02:36 AM7/12/16
to vert.x
If you're using mongo then you should look into:

http://vertx.io/docs/vertx-auth-mongo/java/

The manual explains how it works and there are some code snippets so you can get around.

Cheers,
Paulo
Reply all
Reply to author
Forward
0 new messages