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!
I ended up using this
def savedrequest = SpringSecurityUtils.getSavedRequest(session)
def requestURI = savedrequest.requestURI ?: ''
def contextPath = savedrequest?.contextPath ?: ''