Pages

Friday 9 March 2012

Send flash message from spring security

When I log in, I want to see a flash message, saying "Successful login"
So as you hit the login page, you are served by the secutity filter, and according to the PRG pattern, you will be redirected to the page originally requested. I extended spring's SavedRequestAwareAuthenticationSuccessHandler so that it will ask a SessionFlashMapManager to populate the flash message to the session.
On My context xml:
<bean class="somepackage.AuthenticationSuccessHandler" name="authSucc">
        <property name="messageKey" value="flashmessage"/>
        <property name="message" value="flashmessage.afterlogin"/>
</bean>
...

<security:http auto-config="true" use-expressions="true">
...
    <security:form-login login-page='/login' authentication-success-handler-ref="authSucc"/>
...
</security:http>
You have to define the value in your messages.properties:
flashmessage.afterlogin=Successful login

And its implementation is:


package somepackage;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.support.SessionFlashMapManager;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by matelakat at 3/9/12 7:44 PM
 */
public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessHandler.class);


    private ApplicationContext applicationContext;
    private String messageKey;
    private String message;


    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    public String getMessageKey() {
        return messageKey;
    }


    public void setMessageKey(String messageKey) {
        this.messageKey = messageKey;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws ServletException, IOException {
        SessionFlashMapManager sessionFlashMapManager = new SessionFlashMapManager();
        FlashMap fm = new FlashMap();
        fm.put(getMessageKey(), getMessage());
        sessionFlashMapManager.saveOutputFlashMap(fm, request, response);
        super.onAuthenticationSuccess(request, response, authentication);
    }
}

On my freemarker template:
<#import "spring.ftl" as spring>
<#if flashmessage??>
    <div id="message"><@spring.message flashmessage/></div>
</#if>


1 comment: