Grails Spring Security Core expired-credential redirect after authentication
0 reputation · 04 Apr 2024, 09:31 UTC
0 reputation · 04 Apr 2024, 09:31 UTC
Configure the Grails Spring Security Core plugin so that a user account with passwordExpired = true is sent to an application-defined password-change controller immediately after authentication, rather than to the default target URL.
The core plugin does not supply a mandatory post-login redirect or password-change controller. Expired credentials surface as CredentialsExpiredException during authentication, but the recovery path and any temporary role relaxation for the reset controller must be defined by the application. This assumes a database-backed user lookup and Grails 5 or 6 with Spring Security Core 5.x; LDAP or custom providers may behave differently.
CredentialsExpiredException after a successful login?35775 reputation · 04 Apr 2024, 17:25 UTC
Intercept at the authentication-failure layer, not the success layer. In Spring Security Core 5.x, CredentialsExpiredException is usually thrown by the UserDetailsService during authenticate(), before Spring Security builds a complete Authentication token. Because of that, AuthenticationSuccessHandler does not run. Register a custom AuthenticationFailureHandler (or an ApplicationListener for authentication failure events) that checks for CredentialsExpiredException and redirects to your password-change controller.
If your plugin version catches the exception and lets authentication complete instead, then AuthenticationSuccessHandler is the correct point. That version difference changes the implementation, so confirm it before copying either pattern.
passwordExpired = true on the user domain class can trigger CredentialsExpiredException during authentication.isAuthenticated() without granting unrelated authorities.PasswordChangeController with a change action that allows the current authenticated user to set a new password and clears passwordExpired.AuthenticationFailureHandler in grails-app/conf/spring/resources.groovy:import org.springframework.security.authentication.CredentialsExpiredException
import org.springframework.security.web.authentication.AuthenticationFailureHandler
beans = {
expiredCredentialFailureHandler(ExpiredCredentialFailureHandler)
}
class ExpiredCredentialFailureHandler implements AuthenticationFailureHandler {
private final AuthenticationFailureHandler defaultHandler
ExpiredCredentialFailureHandler(AuthenticationFailureHandler defaultHandler) {
this.defaultHandler = defaultHandler
}
void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
if (exception instanceof CredentialsExpiredException) {
response.sendRedirect("${request.contextPath}/passwordChange/change")
return
}
defaultHandler.onAuthenticationFailure(request, response, exception)
}
}
grails-app/conf/application.groovy or springSecurityConfig.groovy, add the request map:grails.plugin.springsecurity.requestMap.urlMappings = [
'/passwordChange/**': ['access': 'isAuthenticated()'],
'/secured/**': ['access': 'isAuthenticated() and !hasRole(\'ROLE_PASSWORD_EXPIRED\')']
]
Adjust the second entry to match your existing secured URL pattern; do not blanket-protect every path if you already use annotations or intercept-url-map.
ROLE_PASSWORD_EXPIRED in the failure handler or a post-authentication filter so the reset controller remains reachable while other secured URLs reject the user./passwordChange/** from any block that applies to expired users. Block all other secured endpoints until the password is reset.| Path | Access expression | Purpose |
|---|---|---|
| /passwordChange/** | isAuthenticated() | Expired users can reach the reset form |
| /secured/** (other) | isAuthenticated() and !hasRole('ROLE_PASSWORD_EXPIRED') | Block expired users until reset |
CredentialsExpiredException may differ between Spring Security Core 5.x releases; verify the exact exception class and whether authentication aborts in your version.passwordExpired = true and asserts the response redirect location is /passwordChange/change.CredentialsExpiredException and the forwarded or redirected path after login./passwordChange/** is not excluded by a broader secured pattern.Missing diagnostic detail: Does your Spring Security Core version abort authentication with CredentialsExpiredException, or does authentication succeed and expose passwordExpired on the returned principal? That determines whether to use AuthenticationFailureHandler or AuthenticationSuccessHandler.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.