2013-09-25 40 views
2

我使用Spring Security來保護對我的GWT首頁應用程序的訪問「/HumanResources.html」。它檢查用戶的憑證是否正確(通過將它們與ldap內容進行比較)並查看用戶是否存在於數據庫(自定義授權表)中。註銷後的GWT + Spring Security登錄問題

首次用戶登錄,沒有問題,然後顯示用戶註銷和「.jsp」頁面。 但是當他想再次訪問「HumanResources.html」頁面時,認證顯然被忽略(登錄表單不顯示),並顯示頁面。只有界面可見,數據由安全的RPC服務檢索。

此問題出現在外部Tomcat服務器上(在Firefox和Chrome上測試過),但不在GWT Dev模式下。 CTRL + F5似乎工作,我尋找其他緩存問題,但它沒有幫助。

任何人都可以幫助我嗎?

安全的applicationContext.xml的一部分:

<http use-expressions="true" auto-config="false"> 
    <intercept-url pattern="/HumanResources.html" access="isAuthenticated()" /> 
    <form-login 
     login-page='/login.jsp' 
     authentication-failure-url = "/login.jsp?login_error=1" 
     authentication-success-handler-ref="HRAuthenticationHandler" /> 
    <logout 
     logout-url="/logout" 
     logout-success-url="/logout.jsp" 
     delete-cookies="JSESSIONID"/> 
</http> 

<beans:bean id="HRAuthenticationHandler" class="lu.sfeir.candidate.server.auth.HRAuthenticationHandler"> 
    <beans:property name="useReferer" value="true" /> 
</beans:bean> 

<ldap-server url="${ldap.serverUrl}" manager-dn="${ldap.adminLogin}" manager-password="${ldap.adminPassword}" /> 

<authentication-manager> 
    <ldap-authentication-provider 
     group-search-base="${ldap.groups}" 
     user-search-base="${ldap.users}" 
     user-search-filter="${ldap.userId}"> 
    </ldap-authentication-provider> 
</authentication-manager> 

和我的自定義AuthenticationHandler實現

public class HRAuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler { 

    @Autowired 
    private AuthorizedUsersDao usersDao; 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, 
      ServletException { 

     // Check if the user exist in the DB 
     if(usersDao.findUser(((UserDetails)authentication.getPrincipal()).getUsername())) { 
      // Redirect to home page 
      super.onAuthenticationSuccess(request, response, authentication); 
     } else { 
      // Redirect to error page 
      response.sendRedirect("/spring_security_login?login_error"); 
     } 
    } 
} 

回答

0

編輯: 對不起missleading你。 我挖了一下,發現安全不起作用導致用戶瀏覽器正在緩存xxx.html頁面,js,css等 您可以: 1)通過設置html headgers強制刷新頁面: 這就是最簡單的解決方案,因爲您是使用安全的RPC服務,應該爲你服務。 或者完全禁用緩存。

注意它會增加您的網絡流量。如果RPC ocured安全錯誤

2) 我們的應用程序是檢查:

import com.google.gwt.core.client.GWT; 
import com.google.gwt.user.client.Window; 
import com.google.gwt.user.client.rpc.AsyncCallback; 
import com.google.gwt.user.client.rpc.InvocationException; 

public abstract class CustomAsyncCallback<T> implements AsyncCallback<T> { 
    @Override 
    public void onFailure(Throwable caught) { 
     if (caught instanceof InvocationException) { 
      InvocationException ie = (InvocationException) caught; 
      if(ie.getMessage().contains("j_spring_security_check")) { 
       Window.alert("User session expired, please login again"); 
       Window.open(GWT.getHostPageBaseURL() + "login.jsp", "_self", null); 
       return; 
      } 
     } 
    } 
} 

你必須調用它的地方,也就是在顯示你當前登錄的用戶:

userSecurityService.getUser(new CustomAsyncCallback<String>() { 
     @Override 
     public void onSuccess(String result) { 
      usrLoginLbl.setText(result); 
     } 
    }); 

,或者你可能趕上這個你的其中一個onFailure方法的例外

+0

感謝您的回覆,我嘗試了通過向HumanResources.html添加訪問角色(ROLE_HR)來解決您的問題,但它無效:/當我重新登錄時,驗證階段是「滑雪」... – ersefuril

+0

再次感謝! Html標頭可能是最好的解決方案(少數用戶連接到應用程序)。我一直都在研究這個解決方案,但是上次嘗試的時候它沒有奏效(也許我做錯了)。你能告訴我如何處理它嗎? – ersefuril

+0

我們這樣做2)方式 - 代碼在我的文章。在onFailure的RPC中,我們處理安全異常 –