springboot 过滤器

package com.nachuang.zhixiao.union.util;

import com.nachuang.zhixiao.model.HuiYuan;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Order(1)
@WebFilter(filterName = "MSecurity",urlPatterns = {"*.html"})
public class MSecurityFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response= (HttpServletResponse) servletResponse;
System.out.println(request.getRequestURI());
//检查是否是登录页面
if(request.getRequestURI().equals("/Login.html"))
filterChain.doFilter(servletRequest,servletResponse);

//检测用户是否登录
HttpSession session =request.getSession();
HuiYuan huiYuan= (HuiYuan) session.getAttribute("user");
if(huiYuan==null )
{
try{ response.sendRedirect("/Login.html");}catch (Exception e){}
}
filterChain.doFilter(servletRequest,servletResponse);
}

@Override
public void destroy() {

}
}

posted on 2018-06-25 16:02  superficial。  阅读(142)  评论(0编辑  收藏  举报

导航