2012-10-21 78 views
1

我在我的應用程序中使用彈簧安全。現在我試圖讓我記住功能正常工作。這裏是我的spring-security.xml:春季安全記住我問題

<?xml version="1.0" encoding="UTF-8"?> 

<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.1.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <context:component-scan base-package="core"/> 

    <http auto-config="false" use-expressions="true"> 
     <intercept-url pattern="/views/**" access="permitAll"/> 
     <form-login authentication-failure-handler-ref="loginFailure" 
        authentication-success-handler-ref="loginSuccess"/> 
     <remember-me key="key"/> 
     <logout 
       invalidate-session="true" 
       delete-cookies="true"/> 

    </http> 


    <authentication-manager> 
     <authentication-provider> 
      <password-encoder hash="md5"/> 
      <jdbc-user-service data-source-ref="ds" 
           users-by-username-query="SELECT email, password, enabled FROM tuser WHERE email=?" 
           authorities-by-username-query="SELECT tuser, role_id FROM user_role WHERE tuser=?"/> 
     </authentication-provider> 
    </authentication-manager> 

    <global-method-security pre-post-annotations="enabled" secured-annotations="enabled" /> 

</beans:beans> 

這是我的應用程序代碼。 JSP:

<form:form method="POST"> 

     <table> 
      <tr> 
       <td>User:</td> 
       <td> 
        <input type="text" id="j_username"/> 
       </td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td> 
        <input type="password" id="j_password"/> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="checkbox" id="_spring_security_remember_me"/> 
       </td> 
       <td> 
        <s:message code="RememberMe"/> 
       </td> 
      </tr> 
      <tr> 
       <td colspan='2'> 
        <input id="loginBtn" name="submit" type="button" onclick="ajaxLogin()" value="Войти"/> 
       </td> 
      </tr> 
     </table> 

    </form:form> 

和JavaScript,使AJAX登錄:

function ajaxLogin() { 
    $("#errorText").css('display', 'none'); 
    $('#loginBtn').attr('disabled', 'true'); 

    var user_name = $("#j_username").val(); 
    var user_pass = $("#j_password").val(); 
    var rememberMe = $("#_spring_security_remember_me").prop("checked"); 

    $.ajax({ 
     url:"../../j_spring_security_check", 
     data:{ j_username:user_name, j_password:user_pass, _spring_security_remember_me:rememberMe}, 
     type:"POST", 
     beforeSend:function (xhr) { 

      xhr.setRequestHeader("login-ajax", "true"); 
     }, 
     success:function (result) { 

      if (result == "ok") { 
       window.location.reload() 
      } else { 
       $("#errorText").css('display', 'block'); 
      } 

     }, 
     error:function (XMLHttpRequest, textStatus, errorThrown) { 
      return false; 
     } 
    }) 
    ; 
} 

的問題是,春天記住用戶即使我不檢查的記着我的登錄表單選項。如果我從配置文件中刪除,它不記得。這個參數的存在是否會使彈簧的行爲如此呢?我如何控制記住我的功能? 在此先感謝!

回答

1

屬性刪除餅乾=在標籤註銷「真」

<logout delete-cookies="true"/> 

不是一個boolean類型是逗號分隔的cookie的列表文件名

在標籤記得我

<remember-me/> 

存在屬性token-validity-seconds,它決定您的令牌有效秒數

例如

<remember-me key="key" token-validity-seconds="2419200" /> 

意味着1個月

也許當你登錄第一次在這之後註銷您的令牌不被刪除,仍然存在, 但是當你刪除標籤

<remember-me/> 

它不工作,一切都正確