2011-05-25 52 views
6

我想知道,在Spring Security中,我是否可以驗證用戶會話,只允許打開一個瀏覽器選項卡。可能嗎?我可以使用Spring Security管理多個瀏覽器選項卡嗎?

我也想知道,如果我可以做到這一點,當用戶關閉選項卡,並在會議結束前再次打開它SessionFilter從直接應用程序,而無需登錄屏幕。

我使用JSF 1.2,RichFaces的3.3.3,Hibernate和合作......

詳細信息:我知道春天安全,我只是在研究它。

現在,謝謝我的壞英語。

看到你!

回答

4

不,Spring Security無法判斷請求是來自原始選項卡還是來自新選項卡 - 該信息嚴格來說是客戶端。來自http://static.springsource.org/spring-security/site/faq.html

2.1。

我正在使用Spring Security的併發 會話控制,以防止用戶從一次多次登錄到 。 當我在登錄後打開另一個瀏覽器窗口 時,它不會阻止我再次登錄 。爲什麼我可以多次登錄 ?

瀏覽器通常爲每個瀏覽器實例維護一個單一的 會話。您 不能在 一次有兩個單獨的會話。因此,如果您再次登錄 另一個窗口或選項卡,則只需在同一會話中重新驗證 。 服務器不知道任何關於 選項卡,窗口或瀏覽器實例。 它看到的只是HTTP請求,它根據 JSESSIONID cookie的值將它們綁定到特定會話 。 當用戶在 會話期間進行認證時,Spring Security的併發 會話控制會檢查其 具有的其他已認證會話的數目。如果他們已經用 與同一會話進行驗證,則 然後重新驗證將不會有 的影響。

+0

嗯,那麼,我可以用同一個JSESSIONID打開兩個選項卡? 這很糟糕。我想阻止2個選項卡。 2瀏覽器不是問題。 有人知道另一種方式/框架/要做的事情? 歡呼聲 – caarlos0 2011-05-25 17:43:18

2

我最近使用Spring Security實現了多個選項卡/窗口的解決方案。爲了成功登錄,我使用`LoginSucessHandler``並在會話中設置一個唯一的窗口名稱。在主模板頁面上,我設置了一個窗口名稱,並在每個頁面上加載驗證窗口名稱與會話的窗口名稱,如果不相同,則重定向到錯誤頁面。

下面是配置和代碼:

@Service 
public class LoginSucessHandler extends 
     SavedRequestAwareAuthenticationSuccessHandler { 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, 
      HttpServletResponse response, Authentication authentication) 
      throws ServletException, IOException { 
     User user = (User) authentication.getPrincipal(); 
     String windowName = user.getUsername() + new Date().getTime(); 
     HttpSession session = request.getSession(); 
     session.setAttribute("windowName", windowName); 
     session.setAttribute("windowNameToSet", windowName); 
     super.onAuthenticationSuccess(request, response, authentication); 
    } 

} 

主要模板或頭頁:

<script> 
    <%if (session.getAttribute("windowNameToSet") != null) { 
     out.write("window.name = '" 
      + session.getAttribute("windowNameToSet") + "';"); 
     session.removeAttribute("windowNameToSet"); 
    }%> 
    if (window.name != "<%=session.getAttribute("windowName")%>") { 
     window.location = "<%=request.getContextPath()%>/forms/multiwindowerror.jsp"; 
    } 
</script> 

出於安全方面:

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/login.hst**" access="anonymous or authenticated" /> 
    <intercept-url pattern="/**/*.hst" access="authenticated" /> 
    <form-login login-page="/login.hst" 
     authentication-failure-url="/login.hst?error=true" 
     authentication-success-handler-ref="loginSucessHandler" /> 
    <logout invalidate-session="true" logout-success-url="/home.hst" 
     logout-url="/logout.hst" /> 
    <remember-me key="jbcp" authentication-success-handler-ref="loginSucessHandler"/> 
</http> 

只需確保上面的login.jsp不包括JavaScript。

0

我想出了一個更簡單的方法來完成同樣的事情。如果您已經擴展了SimpleUrlAuthenticationSuccessHandler,與@Jagar類似,請創建一個登錄用戶的數組列表,將用戶添加到該用戶,並將其添加到會話中。然後,每次登錄時,檢查會話是否存在以及該用戶是否處於會話數組列表屬性中。如果是,則失敗,如果不是,則允許登錄。

這樣你可以讓多個用戶使用同一個瀏覽器登錄,但不是同一個用戶。這也防止了錯誤地覆蓋windowName屬性的可能性。

相關問題