在 cemerick/Friend 中使用 cookie session 登录
login using cookie session in cemerick/Friend
我正在尝试集成 Friend into my compojure 应用程序。
我实现了两个函数:auth/handle-authentication
和 auth/check-credential
,我在以下代码中使用了它们:
(defroutes app*
(GET "/requires-authentication" req
(friend/authenticated (str "You have successfully authenticated as "
(friend/current-authentication)))))
(defn secured-routes
"Takes a list of unsecure (public) routes
and returns the same routes but with an authentication middleware
(authorization still has to be handled for each route)"
[unsecured-routes]
(-> unsecured-routes
(friend/authenticate
{:allow-anon? true
:unauthenticated-handler auth/handle-authentication
:credential-fn auth/check-credential
:workflows [(workflows/http-basic) ; not sure what to put here
(workflows/interactive-form)]
})))
(def api-middleware-config
"Configure the middlewares of the API"
{:params {:urlencoded true
:multipart true
:nested true
:keywordize true}
:responses {:not-modified-responses true
:absolute-redirects true
:content-types true
:default-charset "utf-8"}})
(defroutes app
;; public routes (they work fine)
(-> (....)
;; authentication required
(-> (secured-routes app*)
(wrap-defaults api-middleware-config)
...)
)
auth/check-credential
检查请求中的基本身份验证字符串。如果正确,returns {:identity username}
。它有效,因为使用 postman/curl 我可以使用基本身份验证并获得 "You have successfully authenticated as ..." 作为响应。
auth/handle-authentication
接收请求并检查 cookie 是否正确。
如果是,那么它 return 是一个映射 {:identity "test"}
。这没有做任何事情,无论我的函数的 return 值如何(我尝试对其进行硬编码)。我错过了什么?
请注意,我的情况可能有点特殊,因为登录是在另一个应用程序中完成的,但是 routes/endpoints 是通过 nginx 共享的,我收到 cookie 并可以从数据库访问会话。检查 cookie 在 auth/handle-authentication
.
中完成
如何从具有有效 cookie 的浏览器中获取 "You have successfully authenticated as ..."
?现在我得到一个 200OK
和 application/octet
流 headers 和一个空的 body (这会触发浏览器中的空文件下载)。我还没有真正理解 :workflow
,或者 alow-anon?
的作用。
谢谢!
我觉得你的方法有点奇怪。您将 auth/handle-authentication
分配给 :unauthorized
,但是分配给 :unauthorized
的处理函数在传入请求无法获得授权时被调用。
如果我对您的理解正确,您需要基本身份验证或由第三方提供的 cookie 进行身份验证,这应该由您的 auth/handle-authentication
方法检查。如果是这样,您需要确保调用此方法作为您分配的内容的一部分 :workflow
(参见 Friend documentation on workflows)。
基本上,我认为您正在寻找:
:workflows [(auth/handle-authentication) (workflows/http-basic)]
Adam Bard 最近在 Easy custom auth in Clojure with Friend 上写了一篇博客 post,我想这可能有助于澄清。
我正在尝试集成 Friend into my compojure 应用程序。
我实现了两个函数:auth/handle-authentication
和 auth/check-credential
,我在以下代码中使用了它们:
(defroutes app*
(GET "/requires-authentication" req
(friend/authenticated (str "You have successfully authenticated as "
(friend/current-authentication)))))
(defn secured-routes
"Takes a list of unsecure (public) routes
and returns the same routes but with an authentication middleware
(authorization still has to be handled for each route)"
[unsecured-routes]
(-> unsecured-routes
(friend/authenticate
{:allow-anon? true
:unauthenticated-handler auth/handle-authentication
:credential-fn auth/check-credential
:workflows [(workflows/http-basic) ; not sure what to put here
(workflows/interactive-form)]
})))
(def api-middleware-config
"Configure the middlewares of the API"
{:params {:urlencoded true
:multipart true
:nested true
:keywordize true}
:responses {:not-modified-responses true
:absolute-redirects true
:content-types true
:default-charset "utf-8"}})
(defroutes app
;; public routes (they work fine)
(-> (....)
;; authentication required
(-> (secured-routes app*)
(wrap-defaults api-middleware-config)
...)
)
auth/check-credential
检查请求中的基本身份验证字符串。如果正确,returns {:identity username}
。它有效,因为使用 postman/curl 我可以使用基本身份验证并获得 "You have successfully authenticated as ..." 作为响应。
auth/handle-authentication
接收请求并检查 cookie 是否正确。
如果是,那么它 return 是一个映射 {:identity "test"}
。这没有做任何事情,无论我的函数的 return 值如何(我尝试对其进行硬编码)。我错过了什么?
请注意,我的情况可能有点特殊,因为登录是在另一个应用程序中完成的,但是 routes/endpoints 是通过 nginx 共享的,我收到 cookie 并可以从数据库访问会话。检查 cookie 在 auth/handle-authentication
.
如何从具有有效 cookie 的浏览器中获取 "You have successfully authenticated as ..."
?现在我得到一个 200OK
和 application/octet
流 headers 和一个空的 body (这会触发浏览器中的空文件下载)。我还没有真正理解 :workflow
,或者 alow-anon?
的作用。
谢谢!
我觉得你的方法有点奇怪。您将 auth/handle-authentication
分配给 :unauthorized
,但是分配给 :unauthorized
的处理函数在传入请求无法获得授权时被调用。
如果我对您的理解正确,您需要基本身份验证或由第三方提供的 cookie 进行身份验证,这应该由您的 auth/handle-authentication
方法检查。如果是这样,您需要确保调用此方法作为您分配的内容的一部分 :workflow
(参见 Friend documentation on workflows)。
基本上,我认为您正在寻找:
:workflows [(auth/handle-authentication) (workflows/http-basic)]
Adam Bard 最近在 Easy custom auth in Clojure with Friend 上写了一篇博客 post,我想这可能有助于澄清。