Skip to content

Instantly share code, notes, and snippets.

@KeeTraxx
Created October 1, 2018 08:31
Show Gist options
  • Save KeeTraxx/56cb753bb1a877390df166167c4880d7 to your computer and use it in GitHub Desktop.
Save KeeTraxx/56cb753bb1a877390df166167c4880d7 to your computer and use it in GitHub Desktop.
package mygrailsapp
import grails.converters.JSON
import org.springframework.security.authentication.AccountExpiredException
import org.springframework.security.authentication.CredentialsExpiredException
import org.springframework.security.authentication.DisabledException
import org.springframework.security.authentication.LockedException
import org.springframework.security.web.WebAttributes
import org.springframework.security.web.authentication.session.SessionAuthenticationException
import org.springframework.web.servlet.support.RequestContextUtils
class LoginController extends grails.plugin.springsecurity.LoginController {
// LoginController in Grails Spring Security uses request.locale, which is wrong according to Grails 3.
//
def authfail() {
Locale locale = RequestContextUtils.getLocale(request)
String msg = ''
def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]
if (exception) {
if (exception instanceof AccountExpiredException) {
msg = messageSource.getMessage('springSecurity.errors.login.expired', null, "Account Expired", locale)
} else if (exception instanceof CredentialsExpiredException) {
msg = messageSource.getMessage('springSecurity.errors.login.passwordExpired', null, "Password Expired", locale)
} else if (exception instanceof DisabledException) {
msg = messageSource.getMessage('springSecurity.errors.login.disabled', null, "Account Disabled", locale)
} else if (exception instanceof LockedException) {
msg = messageSource.getMessage('springSecurity.errors.login.locked', null, "Account Locked", locale)
} else if (exception instanceof SessionAuthenticationException) {
msg = messageSource.getMessage('springSecurity.errors.login.max.sessions.exceeded', null, "Sorry, you have exceeded your maximum number of open sessions.", locale)
} else {
msg = messageSource.getMessage('springSecurity.errors.login.fail', null, "Authentication Failure", locale)
}
}
if (springSecurityService.isAjax(request)) {
render([error: msg] as JSON)
} else {
flash.message = msg
redirect action: 'auth', params: params
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment