grails acegi ajax登錄
Posted on 2011-01-07 14:47 asdtiang 閱讀(754) 評(píng)論(2) 編輯 收藏 所屬分類: grails studyjs代碼如下:
<script type='text/javascript'>
/////ajax登錄js
function showLogin() {
$('ajaxLogin').style.display = 'block';
}
function onSuccessfulLogin(url) {
//alert(url);
document.form1.action=url;
document.form1.submit();
}

function authAjax() {
var form = document.ajaxLoginForm;
var params = Form.serialize(form)
+ '&spring-security-redirect=/login/ajaxSuccess';
//Form.disable(form);
new Ajax.Request(form.action, {
method : 'POST',
postBody : params,
onSuccess : function(response) {
var responseText = response.responseText || '[]';
var json = responseText.evalJSON();
if (json.success) {
//alert("ok");
onSuccessfulLogin(json.url);
} else if (json.error) {
alert(json.error);
Form.enable(document.ajaxLoginForm);
} else {
Element.update('loginMessage', responseText);
Form.enable(document.ajaxLoginForm);
}
}
});
}
</script>
login代碼示例:
def ajaxSuccess = {
nocache(response)
def config = authenticateService.securityConfig.security
def postUrl = "${request.contextPath}${config.defaultTargetUrl}"
render "{success: true,url:'${postUrl}'}"http://這里返回的是json
}



def authfail = {

def username = session[AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
def msg = ''
def exception = session[AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY]
if (exception) {
if (exception instanceof DisabledException) {
msg = "[$username] 用戶被禁用."
}
else {
msg = "[$username] 錯(cuò)誤的用戶名或密碼."
}
}

if (isAjax()) {
render "{error: '${msg}'}"
}
else {
flash.message = msg
redirect action: auth, params: params
}
}



頁面:
天蒼蒼,野茫茫,風(fēng)吹草底見牛羊




































login代碼示例:



































頁面:
<form id="form1" name="form1" target="_blank"></form>
<form action='${request.contextPath}/j_spring_security_check'
method='POST' id='ajaxLoginForm' name='ajaxLoginForm' target="_blank">
用戶名<input id="j_username" name="j_username" type="text" />
密 碼<input name="j_password" id="j_password" type="password" /><input onclick='authAjax(); return false;'type="image" src="login.jpg" />
</form>
暫時(shí)想不起來為什么用兩個(gè)form呢,最后引入js:<g:javascript library="prototype" />
天蒼蒼,野茫茫,風(fēng)吹草底見牛羊