【问题标题】:How to add Friend auth to Chestnut template如何将好友身份验证添加到栗子模板
【发布时间】:2015-05-29 17:18:36
【问题描述】:

几天来,我一直在努力尝试简单地使用安全库 Friend 来使用 Chestnut clj/cljs 模板。

/login uri 的POST 请求应该让我登录并允许访问受保护的路由,例如/role-user。但由于某种原因我无法登录,POST 返回一个303 并将我路由回根页面。

我在 http-handler 函数中添加了 Friend 中间件。这是应用这种中间件的正确位置吗?我想也许 reload 或 api-defaults 中间件可能会弄乱朋友中间件?但是,删除它们并不能解决问题。

(def http-handler
  (if is-dev?
    (-> #'routes
        (reload/wrap-reload)
        (friend/authenticate
          {:allow-anon? true
           :login-uri "/login"
           :default-landing-uri "/"
           :unauthorized-handler #(-> (h/html5 [:h2 "You do not have sufficient privileges to access " (:uri %)])
                                      resp/response
                                      (resp/status 401))
           :credential-fn (fn [x]
                            (let [res (creds/bcrypt-credential-fn @users x)]
                              (log/info x)
                              (log/info res)
                              res))
           :workflows [(workflows/interactive-form)]})
        (wrap-defaults api-defaults))
    (wrap-defaults routes api-defaults)))

根据打印语句,我发现 credential-fn 函数确实在 POST 请求中被调用,具有正确的参数,并且该函数返回正确的(经过身份验证的)结果。

这个http-handler就是这样使用的

(defn run-web-server [& [port]]
  (let [port (Integer. (or port (env :port) 10555))]
    (print "Starting web server on port" port ".\n")
    (run-jetty http-handler {:port port :join? false})))

(defn run-auto-reload [& [port]]
  (auto-reload *ns*)
  (start-figwheel))

(defn run [& [port]]
  (when is-dev?
    (run-auto-reload))
  (run-web-server port))

不管怎样,这是我的路线。

(defroutes routes
  (GET "/" req
    (h/html5
      misc/pretty-head
      (misc/pretty-body
       (misc/github-link req)
       [:h3 "Current Status " [:small "(this will change when you log in/out)"]]
       [:p (if-let [identity (friend/identity req)]
             (apply str "Logged in, with these roles: "
               (-> identity friend/current-authentication :roles))
             "anonymous user")]
       login-form
       [:h3 "Authorization demos"]
       [:ul
        [:li (e/link-to (misc/context-uri req "role-user") "Requires the `user` role")]]
       [:p (e/link-to (misc/context-uri req "logout") "Click here to log out") "."])))
  (GET "/login" req
    (h/html5 misc/pretty-head (misc/pretty-body login-form)))
  (GET "/logout" req
    (friend/logout* (resp/redirect (str (:context req) "/"))))
  (GET "/role-user" req
    (friend/authorize #{::users/user} "You're a user!")))

【问题讨论】:

  • 我想通了。 (wrap api-defaults) 不允许会话,朋友正在尝试使用它们。我应该改用 site-defaults 。请参阅ring middleware 了解更多信息。
  • 将此作为您可以接受的答案发布,因此此问题似乎不再有答案。

标签: authentication clojure middleware ring compojure


【解决方案1】:

我想通了。 (wrap api-defaults) 不允许会话,朋友正在尝试使用它们。我应该改用site-defaults。请参阅ring middleware 了解更多信息。

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2015-12-05
    • 2022-06-13
    • 1970-01-01
    • 2014-06-02
    • 2018-05-25
    相关资源
    最近更新 更多