如何在Clojure中使用Liberator重定向get方法?
我有一个名为如何在Clojure中使用Liberator重定向get方法?,clojure,liberator,Clojure,Liberator,我有一个名为/account的端点,它提供用户信息(返回html) 当未经授权的用户试图访问此端点时,我需要能够重定向到登录页面,但在解放者中,我找到了post redirect,它只适用于post方法 我还需要重定向get方法,如何实现这一点?我找到了一个解决方法,以下代码实现了这一点: (defn account [] (resource :allowed-methods [:get] :available-media-types ["text/html"]
/account
的端点,它提供用户信息(返回html)
当未经授权的用户试图访问此端点时,我需要能够重定向到登录页面
,但在解放者中,我找到了post redirect
,它只适用于post方法
我还需要重定向
get方法
,如何实现这一点?我找到了一个解决方法,以下代码实现了这一点:
(defn account
[]
(resource :allowed-methods [:get]
:available-media-types ["text/html"]
:exists? (fn [_] false)
:existed? (fn [_] true)
:moved-temporarily? (fn [ctx] {:location "/redirected-path-or-url"})
:handle-ok (fn [ctx]
[:html ...])
:handle-exception (fn [_]
"Something went wrong")))
或者你可以检查
:authorized?
并从:handle unauthorized
返回登录html,但我怀疑这是否是一个好的做法。Philipp评论了我的一篇老博文,我也遇到了同样的问题。他写道:“要在get请求上强制重定向,您需要返回false from:exists?当您考虑它时,它实际上是有意义的。然后您可以实现:永久移动?或:临时移动?并在:location的上下文中关联新位置。”。因此,您的解决方法实际上是实现这一点的正式方法。无需在函数中包装false
和true
。您可以使用文本值true::存在?false
将起作用。