asdtiang的博客 感谢blogjava提供的博客交流平台

grails acegi ajax登录

Posted on 2011-01-07 14:47 asdtiang 阅读(733) 评论(2)  编辑  收藏 所属分类: grails study
js代码如下:

<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}'}"//这里返回的是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] 错误的用户名或密码."
            }

        }


        
if (isAjax()) {
            render 
"{error: '${msg}'}"
        }

        
else {
            flash.message 
= msg
            redirect action: auth, params: params
        }

    }






页面:
 

<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" />   

&nbsp;&nbsp;&nbsp;&nbsp;<input name="j_password" id="j_password" type="password" /><input onclick='authAjax(); return false;'type="image" src="login.jpg" />

</form>


暂时想不起来为什么用两个form呢,最后引入js:<g:javascript library="prototype" />



天苍苍,野茫茫,风吹草底见牛羊

Feedback

# re: grails acegi ajax登录[未登录]  回复  更多评论   

2011-05-14 19:24 by oksonic
你好,看了这篇文章,实际了登录跳转,非常感谢。
但我不能明白的是authfail为什么实现不了?

# re: grails acegi ajax登录  回复  更多评论   

2011-05-18 21:59 by asdtiang
代码贴出来看下,不过,我想应该解决了吧

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)