PDA

View Full Version : Translate messages



phe
Jun 29th, 2012, 10:27 AM
Hi

I found out how to translate some messages for exampel


AbstractUserDetailsAuthenticationProvider.badCrede ntials

My problem is that I got this message below that I would like to translate. I searched in the message.properties file of Spring Security but there is no such message.

https://fisheye.springsource.org/browse/~br=master/spring-security/core/src/main/resources/org/springframework/security/messages.properties?r=59ac4c8b964c8ffa86084e53b99f c56fe1415516&r=6db747292815b626f8c6f94bc8e48c2d85cc3f53&r=6db747292815b626f8c6f94bc8e48c2d85cc3f53&r=6db747292815b626f8c6f94bc8e48c2d85cc3f53&r=6db747292815b626f8c6f94bc8e48c2d85cc3f53&r=59ac4c8b964c8ffa86084e53b99fc56fe1415516

How can I make a custom translation of the error below?


UserDetailsService returned null, which is an interface contract violation

I also looked into the actual AbstractUserDetailsAuthenticationProvider class and the only clue is this but can't find anything similar in message.properties.

Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract");

Rob Winch
Jun 29th, 2012, 02:44 PM
Not all Exceptions can be modified (only messages that are intended to be displayed to users). Exceptions that are displayed to an admin or developer are not translated (this is one such case).

phe
Jul 2nd, 2012, 12:25 AM
Thank you. I did not throw the exception before. Now I do and everything works as expected.



@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
User person = personService.getPersonByEmail(username);
//this is new
if (person == null) {
throw new UsernameNotFoundException("User " + username + " not found!");
}
return person;
}