2014-06-10 38 views
-1

我想配置会话超时到我的应用程序。会话超时工作不同,因为我希望

在web.xml中我配置我分钟,像这样:

<session-config> 
     <session-timeout>1</session-timeout> 
    </session-config> 

若要确保一切工作我创建我的听众,请参见:

<listener> 
    <listener-class>br.com.sender.util.SessionListener</listener-class> 
</listener> 

见我的监听器类:

package br.com.sender.util; 

import java.util.Date; 

import javax.servlet.http.HttpSessionEvent; 
import javax.servlet.http.HttpSessionListener; 

public class SessionListener implements HttpSessionListener { 
    public void sessionCreated(HttpSessionEvent event) { 
     System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date()); 
    } 

    public void sessionDestroyed(HttpSessionEvent event) { 
     System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date()); 
    } 
} 

当我在我的应用程序登录会话创建(在这同一时刻我存储UserMB与sessionScope),请参阅:

Session ID 801040FAFEBC8D21C3C8E0DA56BF9B27 created at Tue Jun 10 18:44:33 BRT 2014 

有些分钟后没有动作会被破坏:

Session ID 801040FAFEBC8D21C3C8E0DA56BF9B27 destroyed at Tue Jun 10 18:46:26 BRT 2014 

当某些菜单或按钮会话用户点击重新创建的,但我希望UserMB( SessionScope)被破坏,用户必须重新登录,但不会发生,用户继续使用应用程序而不重新登录。

+0

你是如何检查用户登录?按会话!= null或通过session.getAttrribute(“someattribute”)!= null?第一个是错误的。 – developerwjk

+0

我有一个UserMBLogged与SessionScope,当我需要检查用户是否登录我称之为ManagedBean。我有一个“过滤器”来检查用户是否登录。 –

+0

您称这个托管bean做什么? – EJP

回答

1

你需要一个WebFilter,在会话范围检查一些属性,并确定用户登录或不

-1

你可能有一些servlet或过滤写在你的应用程序,它向前拦截所有的请求,然后它因此在那个servlet /过滤器中,你必须检查session.getAttribute(“someinfo”)!= null,如果它为null,那么请将请求转发到登录页面。

0

这一切都取决于您如何配置您未在问题中发布的security

如果不验证登录通过java ee web.xml安全设置的用户,或者通过一个filter比你的页面检查是不安全的,并且当用户将达到下一个URL的session将被重新创建。

如果不使用集装箱安全或Java EE的安全性,那么你可以有一些类型的过滤器,检查所有网页除了登录

@WebFilter("/*") 
public class LoginFilter implements Filter { 

    private ServletContext context; 

    public void init(FilterConfig fConfig) throws ServletException { 
     this.context = fConfig.getServletContext(); 
    } 

    public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain chain) throws IOException, ServletException { 

     String requestURL = ((HttpServletRequest)request).getRequestURL().toString(); 
     HttpSession session = req.getSession(false); 

     if(session == null && !(requestURL.endsWith("login.xhtml"))){ 
      ((HttpServletResponse)response).sendRedirect("login.xhtml"); 
     }else{ 
      chain.doFilter(request, response); 
     } 
    } 
}