2017-04-07 22 views
2

我有一个叫做/account的端点,它提供用户信息(返回html)。如何使用Clojure中的Liberator重定向get方法?

当非授权用户试图访问这个端点我需要能够重定向到login page但在解放者我发现post-redirect到目前为止,它仅仅是post methods

我还需要重定向get methods,我该如何做到这一点?

回答

3

我发现下面的代码确实的伎俩解决方法:

(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,但我怀疑它它是一个很好的做法与否。

+2

菲利普在我的一篇旧博客文章中发表评论,在那里我也遇到了同样的问题。他写道:“强制重定向get请求时,你需要返回false:exists?当你考虑它时,它实际上是有意义的,然后你可以实现:moving-permanent?或者:moving-temporarily?上下文中的新位置:位置“。所以,你的解决方法实际上是这样做的官方方式。 – schaueho

+1

不需要在函数中包装'false'和'true'。你可以使用文字值true:':exists?假'将起作用。 – ordnungswidrig