标签:vcc mvc dem cep handle ret package oid pack
利用拦截器实现
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;
}
}
标签:vcc mvc dem cep handle ret package oid pack
原文地址:https://www.cnblogs.com/xQlover/p/13529540.html