SpringBoot拦截器Filter的使用方法详解
前言:
最新Servlet3.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 1.8
2.添加Filter拦截器
packagetop.ytheng.demo.filter;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.annotation.WebFilter;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
//Servlet3.0特性
//urlPatterns:拦截的url地址
//filterName:拦截器名称
@WebFilter(urlPatterns="/api/*",filterName="loginFilter")
publicclassLoginFilterimplementsFilter{
/*
*容器加载完成调用
**/
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
//TODOAuto-generatedmethodstub
System.out.println("filterinit...");
}
/*
*请求被拦截的时候调用
**/
@Override
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)
throwsIOException,ServletException{
//TODOAuto-generatedmethodstub
System.out.println("doFilter...");
HttpServletRequestreq=(HttpServletRequest)request;
HttpServletResponseresp=(HttpServletResponse)response;
Stringusername=req.getParameter("username");
if(username.equals("theng")){
chain.doFilter(request,response);
}else{
//重定向
resp.sendRedirect("/filter.html");
return;
}
}
/*
*容器被销毁的时候调用
**/
@Override
publicvoiddestroy(){
//TODOAuto-generatedmethodstub
System.out.println("filterdestroy...");
}
}
3.添加测试控制器
packagetop.ytheng.demo.controller;
importjava.util.HashMap;
importjava.util.Map;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/filter")
publicclassFilterController{
@RequestMapping("/test")
publicObjecttestFilter(){
Mapmap=newHashMap<>();
map.put("name","theng");
map.put("pwd","123456");
returnmap;
}
}
4.添加启动类
packagetop.ytheng.demo;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication//等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//拦截器用到
@ServletComponentScan
publicclassDemoApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(DemoApplication.class,args);
}
}
5.添加拦截后调整的页面filter.html
Inserttitlehere hellotheng
filtersuccess
6.右键项目RunAs启动项目,测试地址
http://localhost:8080/api/v1/filter/test?username=theng http://localhost:8080/api/v1/filter/test?username=ytheng
另附:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。