SpringBoot 整合Filter

Stella981
• 阅读 756

SpringBoot 整合Filter

一、创建基础SpringBoot项目,集成Web即可

<**dependency**> <**groupId**>org.springframework.boot</**groupId**> <**artifactId**>spring-boot-starter-web</**artifactId**> </**dependency**>

二、方式一:通过注解方式注入

1. 创建Filter组件

com.zhl.springbootweb.filter.FirstFilter

  • urlPatterns  可以指定后缀 或者 URL
  • 实现@WebFilter注解
  • 集成javax.servlet中的 Filter

package com.zhl.springbootweb.filter;

import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;

/*整合__Filter__方式一*/ //@WebFilter(filterName = "first Filter",urlPatterns = {"*.do","*.jsp"}) @WebFilter(filterName = "first Filter",urlPatterns = {"/first"}) public class FirstFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {

}

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println(**"进入First Filter"); _/*放行*/ _ filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开First Filter"**);

}

@Override

public void destroy() {

}

}

2. 启动类需要添加注解

@ServletComponentScan

@SpringBootApplication
/*在SpringBoot启动时会扫描@WebServlet,@WebFilter注解,并将该类实例化*/
@ServletComponentScan
public class SpringbootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootWebApplication.class, args);
    }

}

3.测试

访问localhost:8080/first

进入First Filter
First Servlet
离开First Filter

三、方式二:使用方法注入

1. 创建Filter

com.zhl.springbootweb.filter.SecondFilter

package com.zhl.springbootweb.filter;

import javax.servlet.*; import java.io.IOException; /* * 整合__Filter 方式二 * */ public class SecondFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {

}

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println(**"进入SecondFilter"); _/*放行*/ _ filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开SecondFilter"**);

}

@Override

public void destroy() {

}

}

2.创建配置类

/*Filter 配置类*/ @Configuration public class FilterConfig { @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); _//bean.addUrlPatterns(new String[]{"*.do","*.jsp"}); _ bean.addUrlPatterns("/second"); return bean; } }

3.测试

访问 http://localhost:8080/second

进入SecondFilter
Second Servlet
离开SecondFilter
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
tio
本文讲述如何快速将tio服务整合到SpringBoot项目首先,你需要在pom.xml中引入tiocorespringbootstarter构件<dependency<groupIdorg.tio</groupId<artifactIdtiocorespringboot
Easter79 Easter79
3年前
springboot2.0之jsp整合
  这篇博客是学习springboot整合各种三方框架后基本的CRUD操作。  1,整合jsp     注意springboot默认是不支持jsp的,所以我们利用springboot集成jsp时,一定要创建成一个war类型项目,同时要引入外部tomcat。    如项目结构:    !(https://oscim
Easter79 Easter79
3年前
Springboot整合websocket实现一对一消息推送和广播消息推送
springboot基础环境请参考springboot文档maven依赖        <dependency            <groupIdorg.springframework.boot</groupId            <artifactIdspringbootstarterwebso
Stella981 Stella981
3年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Easter79 Easter79
3年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Stella981 Stella981
3年前
Spring Boot整合redis
一、添加依赖<!SpringBoot整合redis的依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter
Easter79 Easter79
3年前
SpringBoot2整合activiti6环境搭建
SpringBoot2整合activiti6环境搭建依赖<dependencies<dependency<groupIdorg.springframework.boot</groupId
Stella981 Stella981
3年前
ElasticSearch快速搭建java项目
1.创建springboot项目Pom文件引入elasticsearch依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterdataelasticsearch</artifactI
Stella981 Stella981
3年前
SpringBoot2整合activiti6环境搭建
SpringBoot2整合activiti6环境搭建依赖<dependencies<dependency<groupIdorg.springframework.boot</groupId
Stella981 Stella981
3年前
Erupt 降维打击代码生成器,急速开发后台管理系统
环境搭建编程语言:Java8使用框架:SpringBoot,Erupt1\.在springboot项目基础上导入erupt依赖<!用户权限管理<dependency<groupIdxyz.erupt</groupId<artifactIderuptupms</a