2014-01-15 75 views
0

我是Spring Security和Spring MVC的新成員,使用jQuery。我的Spring Security是基於Spring Security參考文檔的基本設置。我使用的是Spring 3.2.4。使用SpringMVC和jQuery的Ajax安全性

<http use-expressions="true"> 

    <intercept-url pattern="/secure/login" access="permitAll" /> 
    <intercept-url pattern="/secure/logout" access="permitAll" /> 
    <intercept-url pattern="/secure/denied" access="permitAll" /> 
    <session-management session-fixation-protection="migrateSession" session-authentication-error-url="/login.jsp?authFailed=true"> 
     <concurrency-control max-sessions="10" error-if-maximum-exceeded="true" expired-url="/login.html" session-registry-alias="sessionRegistry"/> 
    </session-management> 

    <intercept-url pattern="/**" access="isAuthenticated()" /> 
    <!-- <intercept-url pattern="/**" access="denyAll" /> --> 
    <form-login login-page="/secure/login" default-target-url="/" authentication-failure-url="/secure/denied" /> 
    <logout logout-url="/secure/logout" logout-success-url="/" /> 
    <expression-handler ref="defaultWebSecurityExpressionHandler" /> 
</http> 

<authentication-manager> 
    <authentication-provider user-service-ref="com.ia.security.SpringSecurityDao" /> 
</authentication-manager> 

<beans:bean id="com.ia.security.SpringSecurityDao" class="com.ia.security.SpringSecurityDaoImpl"> 
    <beans:property name="usersByUsernameQuery"> 
     <beans:value>select username,password,enabled 
     from user 
     where username = ? 
     </beans:value> 
    </beans:property> 
    <beans:property name="dataSource" ref="dataSource" /> 
    <beans:property name="enableGroups" value="true" /> 
    <beans:property name="enableAuthorities" value="false" /> 
    <beans:property name="groupAuthoritiesByUsernameQuery"> 
     <beans:value>SELECT R.ID, R.NAME, P.NAME 
      FROM ROLE R 
      JOIN USER_ROLE UR on R.id = UR.role_id 
      JOIN USER U on U.id = UR.user_id 
      JOIN ROLE_PERMISSION RP ON RP.role_id = R.id 
      JOIN PERMISSION P ON P.id = RP.permission_id 
      WHERE U.username=? 
     </beans:value> 
    </beans:property> 
</beans:bean> 

正常情況下,一切運行正常。我可以通過jQuery.ajax請求我的頁面,我的回調按預期工作。但是,我不知道如何設置以處理會話超時或未經授權的訪問響應。

例如,如果會話超時,並且我繼續發出Ajax請求,Spring Security會將呼叫重定向到登錄頁面。所以對Ajax請求的響應最終成爲登錄頁面。在客戶端,我需要能夠知道用戶不再訪問請求的頁面並採取適當的行動 - 即:將瀏覽器重定向到登錄/錯誤頁面。如果用戶沒有訪問網址的權限,則也是如此。

我發現了類似的帖子,涉及如何通過ajax配置登錄,但無法理解如何通過ajax處理未經授權的請求。我假設在Ajax調用的情況下,服務器應該返回一個特定的狀態代碼(例如401未授權的等),並讓JS處理差異代碼,但不知道在哪裏/如何配置該信息。

我試過看AuthenticationFailureHandlerAuthenticationSuccessHandler類,但它們似乎甚至不能用於我的配置(設置它們的斷點甚至沒有命中),所以我真的很難理解什麼/如何/在哪裏配置必要的處理程序/過濾器/等。

回答

0

您可以嘗試使用禁止訪問的處理程序在你的HTTP標籤像這樣

<http auto-config="true"> 
<intercept-url pattern="/admin*" access="ROLE_ADMIN" /> 
<access-denied-handler ref="accessDeniedHandler"/> 

<bean id="accessDeniedHandler" 
class="CustomAccessDeniedHandler"> 
<property name="accessDeniedUrl" value="acessDenied" /> 

您可以創建自己的處理程序實現Spring的AccessDeniedHandler,然後覆蓋handle()方法。

public class CustomAccessDeniedHandler implements AccessDeniedHandler { 

    private String accessDeniedUrl; 

    public String getAccessDeniedUrl() { 
    return accessDeniedUrl; 
} 

public void setAccessDeniedUrl(String accessDeniedUrl) { 
    this.accessDeniedUrl = accessDeniedUrl; 
} 

@Override 
public void handle(HttpServletRequest request, 
    HttpServletResponse response, 
    AccessDeniedException accessDeniedException) throws IOException, 
    ServletException { 

     // Your own logic something like this 

    response.sendRedirect(accessDeniedUrl); 
    request.getSession().setAttribute("message", 
    "You do not have permission to access this page!"); 

} 

}

accesDenied.jsp可以是​​這樣的

<html> 
<body> 
<h1>HTTP Status 403 - Access is denied</h1> 
<h3>Message : ${message}</h3>  
</body> 
</html>