Skip to content

Instantly share code, notes, and snippets.

@julipool
Created December 6, 2012 10:25
Custom Authentication Failure URL Handler, Accepting No Slash URLs.
public class NoSlashAuthenticationFailureHandler implements AuthenticationFailureHandler {
protected final Log logger = LogFactory.getLog(getClass());
private String defaultFailureUrl;
public NoSlashAuthenticationFailureHandler() {
}
public NoSlashAuthenticationFailureHandler(String defaultFailureUrl) {
setDefaultFailureUrl(defaultFailureUrl);
}
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
if (defaultFailureUrl == null) {
logger.debug("No failure URL set, sending 401 Unauthorized error");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + exception.getMessage());
} else {
logger.debug("Redirecting to " + defaultFailureUrl);
response.sendRedirect(defaultFailureUrl);
}
}
/**
* The URL which will be used as the failure destination.
* @param defaultFailureUrl the failure URL, for example "loginFailed.jsp".
*/
public void setDefaultFailureUrl(String defaultFailureUrl) {
this.defaultFailureUrl = defaultFailureUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment