如何使JSP登录页面上的会话无效

时间:2019-03-28 07:47:54

标签: java jsp session jsessionid

我有一个准系统登录JSP页面,并且在我第一次启动该应用程序(访问http://localhost:8080/)之后,立即创建了一个会话,我认为这是该应用程序中的一个重大安全漏洞。 xml代码,我将我设置为login.jsp,在我的loginServlet中,是在将您的凭据发布到login.jsp中并且一切都签出之后,是何时创建会话,说实话,我不明白为什么要使用该会话正在创建中,请您澄清一下。

如果会话为空,我正在使用servlet过滤器阻止访问,这就是我意识到加载登录页面后创建了一个会话的方式

<welcome-file-list>
   <welcome-file>login.jsp</welcome-file>
</welcome-file-list>

//from loginServlet, will only execute after posting from login page
            System.out.println("login successful");
            if (context != null){
                //get the old session and invalidate
                HttpSession oldSession = request.getSession(false);
                if (oldSession != null) {
                    System.out.println("Session is not null");
                    oldSession.invalidate();
                }

                //generate a new session & set to expiry in 5 mins
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(5*60);

2 个答案:

答案 0 :(得分:0)

在JSP页面中会自动创建会话,此stackoverflow thread

中也问了同样的问题
  

在JSP中,一旦您按下该JSP,就会创建一个会话(显然,除非它已经存在)。

     

除非您使用指令       <%@页面会话=“ false”%>

还有这个post解释了您正在寻找的一些东西

答案 1 :(得分:-1)

<%session.invalidate();%>
<% HttpSession nsession = request.getSession(false);
if(nsession!=null) {
   String data=(String)session.getAttribute( "fname" );
   out.println(data);
}
else
  out.println("Session is not active");
%>
Output:
while opening display page, all the attributes are getting displayed on client (browser).
Since we have already called invalidate in the first line of errorpage.jsp, it is displaying the message “Session is not active” on the screen

Points to Note:
1) This will deactivate the session

<%session.invalidate();%>
2) This logic will exceute the if body when the session is active else it would run the else part.

<% HttpSession nsession = request.getSession(false);
if(nsession!=null)
   ...
else
   ...
%>