SpringBoot整合Filter

释放双眼,带上耳机,听听看~!

SpringBoot 整合 Filter 方式一:通过注解扫描完成 Filter 组件的注册

创建项目,在pom.xml文件中导入依赖。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2  <modelVersion>4.0.0</modelVersion>
3  <parent>
4    <groupId>org.springframework.boot</groupId>
5    <artifactId>spring-boot-starter-parent</artifactId>
6    <version>1.5.10.RELEASE</version>
7  </parent>
8  <groupId>com.bjsxt</groupId>
9  <artifactId>03-spring-boot-filter</artifactId>
10  <version>0.0.1-SNAPSHOT</version>
11  
12  <!-- jdk1.7 -->
13  <properties>
14      <java.version>1.7</java.version>
15  </properties>
16  
17  <dependencies>
18    <dependency>
19        <groupId>org.springframework.boot</groupId>
20        <artifactId>spring-boot-starter-web</artifactId>
21    </dependency>
22</dependencies>
23</project>
24
25

编写Filter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1package com.bjsxt.filter;
2
3import java.io.IOException;
4
5import javax.servlet.Filter;
6import javax.servlet.FilterChain;
7import javax.servlet.FilterConfig;
8import javax.servlet.ServletException;
9import javax.servlet.ServletRequest;
10import javax.servlet.ServletResponse;
11import javax.servlet.annotation.WebFilter;
12
13/**
14 *SpringBoot整合Filter 方式一
15 *<filter>
16 *    <filter-name>FirstFilter</filter-name>
17 *    <filter-class>com.bjsxt.filter.FirstFilter</filter-class>
18 *</filter>
19 *<filter-mapping>
20 *    <filter-name>FirstFilter</filter-name>
21 *    <url-pattern>/first</url-pattern>
22 *</filter-mapping>
23 */
24//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
25@WebFilter(filterName="FirstFilter",urlPatterns="/first")
26public class FirstFilter implements Filter {
27
28  @Override
29  public void destroy() {
30      // TODO Auto-generated method stub
31
32  }
33  @Override
34  public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
35          throws IOException, ServletException {
36      System.out.println("进入Filter");
37      arg2.doFilter(arg0, arg1);
38      System.out.println("离开Filter");
39  }
40
41  @Override
42  public void init(FilterConfig arg0) throws ServletException {
43      // TODO Auto-generated method stub
44  }
45}
46
47

编写启动类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1package com.bjsxt;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.boot.web.servlet.ServletComponentScan;
6
7/**
8 *SpringBoot整合Filter 方式一
9 *
10 */
11@SpringBootApplication
12@ServletComponentScan
13public class App {
14
15  public static void main(String[] args) {
16      SpringApplication.run(App.class, args);
17
18  }
19}
20
21

运行结果
SpringBoot整合Filter

SpringBoot 整合 Filter 方式二:通过方法完成 Filter 组件的注册

pom.xml文件同上

编写Filter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1package com.bjsxt.filter;
2
3import java.io.IOException;
4
5import javax.servlet.Filter;
6import javax.servlet.FilterChain;
7import javax.servlet.FilterConfig;
8import javax.servlet.ServletException;
9import javax.servlet.ServletRequest;
10import javax.servlet.ServletResponse;
11/**
12 *
13 *SpringBoot整合Filter 方式二
14 *
15 */
16public class SecondFilter implements Filter {
17  @Override
18  public void destroy() {
19      // TODO Auto-generated method stub
20  }
21  @Override
22  public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
23          throws IOException, ServletException {
24      System.out.println("进入SecondFilter二");
25      arg2.doFilter(arg0, arg1);
26      System.out.println("离开SecondFilter二");
27  }
28
29  @Override
30  public void init(FilterConfig arg0) throws ServletException {
31      // TODO Auto-generated method stub
32  }
33}
34
35

编写启动类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1package com.bjsxt;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.boot.web.servlet.FilterRegistrationBean;
6import org.springframework.boot.web.servlet.ServletRegistrationBean;
7import org.springframework.context.annotation.Bean;
8
9import com.bjsxt.filter.SecondFilter;
10import com.bjsxt.servlet.SecondServlet;
11
12/**
13 * SpringBoot整合Filter方式二
14 *
15 *
16 */
17@SpringBootApplication
18public class App2 {
19
20  public static void main(String[] args) {
21      SpringApplication.run(App2.class, args);
22  }
23 
24  /**
25   * 注册Servlet
26   * @return
27   */
28  @Bean
29  public ServletRegistrationBean getServletRegistrationBean(){
30      ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
31      bean.addUrlMappings("/second");
32      return bean;
33  }
34 
35  /**
36   * 注册Filter
37   */
38  @Bean
39  public FilterRegistrationBean getFilterRegistrationBean(){
40      FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
41      //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
42      bean.addUrlPatterns("/second2");
43      return bean;
44  }
45}
46
47

测试运行结果
SpringBoot整合Filter

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Netty 实现 WebSocket 聊天功能

2022-1-11 12:36:11

安全经验

如何防止网站被降权八大要素

2021-10-11 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索