簡體   English   中英

應用程序上的舊的新“刪除/無效” Cookie

[英]Old new “delete/invalidade” Cookie on an application

大家好

我在這里已經閱讀了很多有關使請求中的cookie無效並將其添加到響應中的問題(我知道不能將其物理刪除)。

我已經讀過這篇文章《 如何刪除...餅干》 ,它與我的問題最接近。 而且,沒有任何答案和建議對我的特殊情況有幫助(我認為)。

我正在計算機上對其進行測試,因此我的域是我的工作域。 我有一個配置了tomcat服務器的日食,另一個有了配置JBoss服務器的日食。

因此,我的情況是,我已經建立了一個單一登錄系統,其中有一個域是單一登錄系統(例如: myMachine.myCompany.com:8081/login : myMachine.myCompany.com:8081/login login myMachine.myCompany.com:8081/login命名為login ),並且我有一個系統使用此登錄名,它將在另一個地址上,現在一切都在我的機器上,所以這個另一個系統( myMachine.myCompany.com:8080/system/something/index.jsf命名為app

我的實施工作流程是:

-來自jboss的用戶訪問應用
-轉到一個安全過濾器,該過濾器檢查對這個應用程序的請求中的所有內容(在web.xml中為/*
-安全過濾器檢查用戶是否具有cookie(jboss端)

Cookie[] cookies = request.getCookies();
if(cookies != null){ 
    for (Cookie cookie : cookies) {
        if (cookie.getName().equalsIgnoreCase("ssoSecurity")) {
            return cookie;
        }
    }
}

-如果用戶沒有cookie,則將其重定向到登錄名 (tomcat)-當用戶登錄(輸入要在數據庫中檢查的數據)時,我將為此用戶創建一個帶有令牌的會話,以供日后識別(當所需),tomcat服務器上的cookie:

Cookie cookie = new Cookie("ssoSecurity", token);
cookie.setDomain(domain); //.myCompany.com
cookie.setVersion(0);
cookie.setPath("/");
cookie.setSecure(request.isSecure());
cookie.setMaxAge(-1); //deleted when the browser close
response.addCookie( cookie );

-然后登錄將用戶重定向到應用程序 ,它將再次在securityFilter上-在securityFilter檢查cookie並找到它之后,它將獲取其值(令牌)並調用我的securityClient(這是我的類路徑上的JAR,用於與之通信) tomcat通過json返回休息)來檢查用戶是否已登錄(這是第一次在此服務器上創建用戶會話)-然后它可以正常運行應用程序

我的問題是注銷功能無法正常工作。 我會解釋。

應用程序上 ,用戶單擊注銷鏈接,該鏈接將調用( #{appUserBean.logout} )我的注銷方法,該方法是:

HttpServletResponse response = (HttpServletResponse)FacesContext
              .getCurrentInstance().getExternalContext()
              .getResponse();
HttpServletRequest request = (HttpServletRequest)FacesContext
              .getCurrentInstance().getExternalContext()
              .getRequest();
destroyCookie(getCookie(request), response);
SSOClient client = new SSOClient(); //from the jar that I mentioned
client.logout(this.getUserSession().getToken(), request, response);

destroyCookie方法:

private void destroyCookie(Cookie cookie, HttpServletResponse response) {
    if (cookie != null) {
        response.setContentType("text/html");
        cookie.setPath("/");
        cookie.setValue("notValidSession");
        cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
        cookie.setVersion(0);
        cookie.setDomain(""); //I've tried ".myCompany.com" wont work
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

這行代碼: client.logout(this.getUserSession().getToken(), request, response); 將令牌發送到登錄應用程序以從登錄服務器(tomcat)注銷用戶,然后將用戶重定向到登錄應用程序(tomcat)。

這種情況是cookie尚未失效,其值也沒有改變。 因此,如果我不關閉瀏覽器並放置應用程序的鏈接,則cookie仍在其中,而用戶令牌不是我設置的新值(“ notValidSession”),並且在tomcat服務器上,如果我檢查request.getCookies(); 就像沒有cookie一樣,它為null,但是我可以在firefox或chrome資源上看到具有相同值(用戶令牌)的cookie。

我只是在寫這篇文章的時候意識到,我並沒有從jboss方面使用戶會話無效(我會這樣做,而你們會建議一些您可能會發現的錯誤)。

提前致謝,

事實證明問題出在這行cookie.setDomain("");上的方法destroyCookiecookie.setDomain(""); 如我的評論所述,我嘗試設置Cookie域,但是由於進行了大量更改,因此setDomain(".myCompany.com")的使用不起作用。

現在,在我修改了client.logout以清除jboss一側的用戶的會話信息並進行了修改后,在destroyCookie方法上設置了域后,它工作正常。 所以最后的修改是這樣的:

private void destroyCookie(Cookie cookie, HttpServletResponse response) {
    if (cookie != null) {
        response.setContentType("text/html");
        cookie.setPath("/");
        cookie.setValue("notValidSession");
        cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
        cookie.setVersion(0);
        //I've configured the domain on the custom-params-service.xml on JBoss
        cookie.setDomain( AppUtils.getJndiConfig("SSOCookieDomain") ); 
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

以及在JBoss端(在我的客戶端jar上)清除會話信息的方法:

private void cleanUserSession(HttpServletRequest _request) {
    HttpSession session = (HttpSession) _request.getSession(false);
    if ( session != null ){
        session.setAttribute(ClientUtil.AUTH_TOKEN, "" );
        session.setAttribute(ClientUtil.USUER_INFO, null);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM