誰かがURLを書き込んでログインページにジャンプしようとした場合、それを確認してログインページにリダイレクトするにはどうすればよいですか?
自家製の認証を使用しているようです。その場合、サーブレットフィルタを実装する必要があります。JSFは、セッションスコープのマネージドBeanをの属性として格納するため、メソッドHttpSession
で確認できます。doFilter()
HttpServletRequest req = (HttpServletRequest) request;
UserManager userManager = (UserManager) req.getSession().getAttribute("userManager");
if (userManager != null && userManager.isLoggedIn()) {
chain.doFilter(request, response);
} else {
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/login.xhtml");
}
このフィルターを、保護されたページをカバーするURLパターンにマップします/app/*
。
戻ってフォームを操作しようとすると、セッションの有効期限を警告するメッセージが送信されます。これが発生した場合、どうすればログインフォームに再度リダイレクトできますか?
これがAjaxリクエストに関係していることを理解していますか?<error-page>
通常のリクエストでは、 inを使用できますweb.xml
。web.xml
次のようにクライアントに状態保存方法を設定する場合
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
オプションではない場合は、カスタムを実装する必要がありますExceptionHandler
:
public class ViewExpiredExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public ViewExpiredExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public void handle() throws FacesException {
FacesContext facesContext = FacesContext.getCurrentInstance();
for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
Throwable exception = iter.next().getContext().getException();
if (exception instanceof ViewExpiredException) {
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, "viewexpired");
facesContext.renderResponse();
iter.remove();
}
}
getWrapped().handle();
}
@Override
public ExceptionHandler getWrapped() {
return wrapped;
}
}
(この特定の例はに移動するviewexpired
ため、/viewexpired.xhtml
エラーページとして表示されることに注意してください)
上記は、次のExceptionHandlerFactory
実装によってベイク処理する必要があります。
public class ViewExpiredExceptionHandlerFactory extends ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public ViewExpiredExceptionHandlerFactory(ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
return new ViewExpiredExceptionHandler(parent.getExceptionHandler());
}
}
faces-config.xml
次に、次のように登録する必要があります。
<factory>
<exception-handler-factory>com.example.ViewExpiredExceptionHandlerFactory</exception-handler-factory>
</factory>