0

I have a GAE CloudEndpoints project and I am having trouble using Java Sessions to maintain session state. Here is what I have done so far:

First I enabled Sessions in the appengine-web.xml file.

I then created a "login object" to hold the useremail and password. I also made it Serializable (as I read this was a requirement?)

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Session implements Serializable{

private static final long serialVersionUID = 4013168535563327001L;
@Persistent
String email;
@Persistent
String password;

 public String getemail() {
         return email;
 }

 public void setemail(String email) {
     this.email = email;
 }

 public String getpassword() {
     return password;
 }

 public void setpassword(String password) {
     this.password = password;
 }
}

Then I created a controller to create the session and store the login object as an attribute inside of it:

@ApiMethod(name="setLoginTest")
public Object setloginTest(HttpServletRequest request, User myLogin){

    //Just setting some values into myLogin
    //for production, this will compare submitted values against a DB.
    myLogin.email = "Jason";
    myLogin.password = "123";

    //Creating the session object
    HttpSession mySession = request.getSession(true);

    //Trying to set an attribute that holds the login object 
    //(i.e. i want to store the username in the session) 
    mySession.setAttribute("loginObj", myLogin);

    return myLogin;
}

If I call this endpoint, the system returns the myLogin object no problem and I know the mylogin object has the username "jason" and the pw "123"

So, the final step if for me to create the endpoint that checks to see if the session has some user data or null. ideally, I would take some action if logged in, and if not logged in, return a "not logged in" message.

@ApiMethod(name="getSessionTest")
public Object getSessionTest(HttpServletRequest request, User myLogin){

    //getting the current session info
    HttpSession mySession = request.getSession(false);

    //Always returns NULL which is not right!!
    return mySession.getAttribute("loginObj");
}

however, the trouble is that the "Getsessiontest" endpoint always returns null. Please help!

Jason
  • 53
  • 1
  • 5
  • looks like a dupe of this: http://stackoverflow.com/questions/15522255/sessions-with-google-cloud-endpoints – m1k3t Mar 16 '15 at 20:03
  • Do you know why the code above is returning null? I have searched StackOverflow for a good example of Java Session in GAE and CloudEndpoints, however most just recommend using the Google Account system (which is not an option for this project) – Jason Mar 16 '15 at 21:20
  • As per the answer linked to by m1k3t, you can never set a client-side cookie for a session, so the session for the request is always going to be Null. Passing the User object is also no good in your case if you don't want to use Google Accounts. One alternative is to roll your own sessions by passing the Session ID manually to the API method as a named parameter. – Adam Mar 17 '15 at 02:30
  • could you confirm if sessions are being persisted on memcache/datastore? – jirungaray Mar 26 '15 at 15:13

1 Answers1

1

If you are running Cloud Endpoints, why do you even need sessions on the server side? You can store the "session" information on the client side. If you are consuming your endpoints from a Javascript client, you can use localstorage to act like a session cookie.

tborja
  • 359
  • 1
  • 3
  • 5