2013-05-02 164 views
0

我知道這個問題已經討論了很多次,但是,我沒有設法弄清楚。我真的很感激,如果你會看看這個。登錄後請求頁面

所以,我有以下問題:

如果用戶想要通過一個鏈接到我的存取權限的應用程序如

http://example:8080/Kundenportal/protected/post/post.jsf 

,他將被重定向到我的loginPage(我有一個PhaseListener在其驗證如果登錄或沒有)。現在

,登錄後,用戶希望得到

http://example:8080/Kundenportal/protected/post/post.jsf, 

相反,他將被重定向到

http://example:8080/Kundenportal/protected/post/start.jsf. 

我怎樣才能知道我的應用程序,其中用戶應該後重定向登錄?我知道它通過HttpRequestServlet工作,但是在檢查Phaselistener中是否登錄或登錄之後,用戶將被重定向到我的登錄頁面,這意味着requestURL是loginPage,這是錯誤的。

這是我的代碼。

這是我的PhaseListener:

@Override 
public void afterPhase(PhaseEvent event) { 
    FacesContext fc = event.getFacesContext(); 
    String currentPage = fc.getViewRoot().getViewId(); 

    HttpServletRequest origRequest = (HttpServletRequest) fc 
      .getExternalContext().getRequest(); 
    // Here is the requestURI post.xhtml 
    String reqURI = origRequest.getRequestURI(); 

    // boolean loginPage = page.startsWith("/login.xhtml"); 
    // boolean registerPage = page.startsWith("/registrierung.xhtml"); 

    boolean loginRequired = currentPage.startsWith("/protected/"); 
    if (loginRequired && !loggedIn(fc)) { 
     NavigationHandler nh = fc.getApplication().getNavigationHandler(); 
     nh.handleNavigation(fc, null, "loginFAILED"); 
     fc.renderResponse(); 

    } 

} 

好了,現在我這是怎麼設置的面孔,confing.xhtml:

at.ooev.kp.controller.login.LoggedInCheck

<application> 
    <locale-config> 
     <default-locale>de</default-locale> 
     <supported-locale>de</supported-locale> 
    </locale-config> 
    <message-bundle>at.ooev.kp.messages</message-bundle> 
    <resource-bundle> 
     <base-name>at.ooev.kp.messages</base-name> 
     <var>messages</var> 
    </resource-bundle> 
</application> 
<navigation-rule> 
    <from-view-id>*</from-view-id> 
    <navigation-case> 
     <from-outcome>loginFAILED</from-outcome> 
     <to-view-id>/login.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>doLogin</from-outcome> 
     <to-view-id>/login.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>EXPIRED</from-outcome> 
     <to-view-id>/expired.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

<navigation-rule> 
    <from-view-id>*</from-view-id> 
    <navigation-case> 
     <from-outcome>PAGE_0_</from-outcome> 
     <to-view-id>/protected/start.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_1_1</from-outcome> 
     <to-view-id>/protected/pol/polizzen.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_1_2</from-outcome> 
     <to-view-id>/protected/sch/schaden.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_1_3</from-outcome> 
     <to-view-id>/protected/tk/topkundeninfo.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_2_</from-outcome> 
     <to-view-id>/protected/daten/daten.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_2_1</from-outcome> 
     <to-view-id>/protected/daten/kundendaten.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_2_2</from-outcome> 
     <to-view-id>/protected/daten/benutzer.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_3_1</from-outcome> 
     <to-view-id>/protected/post/post.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_4_1</from-outcome> 
     <to-view-id>/protected/komm/vereinbarung.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>PAGE_5_1</from-outcome> 
     <to-view-id>/protected/trans/transport.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

<navigation-rule> 
    <from-view-id>/login.xhtml</from-view-id> 
    <navigation-case> 
     <from-outcome>loginOK</from-outcome> 
     <to-view-id>/protected/start.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>register</from-outcome> 
     <to-view-id>/registrierung.xhtml</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>loginPOST</from-outcome> 
     <to-view-id>/protected/post/post.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 

這是我managedBean蒙山登錄:

@PermitAll 
public String login() { 
    FacesContext fc = FacesContext .getCurrentInstance(); 
    HttpServletRequest origRequest = (HttpServletRequest) fc.getExternalContext().getRequest(); 
    // The requestURI is here login.xhtml(because there was the redirection in the Phaselistener) 
    String str = origRequest.getRequestURI(); 

    if (doLogin(credentials.getUsername(), credentials.getPassword())) { 
     String realname = (!Utils.isEmpty(webbaUser.getVorname())) ? webbaUser 
       .getVorname() + " " 
       : ""; 
     realname += webbaUser.getName(); 
     user.setUsername(credentials.getUsername()); 
     user.setRealname(realname); 
     user.setKndnr(credentials.getUsername()); 
     if ("kunde".equalsIgnoreCase(credentials.getUsername())) 
      user.setKndnr("600828"); 
     user.setMailadresse(webbaUser.getEmail()); 
     user.setHostUserid(webbaUser.getHostuserid()); 

     HttpSession session = ((HttpServletRequest) FacesContext 
       .getCurrentInstance().getExternalContext().getRequest()) 
       .getSession(false); 
     session.setAttribute("LOGGEDINUSER", user.getUsername()); 
     dbLog.add(DbLogController.Aktion.LOGIN, user.getUsername()); 
     // List<String> kundennummern = webbaUser.getDatfilt(); 
     // for (String s : kundennummern) 
     // System.out.println("datfilt: " + s); 
     return "loginOK"; 
    } else { 
     FacesContext.getCurrentInstance().addMessage(null, 
       new FacesMessage("Anmeldung fehlgeschlagen!")); 
     dbLog.add(DbLogController.Aktion.LOGINFAILED, 
       credentials.getUsername()); 
     user = null; 
     return null; 
    } 
} 

回答

1

將您的URL保存在階段偵聽器中的會話屬性(如LOGIN_REDIRECT)中,並在登錄後使用它。

一些像spring security這樣的框架可以爲你做到這一點。

+1

謝謝,你是我的英雄! – leostiw 2013-05-02 14:47:51