SpringBoot入门二十六,配置session超时拦截器

2021-04-21 18:26

阅读:665

标签:code   配置文件   gis   send   get   Stub   ann   pos   util   

项目基本配置参考文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可,此示例使用Eclipse,springboot升级为2.2.8版本。

1. 设置session超时时间

这里采用的是Springboot2的版本,配置方式和1不同,在配置文件application.properties文件中添加以下内容:

## 设置session过期时间(秒)
server.servlet.session.timeout=1800s

2. 自定义一个session拦截器

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class SessionInterceptor implements HandlerInterceptor {
    /**
     * 进入controller方法之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        if (null == session.getAttribute("username")) {
            // 重定向到指定页面
            response.sendRedirect(request.getContextPath() + "/error/401.html");
            return false;
        } 
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * 调用完controller之后,视图渲染之前(不使用可删除此方法)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 整个完成之后,通常用于资源清理(不使用可删除此方法)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

3. 自定义一个拦截器配置

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.jnxj.modules.common.interceptor.SessionInterceptor;

/**
 * 
描述:拦截器配置
* */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 定义好的拦截器 */ @Autowired private SessionInterceptor sessionInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(sessionInterceptor) // 配置定义好的拦截器 .addPathPatterns("/**") // 拦截所有请求 .excludePathPatterns(getExcludePathPatterns()); // 忽略拦截的请求 } // =================== private method ================== /** *
功能:定义需要忽略拦截的请求
* * @return */ private List getExcludePathPatterns(){ List excludePathPatternsList = new ArrayList(); excludePathPatternsList.add("/"); excludePathPatternsList.add("/login"); excludePathPatternsList.add("/css/**"); excludePathPatternsList.add("/error/**"); excludePathPatternsList.add("/img/**"); excludePathPatternsList.add("/js/**"); return excludePathPatternsList; } }

启动项目,测试即可

SpringBoot入门二十六,配置session超时拦截器

标签:code   配置文件   gis   send   get   Stub   ann   pos   util   

原文地址:https://blog.51cto.com/1197822/2509907


评论


亲,登录后才可以留言!