2
我有一个叫做/account
的端点,它提供用户信息(返回html)。如何使用Clojure中的Liberator重定向get方法?
当非授权用户试图访问这个端点我需要能够重定向到login page
但在解放者我发现post-redirect
到目前为止,它仅仅是post methods
。
我还需要重定向get methods
,我该如何做到这一点?
我有一个叫做/account
的端点,它提供用户信息(返回html)。如何使用Clojure中的Liberator重定向get方法?
当非授权用户试图访问这个端点我需要能够重定向到login page
但在解放者我发现post-redirect
到目前为止,它仅仅是post methods
。
我还需要重定向get methods
,我该如何做到这一点?
我发现下面的代码确实的伎俩解决方法:
(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,但我怀疑它它是一个很好的做法与否。
菲利普在我的一篇旧博客文章中发表评论,在那里我也遇到了同样的问题。他写道:“强制重定向get请求时,你需要返回false:exists?当你考虑它时,它实际上是有意义的,然后你可以实现:moving-permanent?或者:moving-temporarily?上下文中的新位置:位置“。所以,你的解决方法实际上是这样做的官方方式。 – schaueho
不需要在函数中包装'false'和'true'。你可以使用文字值true:':exists?假'将起作用。 – ordnungswidrig