Results 1 to 3 of 3

Thread: Spring Security Plugin Ajax Grails not working

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    Angry Spring Security Plugin Ajax Grails not working

    Hello experts,

    I've spent my hole day trying to make this code work, but i think because of my lack of knowledge in javascript i failed.

    I'm trying to implement a login form in my system, but using ajax since i got the login option in every single page of the app.

    So this little form is localized in the header of my app.

    The form code was copied from the Spring Plugin documentation (_header):

    HTML Code:
                                           <form action='${request.contextPath}/j_spring_security_check'
                                                    method='POST' id='ajaxLoginForm' name='ajaxLoginForm'>
                                                    <p>
                                                            <label for='username'>Login ID</label> <input type='text'
                                                                    class='text_' name='j_username' id='username' />
                                                    </p>
                                                    <p>
                                                            <label for='password'>Password</label> <input type='password'
                                                                    class='text_' name='j_password' id='password' />
                                                    </p>
                                                    <p>
                                                            <label for='remember_me'>Remember me</label> <input
                                                                    type='checkbox' class='chk' id='remember_me'
                                                                    name='_spring_security_remember_me' />
                                                    </p>
                                                    <p>
                                                            <a href='javascript:void(0)' onclick='authAjax(); return false;'>Login
                                                    </p>
                                            </form>
    And the javascript code was also copied from the documentation and it is (_config):

    Code:
    <script type='text/javascript'> // center the form Event.observe(window, 'load', function() { var ajaxLogin = $('ajaxLogin'); $('ajaxLogin').style.left = ((document.body.getDimensions().width - ajaxLogin.getDimensions().width) / 2) + 'px'; $('ajaxLogin').style.top = ((document.body.getDimensions().height - ajaxLogin.getDimensions().height) / 2) + 'px'; }); function showLogin() { $('ajaxLogin').style.display = 'block'; } function cancelLogin() { Form.enable(document.ajaxLoginForm); Element.hide('ajaxLogin'); } function authAjax() { Form.enable(document.ajaxLoginForm); Element.update('loginMessage', 'Sending request ...'); Element.show('loginMessage'); var form = document.ajaxLoginForm; var params = Form.serialize(form); Form.disable(form); new Ajax.Request(form.action, { method: 'POST', postBody: params, onSuccess: function(response) { Form.enable(document.ajaxLoginForm); var responseText = response.responseText || '[]'; var json = responseText.evalJSON(); if (json.success) { Element.hide('ajaxLogin'); $('loginLink').update('Logged in as ' + json.username + '(<%=link(controller: 'logout') { 'Logout' }%>)' ); /} else if (json.error) { Element.update('loginMessage', "<span class='errorMessage'>" + json.error + '</error>'); } else { Element.update('loginMessage', responseText); } } }); } </script>
    This code can be found in the following link:

    http://grails-plugins.github.com/gra...ntication.html

    In the documentation example they propose to show a window in the middle of the screen when the user clicks on the logging link... in my case i dont need those javascript events, because i'm already doing this when the user clicks on "Fazer login".

    After the login process i want to update the app header showing the username and etc..

    I 've done this without ajax, but when the logging fails it redirects my user to the login/auth page... and i don't want this.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,834

    Default

    It sounds like you will need to wire a custom AuthenticationFailureHandler. The default will send a redirect to the login page. I'm not all that familiar with the Grails plugin but this should be enough to get you in the right spot.
    Rob Winch
    Twitter @rob_winch
    Spring Security Lead
    Spring by Pivotal

  3. #3
    Join Date
    Jul 2012
    Posts
    5

    Default

    Ok Rob,

    Right now i leaved the ajax authentication and i'm using the sample login form.
    <form class="dropdown-menu pull-right"
    action="${resource(file: 'j_spring_security_check')}"
    method="POST">

    <g:textField class='span2' name='j_username' placeholder="E-mail" />
    <gasswordField class='span2' name='j_password'
    placeholder="Senha" />
    <label><g:checkBox class='pull-left'
    name='_spring_security_remember_me' /> Mantenha-me conectado</label>
    <a href="password-resend.php" class="pull-left">Esqueci minha senha</a>
    <button type="submit" name="login"
    class="btn btn-success btn-small pull-right">Entrar</button>

    </form>
    The only problem is that i want to return to the form page after submitting it, so i found the Config.groovy variables:

    grails.plugins.springsecurity.failureHandler.defau ltFailureUrl = '/'
    grails.plugins.springsecurity.successHandler.defau ltTargetUrl="/main.gsp"

    But i have the logging form in every page of the system... so i want to return to the last page... like a refresh.

    How can i do this? I crowled the web, but no awnser yet

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •