How to implement a own "handleLogin()" for user SQL Auth?

214 views
Skip to first unread message

Domenico Colucci

unread,
Jul 24, 2014, 6:02:34 AM7/24/14
to jets...@googlegroups.com
We are new to your Sourcode and a small group of ppl who are working on some fun projects to learn and enhance our java programming skills.

Since we have played around a bit with the provided examples we asked our selfs how we could implement a "user login auth" backed with mysql in the right way.

First idea was to hack the handleLogin or channelRead0 method from the nadron/handlers/netty/LoginHandler class directly. Works fine, but I have the feeling that there must be a more elegant solution to that.

Maybe implementing and overriding?


Domenico Colucci

unread,
Jul 24, 2014, 6:33:06 AM7/24/14
to jets...@googlegroups.com
Well, I thought to try something like this:

public class myLoginHandler extends LoginHandler {

    @Override
    public void handleLogin(Player player, ChannelHandlerContext ctx, ByteBuf buffer) {
        Credentials credentials = new SimpleCredentials(buffer);
        boolean auth;
        try {
            result = query("SELECT username, password FROM users Where username = '" + credentials.getUsername() + "' AND password = '" + credentials.getPassword() + "'");
            auth = (result.size() == 1) ? true : false;
        } catch (exception e) {
            // error handle or log
        }

        if (null != player && auth == true) {
            ctx.channel().write(NettyUtils.createBufferForOpcode(Events.LOG_IN_SUCCESS));
            handleGameRoomJoin(player, ctx, buffer);
        } else {
            // Write future and close channel
            closeChannelWithLoginFailure(ctx.channel());
        }
    }

Domenico Colucci

unread,
Jul 24, 2014, 8:28:35 AM7/24/14
to jets...@googlegroups.com
Ok got it.

just made my own LoginHandler class and overridden the channelRead0 method.

Thanks to Spring I have just redeclared the loginHandler bean in netty-handlers.xml.

now it works.
How cool is that!?. Thanks anyway. Learnt some new things now.

abraham menacherry

unread,
Jul 24, 2014, 10:59:59 AM7/24/14
to jets...@googlegroups.com
Hi Domenico,

Good to hear that you use nadron and that you got it to work. Yes spring, when used for what it is good for is a beautiful piece of software.

The way you have done it is good enough. Another option would be to actually re-implement the LookupService with your own implementation instead of using existing SimpleLookupService to talk to mysql. You can use the method playerLookup for validating the login attempt. The new mysql based implementation should be wired into LoginHander rather than re-do the LoginHandler itself.

Thanks,
-Abraham


--
You received this message because you are subscribed to the Google Groups "jetserver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jetserver+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Domenico Colucci

unread,
Jul 25, 2014, 3:18:01 AM7/25/14
to jets...@googlegroups.com
Tanks,

I'll give the other option a try, too and decide wich one is more confortable afterwards.

Again, thank you for your answer and I hope it is ok to bug You if I got any new questions ;)



Domenico Colucci

unread,
Jul 29, 2014, 10:34:21 AM7/29/14
to jets...@googlegroups.com
The LookupService approach is neat and works perfect.

just posting to share knowledge as follow:

At first create a new Lookup implementation:

File: MyLookupService
   
public class MyLookupService extends SimpleLookupService {
   ...
   
@Override

    public Player playerLookup(Credentials c) {
        boolean auth = false;
        DefaultPlayer player = new DefaultPlayer();

        // TODO: Database User auth check.
        System.out.println("SELECT * FROM users WHERE Name = '" + c.getUsername() + "' AND Password = '" + c.getPassword() + "'");

        if (auth == false) {
            System.out.println("LOG_IN_FAILED: Invalid credentials provided by user: " + c.getUsername());
            return null;
        }

        return player;
    }
...


Tell Nadron (Server) to use it:

File: SpringConfig
    public @Bean(name = "lookupService")
    LookupService lookupService() {
        Map<String, GameRoom> refKeyGameRoomMap = new HashMap<>();
        refKeyGameRoomMap.put("C3GameRoom", C3GameRoom());
        refKeyGameRoomMap.put("C3GameRoomForNettyClient", c3GameRoomForNettyClient());
        LookupService service = new MyLookupService(refKeyGameRoomMap);
        return service;
    }

Well, Easy when you know how.

Abraham Menacherry

unread,
Jul 29, 2014, 1:19:33 PM7/29/14
to jets...@googlegroups.com
Thanks for posting back the solution you used. It will be helpful to other users.

Christian Bartel

unread,
May 8, 2015, 5:33:48 PM5/8/15
to jets...@googlegroups.com
Hi Abraham!

I am Meldric and I am one of the people Domenico mentioned before. We have been trying to setup Nadron for our project and all in all it looks very good. However, we are stuck on one particular problem. When we do provide wrong credentials, there is no event arriving at the client. And we would love to tell the user that there was something wrong. The Player is null if authentication fails and then this code should run:
CloseChannelWithLoginFailure.

It seems to get there, but the client never sees the event coming in.
Could you help us out on this one, maybe?

Thank you very much!
Meldric

abraham menacherry

unread,
May 10, 2015, 1:01:09 PM5/10/15
to jets...@googlegroups.com
Hi Meldric,

In the LoginHandler class, following code sends back a LOG_IN_FAILURE event back in case of login failure. 

/**
* Helper method which will close the channel after writing
* {@link Events#LOG_IN_FAILURE} to remote connection.
*
* @param channel
* The tcp connection to remote machine that will be closed.
*/
private void closeChannelWithLoginFailure(Channel channel)
{
ChannelFuture future = channel.writeAndFlush(NettyUtils
.createBufferForOpcode(Events.LOG_IN_FAILURE));
future.addListener(ChannelFutureListener.CLOSE);
}

Did i misunderstand the issue and this problem is at client side and not at server side? Let me know. I can try to run a test from client to see what happens in case of login failure due to bad credentials.

Also, what client are you using? web socket(browser) or java or flash?

Thanks,
Abraham.

--

Christian Bartel

unread,
Jun 29, 2015, 4:53:54 PM6/29/15
to jets...@googlegroups.com
Hi!

We are using the java client and server. The method you have mentioned is actually called (we can step in), but the method on the client is never called from there. So the client does not "see" the login fail.
Event LOG_IN_FAILURE is catched and processed on the client, but the respective method on client side does not fire.

On the protocol (SpringConfig.java):
@Autowired
@Qualifier("webSocketProtocol")
private Protocol webSocketProtocol;

...

public @Bean(name = "C3GameRoom") GameRoom C3GameRoom() {
   
GameRoomSessionBuilder sessionBuilder = new GameRoomSessionBuilder();
    sessionBuilder
.parentGame(C3Game()).gameRoomName("C3GameRoom").protocol(webSocketProtocol);
    C3Room room
= new C3Room(sessionBuilder);
   
return room;
}

This is the code on client side (clientTest.java):

    private static void addDefaultHandlerToSession(Session session) {
       
// we are only interested in data in, so override only that method.
       
AbstractSessionEventHandler handler = new AbstractSessionEventHandler(session) {

           
@Override
           
public void onLoginFailure(Event event) {
               
super.onLoginFailure(event); //To change body of generated methods, choose Tools | Templates.
               
System.out.println("onLoginFailure");
           
}        

           
@Override
           
public void onLoginSuccess(Event event) {
               
super.onLoginSuccess(event); //To change body of generated methods, choose Tools | Templates.
               
System.out.println("onLoginSuccess");
           
}

Login success does work (client prints "onLoginSuccess"), but it does nothing for onLoginFailure.
Thanks in advance! And sorry for the long delay.

Meldric

Christian Bartel

unread,
Jun 29, 2015, 4:56:47 PM6/29/15
to jets...@googlegroups.com
Hi!

Me again... :)

The first paragraph reads:

We are using the java client and server. The method you have mentioned is actually called (we can step in), but the method on the client is never called from there. So the client does not "see" the login fail.
Event LOG_IN_FAILURE is catched and processed on the client, but the respective method on client side does not fire.

Make this:

We are using the java client and server. The method you have mentioned is actually called (we can step in), but the method on the client is never called from there. So the client does not "see" the login fail.
Event LOG_IN_FAILURE is catched and processed on the server, but the respective method on client side does not fire.

Sorry and thanks!

Meldric
Reply all
Reply to author
Forward
0 new messages