<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>OcpSoft &#187; Spring</title> <atom:link href="http://ocpsoft.com/tags/spring/feed/" rel="self" type="application/rss+xml" /><link>http://ocpsoft.com</link> <description>&#34;Simple Software&#34;</description> <lastBuildDate>Tue, 27 Jul 2010 15:07:50 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Spring Security &#8211; What happens after /you/ log in?</title><link>http://ocpsoft.com/java/jsf-java/spring-security-what-happens-after-you-log-in/</link> <comments>http://ocpsoft.com/java/jsf-java/spring-security-what-happens-after-you-log-in/#comments</comments> <pubDate>Fri, 24 Jul 2009 02:23:59 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[Spring]]></category> <category><![CDATA[Security]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=64</guid> <description><![CDATA[So you&#8217;ve got Spring Security up and running. Great! Now you&#8217;ve got a login page, and you just added a form on the global page menu to allow users to Login from any public page. There&#8217;s just one problem. When they log-in from a public page, they’re redirected to the default-login-url! Your users will have [...]]]></description> <content:encoded><![CDATA[<p>So you&#8217;ve got Spring Security up and running. Great! Now you&#8217;ve got a login page, and you just added a form on the global page menu to allow users to Login from any public page. There&#8217;s just one problem. When they log-in from a public page, they’re redirected to the default-login-url! Your users will have to re-navigate to the page they were already viewing when they logged in, or maybe they&#8217;ll just use the much dreaded &#8220;Back&#8221; button. That&#8217;s not a good interaction, but we have a solution. <span
id="more-64"></span></p><p><strong>UPDATE:</strong> There is a simpler, but less complete solution built in. (<a
href="http://forum.springsource.org/showthread.php?t=65123">See here</a>.) This means appending &#8220;?spring-security-redirect=/your/target/url&#8221; to your redirect to the Spring Security Filter chain.</p><div
class="featured"><center>Need some <strong>/pretty /urls</strong> in your JSF web-app? Try <a
href="http://ocpsoft.com/prettyfaces/" title="SEO | Dynamic Parameters | Bookmarks | Navigation | JSF | JSF2 | URLRewrite Filter | Open Source">PrettyFaces</a>: URL-rewriting for Java EE and JSF. (Free and <strong>open-source</strong>!)</center></div><p>If you have not already completed <a
href=http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/ id=ornu title="integrating Spring Security and JSF">integrating Spring Security and JSF</a>, please consider it, as this article depends on having a working JSF login page and managed bean.<br
/>  <br
/> <strong>Note:</strong> This approach will not work if you are invalidating/re-creating the session after a successful authentication (see <a
target="_blank" href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html#ns-session-fixation">Session Fixation attacks</a>). Supporting session invalidation would take some extra work that will not be in the scope of this article.<p/><h2>The login form</h2><p>Here is a basic JSF/Spring Security login form. It would be nice if we could enable or disable the redirect functionality, so we’ll add a hidden form field that is only rendered on demand (here we use Facelets <a
target="_blank" href="https://javaserverfaces.dev.java.net/nonav/docs/2.0/pdldocs/facelets/ui/param.html">ui:param</a> functionality for our on-off switch.)<br
/>  </p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span> <span style="color: #000066;">prependId</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;#{redirect == 'true'}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;redirect&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/c:if<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Username:&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;ocp:message</span></span>
<span style="color: #009900;">        <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{loginBean.username}&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Password:&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;ocp:message</span></span>
<span style="color: #009900;">        <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputSecret</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{loginBean.password}&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;_spring_security_remember_me&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span></span>
<span style="color: #009900;">        <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Remember me&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:selectBooleanCheckbox</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;_spring_security_remember_me&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{loginBean.rememberMe}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;login&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{loginBean.doLogin}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Login&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">styleClass</span>=<span style="color: #ff0000;">&quot;faded&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cancel&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{loginBean.doCancel}&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Cancel&quot;</span> <span style="color: #000066;">immediate</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p/><h2>The login action method</h2><p>First, check to make sure that we actually want to do a redirect after login. Do this by testing for the existence of our hidden form parameter.<br
/>  <br
/> Find the full LoginBean code <a
target="_blank" href="http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/">here</a>.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doLogin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException
<span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> redirect <span style="color: #339933;">=</span> FacesUtils.<span style="color: #006633;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;redirect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>redirect <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>redirect.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        redirect <span style="color: #339933;">=</span> PrettyContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOriginalRequestUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Map<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> sessionMap <span style="color: #339933;">=</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sessionMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>LoginRedirectFilter.<span style="color: #006633;">LAST_URL_REDIRECT_KEY</span>, redirect<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    FacesUtils.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/j_spring_security_check&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    FacesUtils.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">responseComplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p> <br
/> Before forwarding to the Spring Security /j_security_login_check intercepting filter chain, we’ll need to set the current URL into a Session attribute: “LoginRedirectFilter.LAST_URL_REDIRECT_KEY”.<br
/>  <br
/> This will be used in our custom filter after the user successfully authenticates with Spring Security.</p><p/><h2>The login filter</h2><p>Here is where we’ll check for the existence of our session attribute: “LAST_URL_REDIRECT_KEY”. If this key contains a value, then we should redirect the user to that URL. If the key does not contain a value, then we should not perform any redirect, and continue as normal.<br
/>  <br
/> One other concern is: what if authentication failed? Let&#8217;s assume that Spring Security will redirect the user to the Login Page if they send invalid credentials. We don&#8217;t want to trigger a redirect as they try to access the login page, so we also check to make sure we have a successfully authenticated user before redirecting.<br
/>  </p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Filter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.FilterChain</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.FilterConfig</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpSession</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.authentication.AnonymousAuthenticationToken</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.core.Authentication</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.core.context.SecurityContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.core.context.SecurityContextHolder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Component</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginRedirectFilter <span style="color: #000000; font-weight: bold;">implements</span> Filter
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LAST_URL_REDIRECT_KEY <span style="color: #339933;">=</span> LoginRedirectFilter.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;LAST_URL_REDIRECT_KEY&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doFilter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> ServletRequest request, <span style="color: #000000; font-weight: bold;">final</span> ServletResponse response, <span style="color: #000000; font-weight: bold;">final</span> FilterChain chain<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException
    <span style="color: #009900;">&#123;</span>
        HttpSession session <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> request<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> redirectUrl <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> session.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>LAST_URL_REDIRECT_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isAuthenticated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>redirectUrl <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>redirectUrl.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            session.<span style="color: #006633;">removeAttribute</span><span style="color: #009900;">&#40;</span>LAST_URL_REDIRECT_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            HttpServletResponse resp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpServletResponse<span style="color: #009900;">&#41;</span> response<span style="color: #339933;">;</span>
            resp.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span>redirectUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            chain.<span style="color: #006633;">doFilter</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isAuthenticated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        SecurityContext context <span style="color: #339933;">=</span> SecurityContextHolder.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>context <span style="color: #000000; font-weight: bold;">instanceof</span> SecurityContext<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            Authentication authentication <span style="color: #339933;">=</span> context.<span style="color: #006633;">getAuthentication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>authentication <span style="color: #000000; font-weight: bold;">instanceof</span> AnonymousAuthenticationToken<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// not authenticated</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>authentication <span style="color: #000000; font-weight: bold;">instanceof</span> Authentication<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> FilterConfig filterConfig<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p> <p/><h2>Web.xml</h2><p>Some specific configuration is required to ensure the proper ordering of our filters. LoginRedirectFilter&#8217;s filter-mapping must be placed after any Spring Security filters &#8211; otherwise we will redirect too soon, and authentication will never occur. You probably want to place it before any filters that apply business logic.</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>loginRedirectFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.filter.DelegatingFilterProxy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>loginRedirectFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p/><h2>Putting it all together</h2><p>This sequence diagram describes the entire process, including what Spring Security will be doing after intercepting the /j_security_check forwarded from LoginBean:<br
/> You should now have a functional LoginRedirectFilter configured in tandem with Spring Security. Please feel free to post any suggestions or questions.</p><p/> <a
target="_blank" href='http://ocpsoft.com/wp-content/uploads/2009/07/sequence.png'><img
src="http://ocpsoft.com/wp-content/uploads/2009/07/sequence-300x212.png" alt="Sequence Diagram for LoginRedirectFilter Flow" title="LoginRedirectFilter Sequence" width="300" height="212" class="alignnone size-medium wp-image-65" /></a></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/jsf-java/spring-security-what-happens-after-you-log-in/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Revisited &#8211; Acegi/Spring Security &amp; JSF Login Page</title><link>http://ocpsoft.com/java/jsf-java/revisited-acegispring-security-jsf-login-page/</link> <comments>http://ocpsoft.com/java/jsf-java/revisited-acegispring-security-jsf-login-page/#comments</comments> <pubDate>Mon, 27 Apr 2009 22:00:07 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[Spring]]></category> <category><![CDATA[Security]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=55</guid> <description><![CDATA[A correction has been made to the post: http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/, fixing an issue where FacesMessages were not being displayed on failed authentications. Because the example had initially used the @PostConstruct annotation to trigger a method to handle the error message, the handleError() method was being called before the actual authentication event had taken place, thus, the [...]]]></description> <content:encoded><![CDATA[<p>A correction has been made to the post: <a
href="http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/" target="_self">http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/</a>, fixing an issue where FacesMessages were not being displayed on failed authentications.<br
/> <span
id="more-55"></span></p><p>Because the example had initially used the @PostConstruct annotation to trigger a method to handle the error message, the handleError() method was being called before the actual authentication event had taken place, thus, the handleError() method was triggering before any BadCredentialsExceptions were stored in the Session.</p><p>Instead of creating an error handling method in the LoginBean itself, instead attach a PhaseListener which will intercept failed logins, and add the new FacesMessage before the RENDER_RESPONSE phase.</p><h3>LoginErrorPhaseListener</h3><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.BadCredentialsException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.ui.AbstractProcessingFilter</span><span style="color: #339933;">;</span>
&nbsp;
impot util.<span style="color: #006633;">FacesUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginErrorPhaseListener <span style="color: #000000; font-weight: bold;">implements</span> PhaseListener
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1216620620302322995L<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> beforePhase<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PhaseEvent arg0<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Exception</span> e <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span><span style="color: #009900;">&#41;</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>
                AbstractProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_LAST_EXCEPTION_KEY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e <span style="color: #000000; font-weight: bold;">instanceof</span> BadCredentialsException<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>
                    AbstractProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_LAST_EXCEPTION_KEY</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            FacesUtils.<span style="color: #006633;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username or password not valid.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPhase<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PhaseEvent arg0<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> PhaseId getPhaseId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> PhaseId.<span style="color: #006633;">RENDER_RESPONSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div><h3>faces-config.xml</h3><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;faces-config</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.2&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span style="color: #000066;">xmlns:xi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XInclude&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>login.LoginErrorPhaseListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faces-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><h3>Happy developing!</h3> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/jsf-java/revisited-acegispring-security-jsf-login-page/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Acegi/Spring Security JSF Integration Project continued</title><link>http://ocpsoft.com/java/acegi-spring-security-jsf-integration-project-continued/</link> <comments>http://ocpsoft.com/java/acegi-spring-security-jsf-integration-project-continued/#comments</comments> <pubDate>Fri, 17 Oct 2008 02:17:19 +0000</pubDate> <dc:creator>Derek</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Spring]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=19</guid> <description><![CDATA[We&#8217;ve gotten a good number of comments from Lincoln&#8217;s latest post on Spring Security and JSF.  A few comments have asked for further code samples on how to get this example working. We created a runnable project for this example, and it can be downloaded here. All you need to do is extract the project [...]]]></description> <content:encoded><![CDATA[<p>We&#8217;ve gotten a good number of comments from <a
href="mailto:lincoln@ocpsoft.com">Lincoln&#8217;s</a> latest post on <a
href="http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/">Spring Security and JSF</a>.  A few comments have asked for further code samples on how to get this example working.</p><p>We created a runnable project for this example, and it can be downloaded <a
href="http://code.google.com/p/springsecurity/">here</a>.</p><p><span
id="more-19"></span></p><p>All you need to do is extract the project to your workspace (We use <a
href="http://www.eclipse.org/downloads/">Eclipse</a>), and import the project.  Add the project to your server and start it up.  Next open your browser and navigate to:</p><p><em>http://localhost:8080/springsecurity/</em></p><p>Note*: You&#8217;ll need to change the URL to your server port number if it isn&#8217;t set to 8080.</p><p>Assuming all the steps above worked successfully you should see a page like the one below.</p><p
style="text-align: center;"><a
href="http://ocpsoft.com/wp-content/uploads/2008/10/home1.gif"><img
class="alignnone size-medium wp-image-22" title="home1" src="http://ocpsoft.com/wp-content/uploads/2008/10/home1-300x214.gif" alt="" width="300" height="214" /></a></p><p
style="text-align: left;">Now click on the Secret link.  This should force you to the login page.  The Secret page requires authentication to view.  Go ahead and type in the <strong>User</strong>: rod  <strong>Password</strong>: koala</p><p
style="text-align: left;">You should now be redirected to the Secret page (shown below).</p><p
style="text-align: center;"><a
href="http://ocpsoft.com/wp-content/uploads/2008/10/secret1.gif"><img
class="alignnone size-medium wp-image-23" title="secret1" src="http://ocpsoft.com/wp-content/uploads/2008/10/secret1-300x214.gif" alt="" width="300" height="214" /></a></p><p
style="text-align: left;">Go back to the Home page and try Logout, and Login with bad credentials and see what you get.  I hope this project helps to tie up any loose ends, and helps answer any outstanding questions.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/acegi-spring-security-jsf-integration-project-continued/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Acegi/Spring Security Integration &#8211; JSF Login Page</title><link>http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/</link> <comments>http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/#comments</comments> <pubDate>Fri, 10 Oct 2008 00:35:33 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[JSF]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Security]]></category> <category><![CDATA[Spring]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=16</guid> <description><![CDATA[Tutorials &#8211; What a nightmare Everyone seems to be going through hell to get a fully functional JSF login page working with Spring Security (formerly Acegi,) and yes, I did too, but there&#8217;s an EASY way to make this happen. And get this: It takes just five clear and well written lines of Java code. [...]]]></description> <content:encoded><![CDATA[<h2>Tutorials &#8211; What a nightmare</h2><p>Everyone seems to be going through hell to get a fully functional JSF login page working with Spring Security (formerly Acegi,) and yes, I did too, but there&#8217;s an <strong>EASY</strong> way to make this happen. And get this:</p><ul><li>It takes just five clear and well written lines of Java code.</li></ul><p>First, the solution. Afterwards, the dirty details. (Spring 2.5.2 was used for this example.)<br
/> You can find a downloadable working example <a
href="http://ocpsoft.com/java/acegi-spring-security-jsf-integration-project-continued/">here</a>. There is also a followup article on post-authentication redirecting, <a
href="http://ocpsoft.com/java/jsf-java/spring-security-what-happens-after-you-log-in/">here</a>.</p><p><span
id="more-16"></span></p><div
class="featured"><center>Need some <strong>/pretty /urls</strong> in your JSF web-app? Try <a
href="http://ocpsoft.com/prettyfaces/" title="SEO | Dynamic Parameters | Bookmarks | Navigation | JSF | JSF2 | URLRewrite Filter | Open Source">PrettyFaces</a>: URL-rewriting for Java EE and JSF. (Free and <strong>open-source</strong>!)</center></div><h3>The Solution:</h3><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginBean
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//managed properties for the login page, username/password/etc...</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// This is the action method called when the user clicks the &quot;login&quot; button</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doLogin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException
    <span style="color: #009900;">&#123;</span>
        ExternalContext context <span style="color: #339933;">=</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        RequestDispatcher dispatcher <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ServletRequest<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                 .<span style="color: #006633;">getRequestDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/j_spring_security_check&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        dispatcher.<span style="color: #006633;">forward</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ServletRequest<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                <span style="color: #009900;">&#40;</span>ServletResponse<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">responseComplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// It's OK to return null here because Faces is just going to exit.</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p
style="text-align: center;">&#8212;-</p><p>For anyone who was struggling because Spring Security requires you to use a Filter to intercept the login postback, thus either preventing you from being able to do JSF style validation, or visa-versa, creating a scenario where JSF can process results, but blocks Acegi from processing the request parameters.</p><p>Simply use an HttpRequestDispatcher to allow <em>both</em> JSf and Spring Security to function one after another. JSF goes first, then delegates work to a Spring Security  (thus preserving any request parameters that Spring Security is looking for.) After forwarding, tell JSF you have finished, and not to do any more work, immediately stop processing.</p><p>If the login credentials were bad, redirect to the Login page. If the credentials were good, redirect to the requested URL. You can even show a dynamic message for bad credentials. Add the following PhaseListener to your faces-config.xml in order to extract any login errors, and display a message to the user:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.BadCredentialsException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.security.ui.AbstractProcessingFilter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">uk.co.pkit.project.view.util.FacesUtils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginErrorPhaseListener <span style="color: #000000; font-weight: bold;">implements</span> PhaseListener
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1216620620302322995L<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> beforePhase<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PhaseEvent arg0<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Exception</span> e <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span><span style="color: #009900;">&#41;</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>
                AbstractProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_LAST_EXCEPTION_KEY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e <span style="color: #000000; font-weight: bold;">instanceof</span> BadCredentialsException<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>
                    AbstractProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_LAST_EXCEPTION_KEY</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            FacesUtils.<span style="color: #006633;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username or password not valid.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPhase<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PhaseEvent arg0<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> PhaseId getPhaseId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> PhaseId.<span style="color: #006633;">RENDER_RESPONSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div><h3>faces-config.xml</h3><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;faces-config</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.2&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span style="color: #000066;">xmlns:xi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XInclude&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>login.LoginErrorPhaseListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faces-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><h3>web.xml</h3><p>You must configure your Spring Security Filter Chain to process Servlet FORWARD as well as REQUESTs.</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.filter.DelegatingFilterProxy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dispatcher<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>FORWARD<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dispatcher<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dispatcher<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>REQUEST<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dispatcher<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p
style="text-align: center;">&#8212;-</p><h3>applicationContext-security.xml</h3><p>As for the Spring Security configuration, everything can be left pretty standard. The relevant parts of my configuration, for example:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:beans</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/security&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:beans</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans</span>
<span style="color: #009900;">                         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</span>
<span style="color: #009900;">                        http://www.springframework.org/schema/security</span>
<span style="color: #009900;">                         http://www.springframework.org/schema/security/spring-security-2.0.1.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;global-method-security</span></span>
<span style="color: #009900;">		<span style="color: #000066;">secured-annotations</span>=<span style="color: #ff0000;">&quot;enabled&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/global-method-security<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;http</span></span>
<span style="color: #009900;">		<span style="color: #000066;">auto-config</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">access-denied-page</span>=<span style="color: #ff0000;">&quot;/accessDenied.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intercept-url</span></span>
<span style="color: #009900;">			<span style="color: #000066;">pattern</span>=<span style="color: #ff0000;">&quot;/login*&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">access</span>=<span style="color: #ff0000;">&quot;IS_AUTHENTICATED_ANONYMOUSLY&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intercept-url</span></span>
<span style="color: #009900;">			<span style="color: #000066;">pattern</span>=<span style="color: #ff0000;">&quot;/**&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">access</span>=<span style="color: #ff0000;">&quot;ROLE_USER,ROLE_ADMIN&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-login</span></span>
<span style="color: #009900;">			<span style="color: #000066;">login-processing-url</span>=<span style="color: #ff0000;">&quot;/j_spring_security_check&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">login-page</span>=<span style="color: #ff0000;">&quot;/login&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">default-target-url</span>=<span style="color: #ff0000;">&quot;/&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">authentication-failure-url</span>=<span style="color: #ff0000;">&quot;/login&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logout</span> <span style="color: #000066;">logout-url</span>=<span style="color: #ff0000;">&quot;/logout&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">logout-success-url</span>=<span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/http<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;authentication-provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- Your authentication provider here (example below)--&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/authentication-provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p
style="text-align: center;">&#8212;-</p><p>Notice here that the &#8220;login-processing-url&#8221; is set to &#8220;/j_spring_security_check&#8221;, which is the location where our HttpRequestDispatcher is going to forward to. You can call this whatever you want, but the two must match exactly.</p><h3>login.xhtml / login.jspx / login.jsp</h3><p>(Whatever you use as your JSF page content type, take your pick.)</p><p>So the last part of the puzzle is relatively easy. You need a JSF login page that conforms to Spring Security&#8217;s parameter naming requirements. When this page submits, its values will be forwarded to the Spring Security Filter Chain.</p><p>Notice that you don&#8217;t even need to tie the input field values to a JSF backing bean! The values only need to be intercepted by Spring Security on forward. However, if you want to do all that cool validation and stuff that JSF lets you do&#8230; go for it. I just wanted to save space in the article, and prove a point that it&#8217;s not needed.</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:root</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:jsp</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/JSP/Page&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:f</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/core&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f:view<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:form</span></span>
<span style="color: #009900;">			<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;loginForm&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">prependId</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Username:&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputText</span></span>
<span style="color: #009900;">				<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:inputText<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Password:&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:inputSecret</span></span>
<span style="color: #009900;">				<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:inputSecret<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;_spring_security_remember_me&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:outputText</span></span>
<span style="color: #009900;">				<span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Remember me&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:selectBooleanCheckbox</span></span>
<span style="color: #009900;">				<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;_spring_security_remember_me&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h:commandButton</span></span>
<span style="color: #009900;">				<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;login&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{loginBean.doLogin}&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Login&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h:form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/f:view<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Remember that Spring Security is expecting parameters to be named as they are in this file. j_username, j_password, _spring_security_remember_me. Don&#8217;t change these ids unless you change your Spring configuration.</p><p
style="text-align: center;">&#8212;-</p><h3>If you&#8217;re having problems</h3><p>Add a LoggerListener to allow Spring Security to print messages to your logging output. This will allow you to view any error messages that may be occurring. (Note: This should be copied verbatim)</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;loggerListener&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.event.authentication.LoggerListener&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div><p
style="text-align: center;">&#8212;-</p><h3>Finished</h3><p>And that&#8217;s all it took. A simple forward to a new servlet. No JSF navigation cases, no extra configuration. Just a little J2EE, and a night of no sleep. I hope this helps a LOT of people who seem to be struggling with the task of integrating these two excellent frameworks.</p><p>Considering the forces of this problem, we really required almost no invasiveness in our normal application logic. JSF does its validation and processing without being impacted by Acegi, and Acegi can perform its magic authentication without knowing that JSF was ever the provider of its parameters.</p><p>You can see a <strong>working example</strong> of this guide <a
href="http://ocpsoft.com/java/acegi-spring-security-jsf-integration-project-continued/">here</a>.<br
/> Enjoy.</p><h3>Example Authentication Provider for Testing</h3><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">		Usernames/Passwords are rod/koala dianne/emu scott/wombat peter/opal</span>
<span style="color: #808080; font-style: italic;">	--&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;authentication-provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password-encoder</span></span>
<span style="color: #009900;">			<span style="color: #000066;">hash</span>=<span style="color: #ff0000;">&quot;md5&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user-service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span></span>
<span style="color: #009900;">				<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rod&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;a564de63c2d0da68cf47586ee05984d7&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span></span>
<span style="color: #009900;">				<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dianne&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;65d15fe9156f9c4bbffd98085992a44e&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_USER,ROLE_TELLER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span></span>
<span style="color: #009900;">				<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;scott&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;2b58af6dddbd072ed27ffc86725d7d3a&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span></span>
<span style="color: #009900;">				<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;peter&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;22b5c9accc6e1ba628cedc63a72d57f8&quot;</span></span>
<span style="color: #009900;">				<span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user-service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/authentication-provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/acegi-spring-security-jsf-login-page/feed/</wfw:commentRss> <slash:comments>58</slash:comments> </item> </channel> </rss>
<!-- Served from: ocpsoft.com @ 2010-07-29 17:03:33 by W3 Total Cache -->