0

In grails 2.2 we could get the saved uri to redirect before login as follows. The redirect url would be saved in session[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]
}

Is there a way to achieve the same in grails 4 and spring security core 4?

Here is what i did

I copied the Login controller class from the plugin to override auth method.

The default auth looks like this and i put the line 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]
}

I get this error.

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

I read the documentation in full and couldnt find anything related to fetching saved request uri from session?

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

i appreciate any help. Thanks!

kofhearts
  • 3,607
  • 8
  • 46
  • 79
  • I think you are referring to a property that does not exist. Does `SpringSecurityUtils.SAVED_REQUEST` work? – Jeff Scott Brown Jan 11 '22 at 13:48
  • If you have a reference to the session, you might use https://github.com/grails/grails-spring-security-core/blob/c714537385cac605c1b99b876c77ebf04c2bdfcb/plugin/src/main/groovy/grails/plugin/springsecurity/SpringSecurityUtils.groovy#L624-L626. – Jeff Scott Brown Jan 11 '22 at 17:17

1 Answers1

0

I ended up using this

    def savedrequest = SpringSecurityUtils.getSavedRequest(session)

    def requestURI = savedrequest.requestURI ?: ''
    def contextPath = savedrequest?.contextPath ?: ''
kofhearts
  • 3,607
  • 8
  • 46
  • 79