【问题标题】:Wrapping resource handlers with bidi用 bidi 包装资源处理程序
【发布时间】:2016-06-22 20:22:29
【问题描述】:

如何使用friend和bidi来包装资源处理程序?

我已成功让 oAuth 对路由进行身份验证:

(defn auth-handler [request] (friend/authorize #{::user}
                                               {:status 200
                                                :body   "a secret"}))

(def routes ["/" {true auth-handler}])

(def app (make-handler routes))

(web/run-dmc (-> app
                   var
                   (friend/authenticate
                     {:allow-anon? true
                      :workflows   [(oauth/workflow
                                      {:client-config client-config
                                       :uri-config    uri-config
                                       :credential-fn credential-fn})]})
                   (wrap-resource "public")
                   (wrap-defaults site-defaults)
                   ))

这适用于“/”路线,但我想确保在没有先验证的情况下无法访问某些资源。

friend/wrap-authorize 函数似乎可以做到这一点:

我最接近的尝试适用于 auth 包装的路由,但与非 /dev/ 路由不匹配:

(def routes ["/" [["dev/" [[true (friend/wrap-authorize (resources {:prefix "dev/"}) #{::user})]]]
                  [true (resources {:prefix "public/"})]]])


(match-route routes "/dev/index.html")
=>
{:handler #object[cemerick.friend$wrap_authorize$fn__24411
              0x2400d0be
              "cemerick.friend$wrap_authorize$fn__24411@2400d0be"]}
;correct

(match-route routes "/index.html")
=>
nil
;not correct

我认为路由模式 [true (resources {:prefix "public/"})] 的匹配部分是错误的,因为当我将其更改为 :key 时,`index.html' 确实匹配。

如何将非 /dev/* 路由匹配到公共资源?

【问题讨论】:

    标签: oauth clojure bidi


    【解决方案1】:

    这里的主要问题是资源路由应该是

    ["" (resources {:prefix "public/"})]
    

    空字符串而不是true

    文档确实说明:模式匹配后,路径的剩余部分被添加到给定的前缀中。

    但坦率地说,我认为这是非常令人惊讶的行为。

    我在这里做了一个最小的示例项目,成功路由 /index.html https://github.com/timothypratley/bidi-resources

    值得注意的是,请求 /index.html2 会导致异常,这又不是我所期望的。我期待的是 404。o_O

    我真的很喜欢 ClojureScript 中的 bidi,但到目前为止,我发现它在服务器端是一个艰难的过程……我想知道为什么 true 不起作用的方法是用我自己的覆盖资源定义打印出输入的版本,发现 :remainder 是空的。

    【讨论】:

    • 感谢您查看问题。我现在注意到我的代码甚至不适用于 dev/ 处理程序,或者至少不一致。我想我现在会改用 compojure。还对为什么我的 (file-response) 和 (resource-response) 让我的浏览器下载响应而不是在浏览器中加载它们感到困惑。
    • 是的,可悲的是没有太多的例子可以效仿......很遗憾,因为这似乎是一个好主意,但让它适用于实际情况却很困难。浏览器下载文件通常表示未设置 content-type 标头(因为缺少中间件或处理程序未返回响应)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2018-12-30
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多