Java Web三大基本组件,我们知道SpringMVC主要就是构建在Servlet的基础上的,接下来我们看一下Filter的使用姿势
在介绍filter的使用之前,有必要知道下什么是fitler。
Filter称为过滤器,主要用来拦截http请求,做一些其他的事情
一个http请求过来之后
通过上面的流程,可以推算使用场景:
在前面Java Config搭建了一个简单的web应用,我们的filter测试也放在这个demo工程上继续
pom配置如下
<properties>
<spring.version>5.1.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>9.2.19.v20160908</version>
</dependency>
</dependencies>
<build>
<finalName>web-mvc</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.12.RC2</version>
<configuration>
<httpConnector>
<port>8080</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
创建一个filter,得首先告诉spring说,我这有个filter,你得把它用起来;使用java config的方式创建应用,干掉xml文件,我们知道Servlet容器会扫描 AbstractDispatcherServletInitializer
的实现类
所以我们的filter声明也放在这里
public class MyWebApplicationInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
// applicationContext.setConfigLocation("com.git.hui.spring");
applicationContext.register(RootConfig.class);
applicationContext.register(WebConfig.class);
System.out.println("-------------------");
return applicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new CharacterEncodingFilter("UTF-8", true), new MyCorsFilter()};
}
}
看上面最后一个方法,返回当前支持的fitler数组,其中 MyCorsFilter
就是我们自定义的fitler
自定义一个filter,需要实现Filter接口,其中有三个方法,主要的是第二个
/**
* Created by @author yihui in 16:13 19/3/18.
*
* 测试:
*
* - 返回头不会包含CORS相关: curl -i 'http://127.0.0.1:8080/welcome?name=一灰灰' -e 'http://hhui.top'
* - 返回头支持CORS: curl -i 'http://127.0.0.1:8080/hello?name=一灰灰' -e 'http://hhui.top'
*/
@Slf4j
public class MyCorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
try {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if ("/hello".equals(request.getRequestURI())) {
response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
}
} finally {
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
上面的 doFilter
方法就是我们重点观察目标,三个参数,注意第三个
filterChain.doFilter(servletRequest, servletResponse)
表示会继续将请求执行下去;若不执行这一句,表示这一次的http请求到此为止了,后面的走不下去了 创建一个rest接口,用来测试下我们的filter是否生效
@RestController
public class HelloRest {
@Autowired
private PrintServer printServer;
@GetMapping(path = {"hello", "welcome"}, produces = "text/html;charset=UTF-8")
public String sayHello(HttpServletRequest request) {
printServer.print();
return "hello, " + request.getParameter("name");
}
@GetMapping({"/", ""})
public String index() {
return UUID.randomUUID().toString();
}
}
启动web之后,观察下访问 hello
与 welcome
的返回头
相关博文
web系列:
mvc应用搭建篇:
一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
打赏 如果觉得我的文章对您有帮助,请随意打赏。