码迷,mamicode.com
首页 > 其他好文 > 详细

简单session实现

时间:2020-09-02 16:45:40      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:vcc   mvc   dem   cep   handle   ret   package   oid   pack   

简单的session校验实现

自己写着玩的一个小demo, 暂存一下

利用拦截器实现

package com.ryh.blog.intecepter;

import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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

/**
 * @author RYH
 * @description 拦截器用来检查session
 * @date 2020/7/7 0007
 **/
@Component
public class SessionHandler implements HandlerInterceptor, WebMvcConfigurer, Ordered {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session == null) {
            String path = request.getContextPath();
            response.sendRedirect(path + "/login");
        }
        return true;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SessionHandler())
                .addPathPatterns("/*")
                .excludePathPatterns("/login", "/doLogin");
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

简单session实现

标签:vcc   mvc   dem   cep   handle   ret   package   oid   pack   

原文地址:https://www.cnblogs.com/xQlover/p/13529540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!