1. Spring Boot 中使用 Filter 34
FilterRegistrationBean 用来注册 Filter 对象
Filter是Servlet规范中的过滤器,可以处理请求, 对请求的参数, 属性进行调整。 常常在过滤器中处理字符编码
1.1 在框架中使用过滤器:34
1. 创建自定义过滤器类
2. 注册Filter过滤器对象
自定义过滤器MyFilter
package com.bjpowernode.web;
import javax.servlet.*;
import java.io.IOException;
//自定义过滤器 34
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("执行了MyFilter,doFilter");
filterChain.doFilter(servletRequest,servletResponse);
}
}
注册过滤器WebApplicationConfig
package com.bjpowernode.config;
import com.bjpowernode.web.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//注册过滤器 34
@Configuration
public class WebApplicationConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter( new MyFilter());
bean.addUrlPatterns("/user/*");
return bean;
}
}
控制器CustomFilterController
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//springboot中使用过滤器 34
@Controller
public class CustomFilterController {
@RequestMapping("/user/account")
@ResponseBody
public String userAccount(){
return "user/account";
}
@RequestMapping("/query")
@ResponseBody
public String queryAccount(){
return "/query";
}
}
测试
浏览器访问http://localhost:8080/user/account会执行过滤器
浏览器访问http://localhost:8080/query不会执行过滤器
2. 字符集过滤器的应用 35
CharacterEncodingFilter : 解决post请求中乱码的问题
在SpringMVC框架, 在web.xml 注册过滤器。 配置他的属性。
2.1 第一种方式: 35-37
代码在course7_1中
使用步骤:
2.1.1 配置字符集过滤器
MyServlet
package com.powernode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//定义一个servlet
//字符集过滤器的应用 36
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");//这里我们不指定编码方式,用过滤器指定
PrintWriter out = resp.getWriter();
out.println("===在Servlet输出中文,默认编码ISO-8859-1===");
out.flush();
out.close();
}
}
WebSystemConfig
package com.powernode.config;
import com.powernode.web.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import sun.misc.CharacterEncoder;
//注册servlet 35-36
@Configuration
public class WebSystemConfig {
//注册Servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet = new MyServlet();
ServletRegistrationBean reg = new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
//注册Filter设置字符编码方式 37
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean reg = new FilterRegistrationBean();
//使用框架中的过滤器类
CharacterEncodingFilter filter = new CharacterEncodingFilter();
//指定使用的编码方式
filter.setEncoding("utf-8");
//指定请求request ,应答 response都使用encoding的值
filter.setForceEncoding(true);
reg.setFilter(filter);
//指定 过滤的url地址
reg.addUrlPatterns("/*");
return reg;
}
}
重点
在 application.properties , 禁用 Spring Boot 中默认启用的过滤器
修改application.properties文件, 让自定义的过滤器起作用
application.properties
#springboot中默认已经设置了CharacterEncodingFilter这个过滤器,他的编码方式为 37
# ISO-8859-1,我们设置为false就是代表关闭了这个,使用我们配置的编码方式
server.servlet.encoding.enabled=false
2.2 第二种方式 39
代码在course7_2中
修改application.properties文件
Spring Boot 项目默认启用了 CharacterEncodingFilter, 设置他的属性就可以
server.port=9001
server.servlet.context-path=/myboot
#让系统的CharacterEncodingFilter生效 39
server.servlet.encoding.enabled=true
#指定编码方式
server.servlet.encoding.charset=UTF-8
#强制request,response都使用charset的属性值
server.servlet.encoding.force=true
MyServlet
package com.bjpowernode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//定义一个servlet
//字符集过滤器的应用 39
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");//这里我们不指定编码方式,用过滤器指定
PrintWriter out = resp.getWriter();
out.println("****在Servlet输出中文,默认编码ISO-8859-1*****");
out.flush();
out.close();
}
}
WebSystemConfig
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
//注册servlet 39
@Configuration
public class WebSystemConfig {
//注册Servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet = new MyServlet();
ServletRegistrationBean reg =
new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
}