[Web development] session management in spring

Source: Internet
Author: User

Assume that the session becomes invalid after the user logs on to the system for a period of time. If the user logs on to the system, the user must jump to the logon page and log on again.

General Solution:

(1) When a user logs on successfully for the first time, the user object is stored in the session, for example, session. setatti.pdf ("loginuser", user );

(2) define the Interceptor to intercept all URL requests and then obtain the loginuser object. If the object does not exist, the session expires:

import org.springframework.stereotype.Repository;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;@Repositorypublic class LoginInterceptor extends HandlerInterceptorAdapter {@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Object obj = request.getSession().getAttribute("loginUser");if(obj==null){request.getRequestDispatcher("/login.jsp").forward(request, response); return false;}else{return super.preHandle(request, response, handler);}}}

If the session becomes invalid, the system will jump to login. jsp and request to log on again.

(3) configure the Interceptor:

<mvc:interceptors><mvc:interceptor><mvc:mapping path="/*.html"/><bean class="com.xx.xxx.util.LoginInterceptor" /></mvc:interceptor></mvc:interceptors>

In this way, when a request ends with *. html, it is first intercepted by the logininterceptor.

(Study notes)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.