How to change authentication success/failure message in JBoss Seam?
If we rely on JBoss Seam’s Security Identity Management for user authentication on a web application, we certainly think of changing the default ‘Welcome <username>’ to something different and catchy. This particular tip explores such a possibility successfully in 2-3 steps:
1) Assuming that you have defined the security identity and core resource loader components have been defined in WEB-INF/components.xml as below, we have to modify the properties myapplication_en.properties (_en is the default locale)
<security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
<core:resource-loader bundle-names="myapplication"/>
define your custom message like this, where #0 is a parameter placeholder.
welcome.message=Good to see you, #0 loginfailed.message=Unable to login mate, please check your credentials!
2) Modify your authenticator component to add custom message if login/authentication is successful.
public boolean authenticate()
{ User loggedInUser = authenticationservice.autheticate(credentials.getUsername(), credentials.getPassword());
FacesMessages messages = FacesMessages.instance();
if(loggedInUser==null)
{ messages.addFromResourceBundle("loginfailed.message", null);
return false;
}
messages.addFromResourceBundle("welcome.message", loggedInUser.getFullname());
return true;
}
Are we done? wait… We need to reset the earlier messages (the default ones)
3) Add two more entries to myapplication_en.properties and set these properties as blank. This would reset the existing default ones.
org.jboss.seam.loginSuccessful= org.jboss.seam.loginFailed=
Aint it easy and cool!