简体   繁体   English

在jsf中,会话超时后将web.xml配置重定向到登录页面

[英]web.xml configuration to redirect to login page after session timeout in jsf

Is there any configuration in web.xml which redirects the application to login page after sesion expired in JSF .I google it but didn't find any good and simple answer. web.xml中是否有任何配置,可在JSF中 Sesion过期后将应用程序重定向到登录页面。
I found this but it didn't work. 我找到了,但是没有用。

 <error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/index.xhtml</location>
</error-page>

If there is no configuration for this then how can i do this in JSF? 如果没有为此配置,那么我该如何在JSF中做到这一点?
Thanks 谢谢

Possible answer: Handling ViewExpiredException depending on the current view . 可能的答案: 根据当前view处理ViewExpiredException

What I'm using is a PrimeFaces - Extensions specific solution called AjaxErrorHandler . 我正在使用的是称为AjaxErrorHandler的PrimeFaces-Extensions特定解决方案。 Take a look at their showcase for implementation tips. 看看他们的展示以获取实施技巧。

As far as I know the above configuration will not work for AJAX requests. 据我所知,以上配置不适用于AJAX请求。

Another possible answer: How to redirect to index page if session time out happend in jsf application 另一个可能的答案: 如果在jsf应用程序中发生会话超时,如何重定向到索引页

Yet another possible answer: Use a JSF PhaseListener . 另一个可能的答案:使用JSF PhaseListener


Additionaly, I suggest using a Filter to check if your session is already alive and, if not, redirect to your custom error page (i don't remember where I learnt this method, probably through BalusC): 另外,我建议使用Filter来检查您的会话是否已经存在,如果不是,请重定向到您的自定义错误页面(我不记得我从哪里学到了这种方法,可能是通过BalusC来学习的):

public class AuthenticationFilter implements Filter {
    private FilterConfig config;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if(((HttpServletRequest) request).getSession().getAttribute("some_attribute_you_store_in_Session") == null){
            ((HttpServletResponse)response).sendRedirect("yourCustomJSF.jsf");
        }else{
            chain.doFilter(request, response);
        }
    }

    public void destroy() {
        this.config = null;
    }

    // You also have to implement init() and destroy() methods.
}

Then, you have to declare this filter (and the url patterns that will trigger the filter) in your web.xml: 然后,您必须在web.xml中声明此过滤器(以及将触发该过滤器的url模式):

<filter>
    <filter-name>AuthenticationFilter</filter-name>
    <filter-class>yourPackage.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>private.jsf</url-pattern>
</filter-mapping>

I store my own bean in my JSF session so I can keep my user information. 我将自己的bean存储在JSF会话中,以便保留用户信息。 When the filter receives a null return while accessing the class, I know the session has been invalidated (maybe because it has expred, or just the user has logged out) and i redirect the request to my error page. 当过滤器在访问类时收到null返回值时,我知道会话已失效(可能是因为它已过期,或者只是用户已注销),并且我将请求重定向到了我的错误页面。

I found this but it didn't work. 我找到了,但是没有用。

It should work. 它应该工作。 Most likely you were exclusively using ajax requests. 您很可能只使用了ajax请求。 This way the standard web.xml error page mechanism won't be used at all. 这样,完全不会使用标准的web.xml错误页面机制。 You should instead be using the JSF ExceptionHandler facility. 您应该改用JSF ExceptionHandler工具。

The JSF utility library OmniFaces has a FullAjaxExceptionHandler which is designed for exactly this purpose. JSF实用程序库OmniFaces具有完全用于此目的的FullAjaxExceptionHandler It reuses standard web.xml error page mechanisms for exceptions/errors during ajax requests. 它在ajax请求期间将标准的web.xml错误页面机制重用于异常/错误。 You can find a showcase demo here . 您可以在此处找到展示演示。 It also handles the ViewExpiredException . 它还处理ViewExpiredException

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM