how to display custom error message in jsp for spring security auth exception

Viewed 40473

I want to display custom error message in jsp for spring security authentication exceptions.

For wrong username or password,

spring displays : Bad credentials
what I need     : Username/Password entered is incorrect.

For user is disabled,

spring displays : User is disabled
what I need     : Your account is diabled, please contact administrator.

Do I need to override AuthenticationProcessingFilter just for this ? or else can I do something in jsp itself to find the authentication exception key and display different message

4 Answers

Here is a JSP EL fix for this. More of a hack than an elegant solution, but gets the job done quick and dirty. Caveat- this is not i18n safe! Only English.

This requires the functions tag library:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

And the replace code:

${fn:replace(SPRING_SECURITY_LAST_EXCEPTION.message, 'Bad credentials', 'Username/Password are incorrect')}
Related