2016-06-12 13 views
0

フィルタ/サーブレットでは、セッションがrequest.getSession(false)で取得されるたびに、それはどういう意味ですか?セッションnull、それはどういう意味ですか?

セッションがnullの場合、これらの2つのケースのいずれかを表すことができます。

  1. 要求に関連付けられたJSESSION IDクッキーはありません。要求は新鮮な要求です。
  2. JSESSION IDに関連付けられたセッションの有効期限が切れていますか?

私は自分のアプリケーションでSpring-Securityを使用しています。私は、すべての要求を傍受し、要求に関連するセッションと認証オブジェクトがあるかどうかを調べるフィルタを作成しました。要求が新鮮であると仮定しない場合、新しいセッションを作成して空の認証オブジェクトを作成しますNULLプリンシパルとブランクの権限リストで、authenticatedをtrueに設定します。

HttpSession session = httpRequest.getSession(false); 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 

    if(session == null && auth == null) { 
     LOGGER.debug("In AuthenticationFilter | In doFilter | Session and Authentication are both null."); 
     session = httpRequest.getSession(true); 

     auth = CookieAuthentication.createBlankAuthentication(); 
     auth.setAuthenticated(true); 
     SecurityContextHolder.getContext().setAuthentication(auth); 

     chain.doFilter(req, res); 
    } 
    else if(session == null && auth != null) { 
     LOGGER.debug("In AuthenticationFilter | In doFilter | Session is null but authentication is not."); 
     LOGGER.info("In AuthenticationFilter | Returning Response."); 

     Response response = new Response(); 

     response = new Response(); 
     response.setMessage("Session Has Expired."); 
     response.setFlag("SE"); 

     httpResponse.setStatus(401); 

     try { 
      httpResponse.getWriter().write(response.toJSON()); 
      httpResponse.getWriter().flush(); 
     } 
     catch (IOException e) {    
      LOGGER.error(e.getMessage()); 
     } 
    } else if (session != null && auth != null) { 
     LOGGER.debug("In AuthenticatorFilter | In doFilter | Session and Authentication are not null. ");   
     chain.doFilter(req, res); 
    } else { 

     /** 
     * Some Fatal error. 
     * We shouldn't be here. 
     */ 

     Response response = new Response(); 

     response = new Response(); 
     response.setMessage("Un Authenticated"); 
     response.setFlag("UA"); 

     httpResponse.setStatus(401); 

     try { 
      httpResponse.getWriter().write(response.toJSON()); 
      httpResponse.getWriter().flush(); 
     } 
     catch (IOException e) {    
      LOGGER.error(e.getMessage()); 
     } 

    } 

セッションヌルと認証オブジェクトnullを受け取ることができ、nullでない場合は何ですか?

私が想定したことは次のとおりです。

Session : null, authenticatin : null -> Fresh Request. 
Session : null, authentication : not-null -> Expired Session. 
Session : not-null, authentication : null -> Shouldn't happen normally. 
Session : not-null, authentication : not-null -> Previously authenticated request. 

私の考えの誤解について教えてください。 これ以外にも、セッションが存在しないことと、セッションをnullとして受け取ったときに期限切れになったことを区別する方法を知りたいと思います。

答えて

0

getSession(false)はセッションを取得することを意味しますが、セッションがない場合は作成しないでください。

関連する問題