Springboot 使用注解方式 配置Filter

在启动类上 加上注解 

//过滤器开关
@ServletComponentScan

@SpringBootApplication
//开启缓存功能
@EnableCaching
//定时器总开关
@EnableScheduling
//过滤器开关
@ServletComponentScan
public class ShiroApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShiroApplication.class, args);
    }


}

 

@WebFilter(filterName = "LoginFilter",urlPatterns = {"/edit"})
public class LoginFilter implements javax.servlet.Filter {
    public void destroy() {
        System.out.println("过滤器销毁....");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("过滤器执行....");
        HttpServletRequest request =(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if (user==null){
         response.sendRedirect(request.getContextPath()+"/login");

        }
        chain.doFilter(req, resp);

    }

    public void init(FilterConfig config) throws ServletException {
        System.out.println("过滤器初始化");
    }

}

你可能感兴趣的:(Springboot,Filter)