<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: java code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 28 Aug 2008 13:10:34 GMT</pubDate>
    <description>DZone Snippets: java code</description>
    <item>
      <title>Overriding Tomcat Valve to return extended login failure status</title>
      <link>http://snippets.dzone.com/posts/show/3715</link>
      <description>See &lt;a href="http://shadegrowncode.blogspot.com/2007/03/returning-login-failure-reason-in.html"&gt;Shade Grown Code&lt;/a&gt; for more information.&lt;br /&gt;&lt;br /&gt;ExtendedStatusSetter.java&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.ofc.tomcat;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Interface flagging that the implementing Realm can set request&lt;br /&gt; * headers providing additional information about an authentication&lt;br /&gt; * failure.&lt;br /&gt; *&lt;br /&gt; * @author Nicholas Sushkin&lt;br /&gt; */&lt;br /&gt;public interface ExtendedStatusSetter&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * The request attribute under which we forward an extended failure status message&lt;br /&gt;     * (as an object of type String) to a login error page.&lt;br /&gt;     */&lt;br /&gt;    public static String LOGIN_FAILURE_MESSAGE_ATTR = &lt;br /&gt;        "com.ofc.tomcat.LOGIN_FAILURE_MESSAGE";&lt;br /&gt;    &lt;br /&gt;    public void setExtendedStatus(String username, HttpServletRequest request, HttpServletResponse response);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;ExtendedStatusFormAuthenticator.java&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.ofc.tomcat;&lt;br /&gt;&lt;br /&gt;import org.apache.catalina.authenticator.Constants;&lt;br /&gt;import org.apache.catalina.authenticator.FormAuthenticator;&lt;br /&gt;import org.apache.catalina.Realm;&lt;br /&gt;import org.apache.catalina.connector.Request;&lt;br /&gt;import org.apache.catalina.connector.Response;&lt;br /&gt;import org.apache.catalina.deploy.LoginConfig;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;import javax.servlet.RequestDispatcher;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Adds extended authentication failure status to tomcat FormAuthenticator.&lt;br /&gt; *&lt;br /&gt; * @author Nicholas Sushkin&lt;br /&gt; */&lt;br /&gt;public class ExtendedStatusFormAuthenticator extends FormAuthenticator&lt;br /&gt;{&lt;br /&gt;    /**&lt;br /&gt;     * Descriptive information about this implementation.&lt;br /&gt;     */&lt;br /&gt;    protected static final String info =&lt;br /&gt;        "com.ofc.tomcat.ExtendedStatusFormAuthenticator/1.0";&lt;br /&gt;&lt;br /&gt;    private static Log log = LogFactory.getLog(ExtendedStatusFormAuthenticator.class);&lt;br /&gt;&lt;br /&gt;    // ------------------------------------------------------------- Properties&lt;br /&gt;    /**&lt;br /&gt;     * Return descriptive information about this Valve implementation.&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    public String getInfo() &lt;br /&gt;    {&lt;br /&gt;        return info;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // ------------------------------------------------------------- Overridden behavior&lt;br /&gt;    /**&lt;br /&gt;     * Called to forward to the error page&lt;br /&gt;     * &lt;br /&gt;     * @param request Request we are processing&lt;br /&gt;     * @param response Response we are creating&lt;br /&gt;     * @param config    Login configuration describing how authentication&lt;br /&gt;     *              should be performed&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    protected void forwardToErrorPage(Request request, Response response, LoginConfig config) &lt;br /&gt;    {&lt;br /&gt;        Realm realm = context.getRealm();&lt;br /&gt;&lt;br /&gt;        if (realm instanceof ExtendedStatusSetter)&lt;br /&gt;        {&lt;br /&gt;            log.debug("realm implements ExtendedStatusSetter, setting extended status for error page");&lt;br /&gt;            String username = request.getParameter(Constants.FORM_USERNAME);&lt;br /&gt;            ((ExtendedStatusSetter) realm).setExtendedStatus(username, request.getRequest(), response.getResponse());&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            log.debug("realm does not implement ExtendedStatusSetter, NOT setting extended status for error page");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        RequestDispatcher disp =&lt;br /&gt;            context.getServletContext().getRequestDispatcher&lt;br /&gt;            (config.getErrorPage());&lt;br /&gt;        try {&lt;br /&gt;            disp.forward(request.getRequest(), response.getResponse());&lt;br /&gt;            response.finishResponse();&lt;br /&gt;        } catch (Throwable t) {&lt;br /&gt;            log.warn("Unexpected error forwarding to error page", t);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Realm implementation will include the following&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class AccountLockoutDatasourceRealm extends DataSourceRealm implements ExtendedStatusSetter&lt;br /&gt;{&lt;br /&gt;    // ...&lt;br /&gt;&lt;br /&gt;    public void setExtendedStatus(String username, HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;    {&lt;br /&gt;        setMessage(request, "Account locked");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected void setMessage(HttpServletRequest request, String message)&lt;br /&gt;    {&lt;br /&gt;        request.setAttribute(ExtendedStatusSetter.LOGIN_FAILURE_MESSAGE_ATTR, message);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Mar 2007 22:25:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3715</guid>
      <author>NicholasSushkin (Nicholas Sushkin)</author>
    </item>
  </channel>
</rss>
