
ロード バランシング URL でセッションの問題が発生しています。セッションは作成されませんが、Tomcat URL を直接使用すると正常に動作します。
コントローラ:
@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());
}
}
}
私のセッション ハンドラー ヘルパー クラス:
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;
}
}
この方法では、ユーザーのログイン時にセッションを作成し、リクエストごとに次のサービスを通じてセッションが作成されたかどうかを確認します。
@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);
}
ユーザーがログインすると、セッションが作成され、ホーム ページにリダイレクトされますが、すぐにセッションが期限切れになり、ログイン ページにリダイレクトされます。
要約すると、この URL ではセッションが作成されません: www.application.com/app (f5 ロード バランシング --> APache Web サーバー --> tomcat)
URL: tomcat:8080/AOS/ では正常に動作しています