views:

4031

answers:

5

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  A: 

Redefine the properties in messages.properties inside spring security jar. For example add to the classpath myMessages.properties and add a message source to the context:

BindAuthenticator.badCredentials=Username/Password entered is incorrect.
AbstractUserDetailsAuthenticationProvider.disabled=Your account is diabled, please contact administrator.

At Salvin Francis:

  1. Add myMessages.properties to the WAR file inside WEB-INF/classes.
  2. Add this bean to spring context config file

Message Source Bean

<bean id="messageSource"   
    class="org.springframework.context.support.ResourceBundleMessageSource">  
    <property name="basenames">  
        <list>
            <value>myMessages</value>
        </list>
    </property>
</bean>
rodrigoap
Thanks. It worked.
novice
hi, could you be more specific so as to how to "bind" spring to accept myMessages.properties as compared to its own message.properties ?
Salvin Francis
there you have Salvin Francis
rodrigoap
A: 

Hi rodrigoap, I am a newbie to spring and currently I am facing the same problem and not able to resolve the issue . could you pls eloberate your solution for the current problem actually I tried to modify the messages.properties file in securtiy jar but nothg is changing . Help!!!!

vinay
Hi Vinay, rodrigoap was telling that messages.properties in spring security jar has to be redefined, it doesn't mean that you have to repack the jar after modifying the file. You can create a new custom_messages.properties and define the same property key and your new property value and add it to the context.
novice
If you have specific additional questions it would probably be better to ask them as a new question (The "Ask Question" button is in the top right of the page). More people would look at it and try to help you that way.
sth
Don't modify that file. Just create a new one with the same keys. Added more info to my original response.
rodrigoap
A: 

I am new to spring, but try this:

@the server:

throw new BadCredentialsException("This is my custom message !!");

Of course you need a class that is an authentication provider for this to work.

Salvin Francis
A: 

Thanks much for the great solution.

chuynh
A: 

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')}
danny