Filter
一、过滤器Filter
(一)概述
概念
过滤器是一个服务器端的组件,它可以截取客户端的请求和服务端的响应信息,并对这些信息进行过滤。
原理:先过滤,后放行。
过滤器会执行两次过滤,第一次是请求时的过滤,第二次是响应时的过滤
3.应用场景
(1)统一处理中文编码问题
(2)统一处理登录控制功能
(二)如何创建过滤器
方式一:使用web.xml注册过滤器
- 创建一个普通类实现Filter接口
- 重写Filter接口中的三个方法
- 在web.xml文件中注册过滤器
package com.offcn.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 1.创建Java类,实现接口javax.servlet.Filter
* 2.重写3个抽象方法
* 3.在web.xml配置过滤规则 或者使用注解配置 @WebFilter
*/
public class Demo1Filter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//过滤到指定请求后,需要做哪些处理, 就在这个方法上完成
System.out.println("过滤到了");
//放行
// filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>demo1Filter</filter-name>
<filter-class>com.offcn.filter.Demo1Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>demo1Filter</filter-name>
<url-pattern>/demo1</url-pattern>
</filter-mapping>
</web-app>
方式二:使用注解方式注册过滤器
- 创建一个普通类继承Filter接口
- 重写Filter接口中的三个方法
- 使用注解注册过滤器
package com.offcn.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 1.创建Java类,实现接口javax.servlet.Filter
* 2.重写3个抽象方法
* 3.在web.xml配置过滤规则 或者使用注解配置 @WebFilter
*/
@WebFilter("/demo1")
public class Demo1Filter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//过滤到指定请求后,需要做哪些处理, 就在这个方法上完成
System.out.println("过滤到了");
//放行
// filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
字符集过滤器
package com.offcn.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//解决请求中文乱码
servletRequest.setCharacterEncoding("utf-8");
//解决响应中文乱码
servletResponse.setContentType("text/html;charset=utf-8");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
跨域过滤器
package com.offcn.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest) servletRequest;
// 不使用*,自动适配跨域域名,避免携带Cookie时失效
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", origin);
// 自适应所有自定义头
String headers = request.getHeader("Access-Control-Request-Headers");
response.setHeader("Access-Control-Allow-Headers", headers);
response.setHeader("Access-Control-Expose-Headers", headers);
// 允许跨域的请求方法类型
response.setHeader("Access-Control-Allow-Methods", "*");
// 预检命令(OPTIONS)缓存时间,单位:秒
response.setHeader("Access-Control-Max-Age", "3600");
// 明确许可客户端发送Cookie,不允许删除字段即可
response.setHeader("Access-Control-Allow-Credentials", "true");
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}