我知道这个问题已经讨论了很多次,但是,我没有设法直截了当。如果你看一下,我真的很感激。
所以,我有以下问题:
如果用户想通过链接访问我的应用程序,例如
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();
}
}
好的,现在这就是我设置faces-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;
}
}
答案 0 :(得分:1)
将您的URL保存在阶段监听器中的LOGIN_REDIRECT等会话属性中,并在登录后使用它。
一些像spring security这样的框架会为你做这件事。