12

I just enabled Session in my Google AppEngine/Java + GWT application. And how do I use it? How do I get session ID and play will all good stuff from it? Are there any real examples of simple login page where I'm just entering LoginName and Password, then it goes to the server over RPC call, authenticates against database and sends Session ID back to the client.

I have following code already but don't know what to do next:

GWT Login Form:

public class LoginForm {
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);

    VerticalPanel loginVp = new VerticalPanel();
    TextBox loginTxt = new TextBox();
    TextBox passTxt = new TextBox();

    Button loginBtn = new Button("Login");

    public Widget getLoginWidget(){

        loginBtn.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent arg0) {

                loginService.authenticateUser(loginTxt.getText(), passTxt.getText(), 
                        new AsyncCallback<String>(){

                            public void onFailure(Throwable caught) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "No Connetion", "Problem conneting to the server.");
                            }

                            public void onSuccess(String result) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "Session ID", "Your session id is: " + result);

                                GWT.log("Setting up session", null);
                                String sessionID = result;
                                final long DURATION = 1000 * 60 * 60 * 24 * 14; //duration remembering login. 2 weeks
                                Date expires = new Date(System.currentTimeMillis() + DURATION);
                                Cookies.setCookie("sid", sessionID, expires, null, "/", false);
                            }
                        }
                );  
            }   
        });

        loginVp.add(loginTxt);
        loginVp.add(passTxt);
        loginVp.add(loginBtn);

        return loginVp;
    }
}

RPC Servlet:

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{ 
    //Sends back to the client session id
    public String authenticateUser(String login, String password){
        String sessionId = new String();

        // TODO: figure out how to work with session id in GAE/J
        sessionId = "How to get session id?";

        return sessionId;
    }

    public Boolean checkIfSessionIsValid(String sessionId){

        //TODO: figure out how to check user's credentials  
        return true;
    }
}

Any hints in the right direction would be helpful. Thanks.

David Underhill
  • 15,896
  • 7
  • 53
  • 61
Maksim
  • 16,635
  • 27
  • 94
  • 135
  • 1
    Be careful if you are just using a cookie based sessionId for authentication ,as it can leave you open to cross site scripting attacks:http://groups.google.com/group/Google-Web-Toolkit/web/security-for-gwt-applications – Peter Recore Jul 17 '09 at 18:40

2 Answers2

18

Enabling session support gives you a standard Servlet HttpSession.

This will be tracked by means of a cookie (called JSESSONID), which is managed by the servlet container under the covers. You do not need to care about the session id.

You can then set attributes (server-side) that will be associated with the session (so that you can retrieve them later).

HttpServletRequest request = this.getThreadLocalRequest();

HttpSession session = request.getSession();

// in your authentication method
if(isCorrectPassword)
   session.setAttribute("authenticatedUserName", "name");

// later
 if (session.getAttribute("authenticatedUserName") != null)

This should also work with Ajax requests from GWT. Please refer to any Servlet tutorial for more details.

The drawback of sessions on GAE (compared to other servlet engines) is that they are serialized in and loaded from the database every time, which could be expensive, especially if you put a lot of data in there.

Alex
  • 143
  • 1
  • 7
Thilo
  • 257,207
  • 101
  • 511
  • 656
16

Here is how you can get the session in GAE:

this.getThreadLocalRequest().getSession();
KevMo
  • 5,590
  • 13
  • 57
  • 70
  • 2
    But it's still ok to use request.getSession() in a servlet context as well though right? – HaveAGuess Dec 19 '11 at 19:58
  • 1
    The getThreadLocalRequest() is the GWT way of getting access to the request that is passed into service and into doGet, doPut, etc... Since I don't generally use GWT, I rely on request.getSession(). Works fine. – Chuck Mar 09 '13 at 16:42