请求拦截器

概述

本文介绍了如何在 Tio-Boot 中配置和使用自定义的 HTTP 拦截器。拦截器可用于控制对特定 URL 路径的访问,允许您定义哪些路径应被拦截 (blockedUrls),哪些路径应被允许 (allowedUrls),从而实现对请求的精细化控制。

Tio-Boot 拦截器常用类

1. HttpInterceptorModel

HttpInterceptorModel 类是拦截器的配置模型,包含拦截器的名称、允许的 URL 列表 (allowedUrls)、被拦截的 URL 列表 (blockedUrls),以及拦截器的实例。可以通过以下方法对拦截器进行配置:

  • 添加允许的 URL: 使用 addAllowedUrl(String url)addAllowedUrls(String... urls) 方法添加一个或多个允许的 URL。
  • 添加被拦截的 URL: 使用 addBlockedUrl(String url)addBlockedUrls(String... urls) 方法添加一个或多个需要拦截的 URL。
  • 设置拦截器: 提供一个实现了 HttpRequestInterceptor 接口的拦截器实例,用于处理拦截逻辑。
2. DefaultHttpRequestInterceptorDispatcher

DefaultHttpRequestInterceptorDispatcher 类是实际执行拦截操作的类,它实现了 HttpRequestInterceptor 接口,并处理拦截器的执行顺序。

  • 拦截前处理: doBeforeHandler 方法在 HTTP 请求处理之前执行,检查 URL 是否匹配拦截规则。
  • 拦截后处理: doAfterHandler 方法在 HTTP 请求处理之后执行,用于执行后续处理或日志记录等操作。
3. HttpInteceptorConfigure

HttpInteceptorConfigure 类用于管理和配置所有的拦截器。可以通过以下方法管理拦截器:

  • 添加拦截器: 使用 add(HttpInterceptorModel model) 方法将拦截器模型添加到配置中。
  • 移除拦截器: 使用 remove(String name) 方法通过拦截器的名称移除它。
  • 获取拦截器: 使用 getInterceptors() 方法获取当前配置的所有拦截器模型。

示例代码

// 创建拦截器模型
HttpInterceptorModel model = new HttpInterceptorModel()
    .setName("exampleInterceptor")
    .addBlockeUrl("/admin/*")
    .addAlloweUrl("/admin/login")
    .setInterceptor(new YourCustomInterceptor()); // YourCustomInterceptor 应实现 HttpRequestInterceptor

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

// 在 TioBootServer 中设置拦截器配置
TioBootServer.setServerInterceptorConfigure(configure);

注意事项

  1. URL 匹配规则: 确保 allowedUrlsblockedUrls 规则不冲突。同一路径不能同时出现在这两个列表中。
  2. 通配符支持: 通配符 /**/* 可以匹配子路径,例如 /admin/** 可匹配 /admin 下的所有路径。
  3. 拦截器执行逻辑: 在 DefaultHttpRequestInterceptorDispatcherdoBeforeHandler 方法中,如果请求 URL 匹配 blockedUrls 且不匹配 allowedUrls,则该请求会被拦截。
  4. 自定义拦截器: 实现 HttpRequestInterceptor 接口并自定义拦截逻辑。

自定义拦截器示例

自定义拦截器需要实现 HttpRequestInterceptor 接口,包含以下两个方法:

  1. doBeforeHandler:在处理 HTTP 请求之前执行。该方法可记录请求信息并返回缓存的响应对象。如果返回值为 null,则执行后续拦截器;如果不为 null,则后续拦截器不会执行。
  2. doAfterHandler:在处理 HTTP 请求之后执行。用于记录日志、清理操作或其他后处理逻辑。

示例代码:

package com.litongjava.tio.web.hello.interceptor;

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.HttpRequestInterceptor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GlobalInterceptor implements HttpRequestInterceptor {

  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 completed: {}", request);
  }
}

拦截器配置:

package com.litongjava.tio.web.hello.config;

import com.litongjava.annotation.AConfiguration;
import com.litongjava.annotation.AInitialization;
import com.litongjava.tio.boot.http.interceptor.HttpInteceptorConfigure;
import com.litongjava.tio.boot.http.interceptor.HttpInterceptorModel;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.web.hello.interceptor.GlobalInterceptor;

@AConfiguration
public class InterceptorConfiguration {

  @Initialization
  public void configureInterceptors() {
    HttpInterceptorModel global = new HttpInterceptorModel();
    global.setName("global");
    global.addBlockeUrl("/**"); // 拦截所有路径
    global.setInterceptor(new GlobalInterceptor());

    HttpInteceptorConfigure config = new HttpInteceptorConfigure();
    config.add(global);

    TioBootServer.me().setHttpInterceptorConfigure(config);
  }
}

通过该配置,GlobalInterceptor 会拦截所有路径 (/**),并对请求进行处理。