首页 > 编程语言 >springboot javax.servlet.Filter使用

springboot javax.servlet.Filter使用

时间:2022-11-03 10:32:25浏览次数:46  
标签:springboot void request response Filter Override servlet public 请求


请求拦截器优点:

1、拦截非法请求重定向
2、验证用户token

下面是demo程序,有问题的可以在评论区留言

@WebFilter(filterName = "authenticationFilter", urlPatterns = {"/user/*","/detail/*"})
public class AuthenticationFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}


@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpSession session = servletRequest.getSession();
Object object = session.getAttribute(Constant.USER_SESSION_NAME);
if (Objects.isNull(object)) {
HttpServletResponse servletResponse = (HttpServletResponse) response;
servletResponse.sendRedirect("/login");
} else {
chain.doFilter(request, response);
}
}


@Override
public void destroy() {

}
}

代码逻辑比较简单易懂。根据springboot的自动配置特性,这样写,就可以达到用户在请求 /user 和 /detail 开头的请求的时候进行过滤了。


标签:springboot,void,request,response,Filter,Override,servlet,public,请求
From: https://blog.51cto.com/u_14196886/5819073

相关文章

  • Springboot使用mongodb遇到问题及解决
    网上看到使用mongodb好像很简单,没有什么问题,可我一用就怎么都连不上,先看看我的配置 在pom.xml中添加依赖 1234<dependency>  <groupId>org.springframew......
  • SpringBoot定时任务实现数据同步
    业务的需求是,通过中台调用api接口获得,设备数据,要求现实设备数据的同步。方案一:通过轮询接口的方式执行pullData()方法实现数据同步该方式的原理是先清空之前的所有数据,然......
  • springboot项目整合-注册功能模块开发
    工程简介准备工作:项目所用到的html界面以及sql文件链接如下:链接:https://pan.baidu.com/s/18loHJiKRC6FI6XkoANMSJg?pwd=nkz2提取码:nkz2复制这段内容后打开百度网盘......
  • SpringBoot高级篇MongoDB之查询基本使用姿势
    学习一个新的数据库,一般怎么下手呢?基本的CURD没跑了,当可以熟练的增、删、改、查一个数据库时,可以说对这个数据库算是入门了,如果需要更进一步的话,就需要了解下数据库的特性,比......
  • SpringBoot文件上传异常之temporary upload location not valid
    SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示​​org.springframework.web.multipart.MultipartException:Failedtoparsemultipartservletrequest;......
  • SpringBoot + Mybatis系列之插件机制 Interceptor
    【SpringBoot+Mybatis系列】插件机制Interceptor在Mybatis中,插件机制提供了非常强大的扩展能力,在sql最终执行之前,提供了四个拦截点,支持不同场景的功能扩展Executor(......
  • [springboot, lettuce] io.lettuce.core.RedisCommandTimeoutException: Command time
    https://blog.csdn.net/zzhongcy/article/details/118935350?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFr......
  • SpringBoot笔记:集成MyBatis
    SpringBoot中使用MyBatis与MVC中本质是一样的,只是某些配置可以直接使用注解完成,使编码更加便捷了。1.pom依赖集成MyBatis通常需要MyBatis、Spring、数据库驱动三个依赖,......
  • SpringBoot笔记:拦截器Interceptor和过滤器Filter
    一、拦截器InterceptorSpringBoot中定义拦截器与MVC中是一样的,区别在于拦截器的配置,MVC是配置在配置文件中的,SpringBoot中则是配置在配置类中的。(SpringBoot中的配置类需......
  • JavaWeb三大组件之Servlet学习
    JavaWeb三大组件之Servlet学习平时直接用springmvc较多,都没怎么接触底层的Servlet,导致对一些基本的知识点了解都不够,所以今天专门的抽出时间来学习一下带着问题出发,看下可以......