2017-03-02 92 views
1

我有基本的登錄 - 註銷會話管理問題。 當我點擊登錄按鈕,下面的這個功能被觸發。在LDAP認證後,它移動到index.html我顯示他們的名字。會話即使失效後仍存在

function validate() 
      { 
       var pageTitle=$(document).attr('title'); 
       var un=document.getElementById('username').value; 
       var pwd=document.getElementById('pass').value; 
       $.ajax({ 
        'url':'/analytics_1/validate', 
        'data':'userName='+un+'&password='+pwd, 
        'type':'GET', 
        'success':function(response) 
        { 
         if(response==1) 
         { 
          $.ajax({ 
           'url':'/analytics_1/LogButton', 
           'type':'POST', 
           'data':'userName='+un+'&buttonId=VIKALPLoginButton&pageTitle='+pageTitle, 
           'success':function() 
           { 
            window.open("index.html","_self"); 
           } 
           }); 

         } 
         else 
         { 
          alert("Invalid Credentials"); 
         } 
        } 
       }); 
      } 

我檢查後創建LogButton.java會話,如果它是新

if(session.isNew()) 
      { 
       System.out.println("session is not set, lets create the name"); 
       associate=req.getParameter("userName"); 
       session.setAttribute("Associate",associate); 
      } 
      else 
      { 
       System.out.println("session is already set, lets get the name"); 
       associate=(String)session.getAttribute("Associate"); 
      } 

我從我成功登錄

後創建的會話中獲取他們的名字和我做一些動作和註銷,

$('#logout').on('click',function() 
     { 
      var pageTitle=$(document).attr('title'); 
      $.ajax({ 
        'url':'/analytics_1/LogButton', 
        'data':'buttonId=VIKALPLogoutButton&pageTitle='+pageTitle, 
        'type':'POST', 
        'success':function() 
        { 
         window.open('Login.html',"_self"); 
        }, 
        'error':function(err) 
        { 
         alert("haha:"+err.response); 
        } 
      }); 
     }); 

在LogButton.java中,我檢查按鈕是否是VIKALPLogoutButton,如果屬實,我繼續使會話無效並刪除屬性

if(button.equals("VIKALPLogoutButton")) 
      { 
       System.out.println("deleting the session cuz of logout"); 
       session.removeAttribute("Associate"); 
       session.invalidate(); 
       //System.out.println("what happens to the session? " +session.isNew()); 
      } 

所有這些都按照要求發生。現在出現安全用例:如果我在沒有登錄的情況下訪問index.html會發生什麼?

於是我開始檢查,如果會話設置與否時的index.html負荷,

$(document).ready(function() { 
$.ajax({ 
       'url':'/analytics_1/GetAssocId', 
       'type':'POST', 
       'success':function(response) 
       { 
        if(response!="null") 
         {} 
        else 
        { 
         window.open("Login.html","_self"); 
        } 
        $('#name').text(response); 
       } 
      }); 
..... 
..... 
} 

GetAssocId.java:

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException 
{ 
    HttpSession session=req.getSession(); 
    PrintWriter out=res.getWriter(); 
    out.print(session.getAttribute("Associate")); 
} 

這也能正常工作,即它重定向我登錄。 html如果會話未被創建。

現在的問題是,我無法登錄,甚至提供有效憑據後,莫名其妙的「關聯」屬性被設置爲null,

下面是的System.out.println輸出,我在cmd中 enter image description here得到

高於白線:登錄,註銷行動(請注意,我已經給會話無效輸出)

低於白線:直接去的index.html,它重定向到login.html的,和你登陸您的有效憑證,

現在這是我的問題,它使會話無效,但它仍然說會話已經存在。更令人困惑的是,會話是存在的,但值爲空。

我該如何解決這個問題?請幫助

PS:除了我提供了LogButton.java的片段是不顯著對於這個問題

回答

1
HttpSession session=req.getSession(); 

如果你看一下getSession方法的文檔

返回與此請求關聯的當前HttpSession,或者,如果沒有當前會話且create爲true,則返回新會話。 如果create爲false並且請求沒有有效的HttpSession,則此方法返回null。

您打電話給req.getSession()方法給你一個新的會議。也許讓現有會話,您需要使用

HttpSession session=req.getSession(false); 

正如你已經失效了會議,這給你會話null

您的其他問題

現在,這是我的問題,這使會話失效,但仍它說會議已經存在。更令人困惑的是,會話是存在的,但值爲空。

這是因爲你創建了一個新的會話,也有它沒有屬性,這是你得到null

+0

謝謝蘇雷什的原因吧!除了你的回答,我似乎在做index.html中的錯誤類型的變量檢查,而不是在'錯誤'中給出ajax的空值,我在'成功'中檢查它是否就像一個字符串! 乾杯 –

相關問題