[英]Spring security remember me issue
我在我的應用程序中使用Spring Security。 現在,我想讓我記住功能正常工作。 這是我的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>
和使ajax登錄的javascript:
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;
}
})
;
}
問題是即使我沒有在登錄表單中選中“記住我”選項,Spring也會記住用戶。 如果我從配置文件中刪除,它將不記得了。 這個參數的存在會使spring表現得像這樣嗎? 如何控制“記住我”功能? 提前致謝!
標簽注銷中的屬性delete-cookies =“ true”
<logout delete-cookies="true"/>
不是布爾類型,它是逗號分隔的cookie文件名列表
在標簽中記住我
<remember-me/>
存在屬性token-validity-seconds,它確定令牌有效的秒數
例如
<remember-me key="key" token-validity-seconds="2419200" />
意味着一個月
也許當您第一次登錄時,並且在此注銷之后,您的令牌不會被刪除並且仍然存在,但是當您刪除標簽時
<remember-me/>
它不起作用,一切都正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.