SpringBoot 03: 常用web組件 – – – 攔截器 + Servlet + 過濾器
- 2022 年 11 月 12 日
- 筆記
- springboot
常用web組件
使用思想
- 創建自定義類
- 實現或者繼承框架里的接口或類
- 將自定義類註冊到框架中
- 使用自定義類
攔截器
說明
- 攔截器是SpringMVC中的一種對象,能攔截對Controller的請求
- 攔截器框架中有系統的攔截器,還可以自定義攔截器,實現對請求的預先處理
自定義攔截器
- 創建自定義的攔截器類實現SpringMVC框架的HandlerInterceptor接口,根據對攔截時機的需求,重寫接口裡的對應方法
package com.example.web.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//實際應用中被攔截的請求在這裡執行其他邏輯判斷,是否放行由判斷結果來定
System.out.println("LoginInterceptor攔截器的preHandle方法執行");
return true;
}
}
package com.example.web.config;
import com.example.web.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//創建一個攔截器對象
LoginInterceptor loginInterceptor = new LoginInterceptor();
//需要攔截的url
String[] interceptorUrl = {"/user/**"};
//需要放行的url
String[] excludeUrl = {"/user/login"};
registry.addInterceptor(loginInterceptor)
.addPathPatterns(interceptorUrl)
.excludePathPatterns(excludeUrl);
}
}
package com.example.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class InterceptorTest {
@RequestMapping("/user/account")
@ResponseBody
public String account(){
return "訪問賬戶頁面";
}
@RequestMapping("/user/login")
@ResponseBody
public String login(){
return "訪問用戶登錄頁面";
}
}
Servlet
package com.example.web.servlet;
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;
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;charset=utf-8");
//獲取輸出流對象
PrintWriter out = resp.getWriter();
//向瀏覽器輸出數據
out.println("--- springboot的servlet被調用 ---");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
//調用方法,註冊servlet
// @Bean
// public ServletRegistrationBean servletRegistrationBean(){
// return new ServletRegistrationBean(new MyServlet(), "/servlet.do");
// }
//註冊servlet的方式2
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/servlet1.do", "/servlet2.do");
return servletRegistrationBean;
}
}
過濾器
說明
- Filter是Servlet規範中的過濾器,可以處理請求,對請求的參數,屬性進行調整。常常在過濾器中處理字符編碼
使用自定義過濾器
package com.example.web.filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletResponse.setContentType("text/html;charset=utf-8");
System.out.println("MyFilter的doFilter方法被調用");
filterChain.doFilter(servletRequest, servletResponse);
}
}
package com.example.web.config;
import com.example.web.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyFilterConfig {
//定義方法,註冊過濾器
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.addUrlPatterns("/user/*");
return filterRegistrationBean;
}
}
使用自定義字符集過濾器
package com.example.web.servlet;
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;
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("使用springboot字符過濾器");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.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;
@Configuration
public class MyWebConfig {
//註冊servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/test");
return servletRegistrationBean;
}
//註冊內置的字符過濾器
@Bean
public FilterRegistrationBean filterRegistrationBean(){
//內置的字符過濾器
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
//按需求定製字符過濾器
characterEncodingFilter.setEncoding("utf-8");
characterEncodingFilter.setForceEncoding(true);
//註冊按照個性化需求配置後的字符過濾器
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(characterEncodingFilter);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
- 修改application.properties文件,讓自定義的過濾器起作用
#關閉springboot框架默認的字符編碼格式:ISO-8859-1
server.servlet.encoding.enabled=false
使用框架內置的字符集過濾器
package com.example.web.servlet;
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;
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("**** 使用springboot字符過濾器 ****");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
//註冊servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/test");
return servletRegistrationBean;
}
}
- 修改application.properties文件,讓框架內置的字符集過濾器實現個性化定製
#讓springboot默認的字符過濾器生效
server.servlet.encoding.enabled=true
#將默認的字符過濾器的編碼格式設置為utf-8
server.servlet.encoding.charset=utf-8
#將請求和響應的編碼格式都按照字符過濾器中的編碼格式來進行解析
server.servlet.encoding.force=true