【问题标题】:Exclude route from authentication in Compojure with friend?与朋友一起在 Compojure 中从身份验证中排除路由?
【发布时间】:2015-12-17 13:49:45
【问题描述】:

我通过使用friend 库的基本身份验证来保护使用compojure 创建的几个HTTP 路由。它看起来像这样:

(defroutes my-routes
             (context "/ctx" []
                      (GET "/x" [] ...)
                      (GET "/y" [] ...)
                      (GET "/z" [] ...)))

(let [friend-auth-cfg {:allow-anon?             false
                       :unauthenticated-handler #(workflows/http-basic-deny "Unauthorized" %)
                       :workflows               [(workflows/http-basic
                                                   :credential-fn #(creds/bcrypt-credential-fn {"username" {:password (creds/hash-bcrypt "password")}} %)
                                                   :realm "My realm")]}
      my-route (-> (wrap-defaults my-routes api-defaults)
                   (friend/authenticate friend-auth-cfg))]
   (run-jetty (routes my-route)))

我想做的是排除“y”路由 (/ctx/y) 不受基本身份验证的保护(但 x 和 z 仍应受到保护)。我怎样才能做到这一点?

【问题讨论】:

    标签: clojure basic-authentication compojure


    【解决方案1】:

    我最终将“我的路线”分成两部分:

    (defroutes protected-routes
                 (context "/ctx" []
                          (GET "/x" [] ...)
                          (GET "/z" [] ...)))
    

    和:

    (defroutes unprotected-routes
                 (GET "/ctx/y" [] ...))
    

    并且只将friend/authenticate 中间件应用于protected-routes 并最终像这样将它们合并在一起:

    (run-jetty (routes my-route))
    

    【讨论】:

    • 这是我通常最终会做的事情,并且看到其他人也这样做。
    猜你喜欢
    • 2016-05-10
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多