Grails Spring Security:登录成功/失败后重定向 [英] Grails Spring Security: redirect after login success/failure

查看:115
本文介绍了Grails Spring Security:登录成功/失败后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Grails应用程序中使用Spring Security插件的1.2版本。我希望以下列方式处理登录尝试:

成功




  • 如果登录是由尝试访问受保护页面触发的,请将其发送到该页面

  • ,如果用户直接将其重定向到主页面b

    $ b

    失败




    • 将它们发送给再试一次登录页面并使用他们输入的无效登录详细信息填充表单(密码字段除外)。这个再试一次的登录页面与第一次登录页面不一样。



    我看了一下活动部分插件的手册,这似乎涵盖了这一点。然而,在这些事件处理程序中,似乎没有任何方法可以重定向用户。

    解决方案

    当您点击一个安全链接并且没有登录时,一个 SavedRequest 存储在会话中以跟踪您想要去的地方。成功登录后,将检查此对象,并从中构建重定向。如果直接进入登录页面,则没有保存的信息,因此它会重定向到默认位置。默认情况下,它是应用程序的根目录('/'),但它是可配置的,例如

      grails.plugins.springsecurity.successHandler.defaultTargetUrl ='/ home'

    先前输入的登录名称将重新显示,请在 auth.gsp 中使用 SPRING_SECURITY_LAST_USERNAME 会话密钥:

     < input type ='text'class ='text_'name ='j_username'id ='username'
    value = $ {session ['SPRING_SECURITY_LAST_USERNAME']}/>


    I'm using version 1.2 of the Spring Security plugin in a Grails application. I want login attempts to be handled in the following way:

    Success

    • if the login was triggered by an attempt to access a protected page, send them to that page
    • if the user logged in "directly" redirect them back to the home page

    Failure

    • Send them to a "try again" login page and populate the form thereon with the invalid login details they entered (except for the password fields). This "try again" login page is not the same page that they use to login the first time

    I've had a look at the Events section of the plugin's manual, which seems to cover this ground. However there doesn't seem to be any way to redirect a user within these event handlers.

    解决方案

    You're basically describing how it currently works except for re-displaying login details, which is straightforward.

    When you click a secured link and aren't logged in, a SavedRequest is stored in the session to keep track of where you were trying to go. After a successful login, this object is inspected and the redirect is built from it. If you go directly to the login page there's no saved info, so it redirects to a default location. By default it's the root of the app ('/') but it's configurable, e.g.

    grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/home'
    

    To get the previously entered login name to re-display, use the SPRING_SECURITY_LAST_USERNAME session key in auth.gsp:

    <input type='text' class='text_' name='j_username' id='username'
           value="${session['SPRING_SECURITY_LAST_USERNAME']}" />
    

    这篇关于Grails Spring Security:登录成功/失败后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆