spring boot servlet 过滤器——迹忆客-ag捕鱼王app官网

servlet 过滤器是用于拦截应用程序的 http 请求和响应的对象。 通过使用过滤器,我们可以在两个实例上执行两个操作

  • 在向控制器发送请求之前
  • 在向客户端发送响应之前

以下代码显示了带有 @component 注解的 servlet 过滤器实现类的示例代码。

@component
public class simplefilter implements filter {
   @override
   public void destroy() {}
   @override
   public void dofilter
      (servletrequest request, servletresponse response, filterchain filterchain) 
      throws ioexception, servletexception {}
   @override
   public void init(filterconfig filterconfig) throws servletexception {}
}

以下示例显示了在将请求发送到控制器之前从 servletrequest 对象读取远程主机和远程地址的代码。

dofilter() 方法中,我们添加了 system.out.println 语句来打印远程主机和远程地址。

package com.study.filter;
import org.springframework.stereotype.component;
import javax.servlet.filter;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
import javax.servlet.filterchain;
import javax.servlet.servletexception;
import javax.servlet.filterconfig;
import java.io.ioexception;
@component
public class simplefilter implements filter {
    @override
    public void init(filterconfig filterconfig) throws servletexception {
    }
    @override
    public void dofilter(servletrequest request, servletresponse response, filterchain filterchain) throws ioexception, servletexception {
        system.out.println("remote host:" request.getremotehost());
        system.out.println("remote address:" request.getremoteaddr());
        filterchain.dofilter(request, response);
    }
    @override
    public void destroy() {
    }
}

在 spring boot 主应用程序类文件中,我们添加了返回 “hello 迹忆客(jiyik.com)” 字符串的简单 rest 端点。

package com.study;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.requestmapping;
/**
 * @author jiyik.com
 */
@springbootapplication
public class myapplication {
    public static void main(string[] args) {
        springapplication.run(myapplication.class, args);
    }
    @requestmapping(value = "/")
    public string hello() {
        return "hello 迹忆客(jiyik.com)";
    }
}

maven 构建的代码 – pom.xml 如下所示



    4.0.0
    com.spring
    springbootproject
    1.0-snapshot
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

gradle build - build.gradle 的代码如下

buildscript {
   ext {
      springbootversion = '1.5.8.release'
   }
   repositories {
      mavencentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.study'
version = '0.0.1-snapshot'
sourcecompatibility = 1.8
repositories {
   mavencentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testcompile('org.springframework.boot:spring-boot-starter-test')
}

这里我们使用 idea 来启动服务(读者也可以使用 mvn clean install 或者 gradle clean build 生成可执行jar包)

spring boot 异常处理 idea启动服务

现在访问 url http://localhost:8080/ 并查看输出 hello 迹忆客(jiyik.com)。 如下所示

spring boot servlet 过滤器

然后,我们可以在控制台日志中看到远程主机和远程地址,如下所示

spring boot servlet 过滤器控制台

查看笔记

扫码一下
查看教程更方便
网站地图