如何在 grails spring security core 4 中获取默认保存的请求 uri?

how to get default saved request uri in grails spring security core 4?

在 grails 2.2 中,我们可以获取保存的 uri 以在登录前重定向,如下所示。重定向 url 将保存在会话 [WebAttributes.SAVED_REQUEST].

def auth = {
    def config = SpringSecurityUtils.securityConfig
    def redirectURL
    DefaultSavedRequest defaultSavedRequest = session[WebAttributes.SAVED_REQUEST]
    def requestURI = defaultSavedRequest?.getRequestURI() ?: ''
    def contextPath = defaultSavedRequest?.contextPath ?: ''
    if (contextPath && requestURI.startsWith(contextPath)){
        requestURI = requestURI.substring(contextPath.length())
    }
    def id
    try{
        id = (requestURI =~ /^\/.*\/.*\/(\d+)/)[0][1]
    }
    catch(IndexOutOfBoundsException e){} // ignore
    if (requestURI.startsWith('/raceRegistration/registrationAuth') && id){
        defaultSavedRequest.parameterMap.id = id
        redirectURL = g.createLink(controller: 'stagedRaceRegistration', action: 'create', params: defaultSavedRequest.parameterMap)
    }

    if (springSecurityService.isLoggedIn()) {
        redirect uri: config.successHandler.defaultTargetUrl
        return
    }

    String view = 'auth'
    String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
    render view: view, model: [postUrl: postUrl,
                               rememberMeParameter: config.rememberMe.parameter,
                               requestParams: params.findAll{ key, value -> !key.matches('controller|action|id')},
                               skipUrl: redirectURL]
}

有没有办法在 grails 4 和 spring security core 4 中实现相同的功能?

这是我做的

我从插件中复制了登录控制器 class 以覆盖 auth 方法。

默认身份验证看起来像这样,我将行 DefaultSavedRequest defaultSavedRequest = session[WebAttributes.SAVED_REQUEST].

/** Show the login page. */
def auth() {

    def conf = getConf()

    if (springSecurityService.isLoggedIn()) {
        redirect uri: conf.successHandler.defaultTargetUrl
        return
    }


DefaultSavedRequest defaultSavedRequest = session[WebAttributes.SAVED_REQUEST]



    String postUrl = request.contextPath + conf.apf.filterProcessesUrl
    render view: 'auth', model: [postUrl: postUrl,
                                 rememberMeParameter: conf.rememberMe.parameter,
                                 usernameParameter: conf.apf.usernameParameter,
                                 passwordParameter: conf.apf.passwordParameter,
                                 gspLayout: conf.gsp.layoutAuth]
}

我收到这个错误。

No such property: SAVED_REQUEST for class: org.springframework.security.web.WebAttributes. Stacktrace follows:

我完整阅读了文档,但找不到任何与从会话中获取保存的请求 uri 相关的信息?

https://grails-plugins.github.io/grails-spring-security-core/snapshot/index.html

感谢任何帮助。谢谢!

我最终使用了这个

    def savedrequest = SpringSecurityUtils.getSavedRequest(session)

    def requestURI = savedrequest.requestURI ?: ''
    def contextPath = savedrequest?.contextPath ?: ''