Filter过滤器

小咪咪 2023-07-06 09:49 13阅读 0赞

1.创建项目
在这里插入图片描述
2.点击Next
在这里插入图片描述
3.选择web
在这里插入图片描述
4.pom如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.4.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.take</groupId>
  12. <artifactId>filter-demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>filter-demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.junit.vintage</groupId>
  31. <artifactId>junit-vintage-engine</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

5.创建controller包,包下创建两个类:HelloControllerWorldController

  1. package com.take.filterdemo.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @RequestMapping("hello")
  7. public String hello(){
  8. return "hello";
  9. }
  10. }
  11. package com.take.filterdemo.controller;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. @RestController
  15. public class WorldController {
  16. @RequestMapping("world")
  17. public String world(){
  18. return "world";
  19. }
  20. }

6.添加application.yml,加入配置

  1. server:
  2. port: 8080
  3. spring:
  4. main:
  5. allow-bean-definition-overriding: true

7.创建filter包,包下创建HelloFilter类,并实现Fillter接口,注意是javax.servlet.*;下的包

只拦截了/hello的资源,并没有拦截/world资源

  1. package com.take.filterdemo.filter;
  2. import org.springframework.stereotype.Component;
  3. import javax.servlet.*;
  4. import javax.servlet.annotation.WebFilter;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. /** * urlPatterns: 拦截的路径,可以写多个,如 @WebFilter(urlPatterns = {"/hello","/world"},filterName = "helloFilter") */
  9. @Component
  10. @WebFilter(urlPatterns = "/hello",filterName = "helloFilter")
  11. public class HelloFilter implements Filter {
  12. @Override
  13. public void init(FilterConfig filterConfig) throws ServletException {
  14. }
  15. @Override
  16. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  17. HttpServletRequest request=(HttpServletRequest)servletRequest;
  18. HttpServletResponse response = (HttpServletResponse) servletResponse;
  19. //逻辑省略。。。
  20. // filterChain.doFilter(servletRequest,servletResponse);//放行
  21. }
  22. @Override
  23. public void destroy() {
  24. }
  25. }

8.在启动类上加上注解@ServletComponentScan,会把过滤器扫描到spring

  1. package com.take.filterdemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. @SpringBootApplication
  6. @ServletComponentScan
  7. public class FilterDemoApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(FilterDemoApplication.class, args);
  10. }
  11. }

9.启动访问

/hello资源被拦截
在这里插入图片描述
/world资源访问成功
在这里插入图片描述

参考链接:
https://www.cnblogs.com/ideal-20/p/11496091.html

下一篇:SpringMVC的拦截器

发表评论

表情:
评论列表 (有 0 条评论,13人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Filter 过滤器

    Javaweb中的过滤器可以拦截所有访问web资源的请求或响应操作。 1.什么是Filter及其作用介绍      Filter是sun公司中servlet2.3后增加的一

    相关 Filter过滤器

    Filter的基本功能是对servlet容器调用servlet的过程进行拦截,从而在servlet进行响应处理的前后实现一些特殊的功能。 在servletAPI中定义了三个接

    相关 Filter过滤器

    过滤器: 过滤   过滤请求 可以写多个 从用户访问  ->第一个过滤器  -> 第二个过滤 -> servlet   生命周期:随着项目的启动而创建 当访问了地址后而

    相关 Filter过滤器

    一、Filter过滤器 1、介绍       Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态资源文件等进行

    相关 Filter 过滤器

    > 过滤器,其实就是对客户端发出来的请求进行过滤。浏览器发出,然后服务器派Servlet处理。在中间就可以过滤,其实过滤器起到的是拦截的作用,也就是拦截器。 作用