目的:springboot,实现简单的filter过滤器
环境搭建 : https://blog.csdn.net/wuyinlei/article/details/79227962
项目整体目录结构:
filter 方法
package com.xdx97.backstage.filter;
//判断是否登陆的 filter
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
// /* 表示全部拦截
@WebFilter(filterName = "loginFilter",urlPatterns = "/*")
public class LoginFilter implements Filter {
//初始化调用的方法
//当服务器 被启动的时候,调用
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("哎呀,我被生出来了!!!");
}
//拦截的方法
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("哈哈 被拦截了吧!!!");
filterChain.doFilter(servletRequest,servletResponse);
}
//销毁时候调用的方法
public void destroy() {
System.out.println("哎呀,我要被销毁了!!!");
}
}
controller 方法
package com.xdx97.backstage.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BlogController {
@RequestMapping("/aaa")
public String aaa() {
return "aaa";
}
}
启动方法 App.java
package com.xdx97.backstage;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@MapperScan(basePackages = { "com.xdx97.backstage.dao" })
@ServletComponentScan(basePackages = {"com.xdx97.backstage.filter"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
如果还想要,排除某个 url