1.首先在src下创建interceptor包
2.分别在包下面创建两个类 我这里以 LoginInterceptor和WebConfig为例3
3.在LoginInterceptor中写入如下代码
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录过滤拦截 的配置类
*
* */
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
if (request.getSession().getAttribute("user") == null){
response.sendRedirect("/admin");
}
return true;
}
}