前言:
成都創(chuàng)新互聯(lián)專注于南召網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供南召營銷型網(wǎng)站建設(shè),南召網(wǎng)站制作、南召網(wǎng)頁設(shè)計、南召網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造南召網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供南召網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
最新Servlet 3.0攔截器的使用
1.pom.xml添加需要使用的依賴
4.0.0 top.ytheng springboot-demo 0.0.1 jar org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-devtools true true myspringboot org.springframework.boot spring-boot-maven-plugin maven-compiler-plugin 1.8
2.添加Filter攔截器
package top.ytheng.demo.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //Servlet3.0特性 //urlPatterns:攔截的url地址 //filterName:攔截器名稱 @WebFilter(urlPatterns="/api/*", filterName="loginFilter") public class LoginFilter implements Filter{ /* * 容器加載完成調(diào)用 * */ @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("filter init..."); } /* * 請求被攔截的時候調(diào)用 * */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("doFilter..."); HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response; String username = req.getParameter("username"); if(username.equals("theng")) { chain.doFilter(request, response); } else { //重定向 resp.sendRedirect("/filter.html"); return; } } /* * 容器被銷毀的時候調(diào)用 * */ @Override public void destroy() { // TODO Auto-generated method stub System.out.println("filter destroy..."); } }
3.添加測試控制器
package top.ytheng.demo.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/filter") public class FilterController { @RequestMapping("/test") public Object testFilter() { Mapmap = new HashMap<>(); map.put("name", "theng"); map.put("pwd", "123456"); return map; } }
4.添加啟動類
package top.ytheng.demo; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //等于下面3個 //@SpringBootConfiguration //@EnableAutoConfiguration //@ComponentScan //攔截器用到 @ServletComponentScan public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
5.添加攔截后調(diào)整的頁面filter.html
Insert title here hello theng
filter success
6.右鍵項目Run As啟動項目,測試地址
http://localhost:8080/api/v1/filter/test?username=theng http://localhost:8080/api/v1/filter/test?username=ytheng
另附:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。