SuperStarter使用Arcjet进行应用安全防护,其中包含您可以在应用程序中使用的IP详情和地理位置信息

app应用中,Arcjet在apps/app/app/(authenticated)/layout.tsx文件中被调用,该文件在每个认证路由上运行。

对于web应用,Arcjet在中间件中被调用,该中间件在每个请求上运行(静态资源除外)。

在这两种情况下,您都可以基于IP详情应用应用/网站范围的规则,例如根据用户位置显示不同内容。

访问IP位置数据

IP详情可在Arcjet决策对象中获得,该对象从aj.protect()调用返回。IP位置字段可能为undefined,因此您可以使用各种实用函数来获取数据。

// ...
const decision = await aj.protect(req);

if (decision.ip.hasCity() && decision.ip.city === "San Francisco") {
  // Create a custom response for San Francisco
}

if (decision.ip.hasRegion() && decision.ip.region === "California") {
  // Create a custom response for California
}

if (decision.ip.hasCountry() && decision.ip.country === "US") {
  // Create a custom response for the United States
}

if (decision.ip.hasContinent() && decision.ip.continent === "NA") {
  // Create a custom response for North America
}

See the Arcjet IP analysis reference for more information on the fields available.

在其他地方访问IP详情

Next.js不允许从根布局或中间件传递数据到应用程序的其他页面。要在应用程序的其他部分访问IP详情,您需要从根布局(app)或中间件(web)中移除Arcjet调用,并在需要IP详情的特定页面中调用它。

请参阅Arcjet文档了解如何在路由处理器页面/服务器组件服务器操作中调用aj.protect()

If you remove the Arcjet call from apps/app/app/(authenticated)/layout.tsx or middleware.ts it will no longer run on every request. You will need to call aj.protect() everywhere you wish to apply Arcjet rules, even if you don’t need the IP details.