0

I'm facing the session issue on the load balancing URL, the session is not getting created but it works fine when I use the Tomcat URL directly.

Controller:

@RequestMapping(value = "/authenticateUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Users> login(@RequestBody LoginParams params, UriComponentsBuilder ucBuilder)  {
    Users user = null;
    try {
        /* Check if user is already logged in*/
        boolean isUserAlreadyLoggedin = false;
        HttpSession httpSession = SessionHandler.getSession();
        if (httpSession.getAttribute("session_user") != null) {
            isUserAlreadyLoggedin = true;
            user = (Users) httpSession.getAttribute("session_user");
        }
        if (!isUserAlreadyLoggedin) {
            // after login related code...
            httpSession.setAttribute("session_user", user);
        }
        return new ResponseEntity<Users>(user, HttpStatus.OK);
    } catch (Exception e) {
        logger.error("Error while authenticating:" + e.getMessage());
    }
 }
}  

My Session Handler helper class:

class SessionHandler{
     public static HttpSession getSession() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();

        HttpSession session = attr.getRequest().getSession(false);
        if (session == null) {
            // Not created yet. Now do so yourself.
            session = attr.getRequest().getSession(true);
        } 
        return session;
    }


 public static boolean isSessionAlive()
 {
      boolean isSessionAlive = false;
        try {

            HttpSession httpSession = SessionHandler.getSession();
            if (httpSession.getAttribute("session_user") != null) {
                isSessionAlive = true;
            }
        }catch(Exception e){
            logger.error("Error in Session handler",e);
        }
        return isSessionAlive;
  }
 }

in this way i'm creating the session upon the user login and for every request i'm checking the whether the session is created through the following service.

    @RequestMapping(value = "/checkWhetherSessionIsAlive", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> checkWhetherSessionIsAlive(UriComponentsBuilder ucBuilder) {

    boolean isSessionAlive = false;
    try {

        HttpSession httpSession = SessionHandler.getSession();
        if (httpSession.getAttribute("session_user") == null) {
            isSessionAlive = false;
        } else {
            isSessionAlive = true;
        }
    } catch (Exception e) {

    }

    return new ResponseEntity<Boolean>(isSessionAlive, HttpStatus.OK);
}

Once the user logs in the session is getting creating and he is redirected to Home page and immediately the session is getting expired and he is redirected to the login page.
So to summarize the session is not getting created on this URL : www.application.com/app (f5 load balancing --> APache Web server --> tomcat )

it s working fine on URL: tomcat:8080/AOS/

  • If it works on tomcat we can pretty much rule out tomcat config and app code. Please edit to include relevant Apache config and F5 config. – kubanczyk Jan 13 '17 at 18:21
  • Do you have more than one tomcat load balanced ? Are sessions manager in files locally at each tomcat? If answer to both question is yes you need to look up for sticky sessions – kalyan Jan 14 '17 at 17:07

0 Answers0