2 Replies Latest reply on Aug 22, 2008 1:09 PM by Javier Carazo

    Force redirect after login

    Javier Carazo Newbie

      Hi,


      I´m working with JBOSS 4.2.2 GA and Seam 2.0.2 GA.


      I would like to know if when a user, which is not logged into the application, request a protected page, it is possible to redirect him to a specific page (home i.e) after his log in and not to the requested page.


      I have tried it with no success.


      This is my pages.xml:



      <?xml version="1.0" encoding="UTF-8"?>
      <pages xmlns="http://jboss.com/products/seam/pages"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd"
      
             no-conversation-view-id="/login.xhtml"
            login-view-id="/login.xhtml">
      
          <page view-id="*">
              <navigation from-action="#{identity.logout}">
                  <redirect view-id="/login.xhtml"/>
              </navigation>  
          </page>     
          
          <page view-id="/login.xhtml">
              <navigation from-action="#{identity.login}">
                    <rule if="#{identity.loggedIn and s:hasRole('ADMIN')}">
                           <redirect view-id="/adminpanel.xhtml"/>
                   </rule>
                    <rule if="#{identity.loggedIn and s:hasRole('SUPERADMIN')}">
                           <redirect view-id="/sadminpanel.xhtml"/>
                   </rule>
              </navigation>
          </page> 
      
          <page view-id="/products.xhtml" login-required="true">
                   
          </page>



      Now if user copy produts.xhtml URL and he tries to access, login.xhtml page appears. After this, user gets prodcuts.xhtml page, I think this is the correct way to work, but I would like to redirect him to adminpanel or sadminpanel.
      Redirection for roles is working if the user access to the application trough the login.xhtml page.


      Any idea?


      Thanks in advance.

        • 1. Re: Force redirect after login
          Samuel Mendenhall Apprentice

          One way to do this would be to extend/override the built in Seam component:  org.jboss.seam.faces.Redirect  which is responsible for:



          <event type="org.jboss.seam.security.notLoggedIn">
                 <action execute="#{redirect.captureCurrentView}"/>
             </event>
             <event type="org.jboss.seam.security.loginSuccessful">
                 <action execute="#{redirect.returnToCapturedView}"/>
             </event>




          And specifically override and change the logic in:


          public void execute()
             {
                FacesManager.instance().redirect(viewId, parameters, conversationPropagationEnabled);
             }



          To check the roles and return to the appropriate viewId, that is if you want to retain the return to captured view functionality.

          • 2. Re: Force redirect after login
            Javier Carazo Newbie

            Thanks a lot, that´s exactly what I was looking for!!!!