请求拦截器

概述

这个文档提供了如何在 tio-boot 中配置和使用自定义的 HTTP 拦截器。拦截器可用于控制对特定 URL 路径的访问,允许您定义哪些路径应被拦截(blockedUrls)和哪些路径应被允许(allowedUrls)。

tio-boot 拦击器常用类

HttpServerInterceptorModel

HttpServerInterceptorModel 类是拦截器的配置模型,包含拦截器的名称、被允许和被拦截的 URL 列表,以及拦截器实例。

使用方法
  • 添加允许的 URL: 使用 addAllowedUrl(String url)addAllowedUrls(String... urls) 方法添加一个或多个允许的 URL。
  • 添加被拦截的 URL: 使用 addblockedUrl(String url)addBlockedUrls(String... urls) 方法添加一个或多个被拦截的 URL。
  • 设置拦截器: 提供一个实现了 HttpServerInterceptor 接口的拦截器实例。

DefaultHttpServerInterceptor

DefaultHttpServerInterceptor 类是实际执行拦截操作的类,它实现了 HttpServerInterceptor 接口。

实现细节
  • 拦截处理: doBeforeHandler 方法在 HTTP 请求处理之前执行。它会检查请求的 URL 是否匹配任何拦截器的 blockedUrlsallowedUrls 规则。
  • 后处理: doAfterHandler 方法在 HTTP 请求处理之后执行。它可以用于执行清理操作或后续处理。

ServerInteceptorConfigure

ServerInteceptorConfigure 类用于管理和配置所有的拦截器。

使用方法
  • 添加拦截器: 使用 add(HttpServerInterceptorModel model) 方法添加一个拦截器模型。
  • 移除拦截器: 使用 remove(String name) 方法通过名称移除一个拦截器。
  • 获取所有拦截器: 使用 getInteceptors() 方法获取当前配置的所有拦截器模型。

示例代码

// 创建拦截器模型
HttpServerInterceptorModel model = new HttpServerInterceptorModel()
    .setName("exampleInterceptor")
    .addBlockedUrl("/admin/*")
    .addAllowedUrl("/admin/login")
    .setInterceptor(new YourCustomInterceptor()); // YourCustomInterceptor 应实现 HttpServerInterceptor

// 配置拦截器
ServerInteceptorConfigure configure = new ServerInteceptorConfigure();
configure.add(model);

// 在 TioBootServer 中设置拦

截器配置
TioBootServer.setServerInteceptorConfigure(configure);

注意事项

  • 确保 allowedUrlsblockedUrls 之间的规则不发生冲突,即同一路径不应同时出现在两个列表中。
  • 通配符 /**/* 用于匹配特定路径下的所有子路径。例如,/admin/** 会匹配 /admin 下的所有路径。
  • DefaultHttpServerInterceptordoBeforeHandler 方法中,如果请求的 URL 匹配 blockedUrls 且不匹配 allowedUrls,则请求将被拦截。
  • 自定义拦截器应实现 HttpServerInterceptor 接口,并定义适当的处理逻辑。

自定义拦截器示例

定义一个拦击器需要实现 HttpServerInterceptor 接口,用于拦截 HTTP 请求。它主要包含两个方法:

  1. doBeforeHandler:在处理 HTTP 请求之前被调用。这个方法记录了请求的信息,并返回原来的响应对象(responseFromCache),如果有的话。如果返回值为 null 则会执行后续拦击器,如果返回的值不为 null 则不会执行后续的拦击器

  2. doAfterHandler:在处理 HTTP 请求之后被调用。这个方法再次记录了请求的信息,并可以进行额外的处理。

通过拦截器,可以在请求处理的前后阶段记录日志,监控请求的详细情况或执行其他自定义逻辑。这在跟踪和分析 HTTP 请求流程时非常有用。

package com.litongjava.tio.boot.admin.inteceptor;

import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.common.RequestLine;
import com.litongjava.tio.http.server.intf.HttpServerInterceptor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GlobalInteceptor implements HttpServerInterceptor {

  public HttpResponse doBeforeHandler(HttpRequest request, RequestLine requestLine, HttpResponse responseFromCache)
      throws Exception {
    log.info("request:{}", request);
    return responseFromCache;
  }

  public void doAfterHandler(HttpRequest request, RequestLine requestLine, HttpResponse response, long cost)
      throws Exception {
    log.info("request:{}", request);

  }

}

package com.litongjava.tio.boot.admin.inteceptor;

import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.common.RequestLine;
import com.litongjava.tio.http.server.intf.HttpServerInterceptor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HelloInteceptor implements HttpServerInterceptor {

  public HttpResponse doBeforeHandler(HttpRequest request, RequestLine requestLine, HttpResponse responseFromCache)
      throws Exception {
    log.info("request", request);
    return null;
  }

  public void doAfterHandler(HttpRequest request, RequestLine requestLine, HttpResponse response, long cost)
      throws Exception {
    log.info("request", request);

  }

}

 package com.litongjava.tio.boot.admin.config;

import com.litongjava.jfinal.aop.annotation.AConfiguration;
import com.litongjava.jfinal.aop.annotation.AInitialization;
import com.litongjava.tio.boot.admin.inteceptor.GlobalInteceptor;
import com.litongjava.tio.boot.admin.inteceptor.HelloInteceptor;
import com.litongjava.tio.boot.http.interceptor.HttpServerInterceptorModel;
import com.litongjava.tio.boot.http.interceptor.ServerInteceptorConfigure;
import com.litongjava.tio.boot.server.TioBootServer;

@AConfiguration
public class IntecpetorConfiguration {

  @AInitialization
  public void serverInteceptorRoutes() {

    // add global
    HttpServerInterceptorModel global = new HttpServerInterceptorModel();
    global.setName("global");
    global.addblockeUrl("/**");
    global.setInterceptor(new GlobalInteceptor());

    HttpServerInterceptorModel hello = new HttpServerInterceptorModel();
    hello.setName("hello");
    hello.addblockeUrl("/hello");
    hello.setInterceptor(new HelloInteceptor());

    ServerInteceptorConfigure config = new ServerInteceptorConfigure();
    config.add(global);
    config.add(hello);

    TioBootServer.setServerInteceptorConfigure(config);
  }

}
  1. IntecpetorConfiguration 类:

    • @AConfiguration 注解标记,表明它是一个配置类。
    • 定义了 serverInteceptorRoutes 方法,用 @ABean 注解标记,表明它提供一个 bean 实例。
    • 在这个方法中,创建了 ServerInteceptorConfigure 实例,并通过 add 方法添加了两个拦截器:GlobalInteceptor 应用于所有路径(/**),HelloInteceptor 仅应用于 /hello 路径。
  2. GlobalInteceptorHelloInteceptor 类:

    • 两个类都实现了 HttpServerInterceptor 接口。
    • doBeforeHandler 方法在处理 HTTP 请求之前执行。对于 GlobalInteceptor,它返回 responseFromCache,而 HelloInteceptor 返回 null
    • doAfterHandler 方法在请求处理后执行,记录请求信息。
  3. DefaultHttpServerInterceptor 用于执行拦截器,拦截器的执行顺序是 config.add 添加的顺序