【问题标题】:How to read the apple-app-site-association file on Vapor 4?如何阅读 Vapor 4 上的 apple-app-site-association 文件?
【发布时间】:2021-06-02 22:42:35
【问题描述】:

为了在 Apple 平台上使用自动填充密码,我正在这个 website 中测试 Apple App Site Association (AASA) Validator。 我在 Public/.well-known/apple-app-site-association 文件中添加了所需的 json,以便自动填充密码在我的 iOS 应用程序上工作。

此测试的结果返回此错误: Your file's 'content-type' header was not found or was not recognized.

有人遇到过这个问题吗? AASA 文件似乎没有下载到我的设备中。

请注意,在 iOS 14 上,AASA 文件将通过 Apple 的 CDN 传送,这与当前下载 AASA 文件的方式不同。

在我的 Vapor 4 项目中还有其他事情要做吗?

【问题讨论】:

  • 你的 Vapor 应用前面有 nginx 吗?
  • 如果是,那么您可以在虚拟主机文件location /.well-known/apple-app-site-association { default_type application/json; } 中声明正确的内容类型,如果不是,那么您应该在vapor 中创建中间件,该中间件将使用正确的apple-app-site-association 流式传输content-type 标头跨度>
  • 我没有使用 nginx。我的 Vapor 应用程序部署在 Heroku 上。你能告诉我如何创建这个特定的中间件吗?
  • 只需查看FileMiddleware 来源并对其进行编辑。同样在heroku上,您似乎正在使用FileMiddleware,这对生产不利,因为每个请求(甚至对API端点)它首先检查磁盘上的文件,然后才将请求传递给下一个响应者(路由器),这非常慢用于生产。如果您将使用自己的中间件,请注意这方面,请检查 headers 或 url.lastPathComponent 以了解您必须在磁盘上查找文件,如果没有,则立即传递给下一个响应者。
  • Public 文件夹在 Heroku 上运行良好,唯一的问题是 apple-app-site-association 没有 .json 扩展名,这就是为什么 FileMiddleware 甚至 nginx 都无法提供正确的 content-type 标头,所以你必须在自定义中间件中自己完成。

标签: content-type vapor aasa apple-app-site-association


【解决方案1】:

我遇到同样的问题,按照 imike 的回答并做一些研究,这是解决方案。

  1. 创建自定义中间件
struct UniversalLinksMiddleware: Middleware {
    
    func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
        guard request.url.string == "/.well-known/apple-app-site-association" else {
            return next.respond(to: request)
        }
        
        return next.respond(to: request).map { response in
            response.headers.add(name: "content-type", value: "application/json")
            return response
        }
    }
    
}

  1. config.swift 文件中添加这个中间件。注意添加中间件的顺序,必须在FileMIddleware之前添加。因为离开您的应用程序的响应以相反的顺序通过中间件。
app.middleware.use(UniversalLinksMiddleware())
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2016-01-06
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多