用bidi包装资源处理程序 [英] Wrapping resource handlers with bidi
问题描述
如何使用朋友和比迪包装资源处理程序?
How can I use friend and bidi to wrap resource handlers?
我已经成功地使oAuth验证了路由:
I've succeeded in getting oAuth to authenticate a route:
(defn auth-handler [request] (friend/authorize #{::user}
{:status 200
:body "a secret"}))
(def routes ["/" {true auth-handler}])
(def app (make-handler routes))
(web/run-dmc (-> app
var
(friend/authenticate
{:allow-anon? true
:workflows [(oauth/workflow
{:client-config client-config
:uri-config uri-config
:credential-fn credential-fn})]})
(wrap-resource "public")
(wrap-defaults site-defaults)
))
这适用于'/'路线,但我想确保某些资源可以
This works on the '/' route, but I would like to make sure some resources can not be reached without authing first.
这似乎可以通过 friend / wrap-authorize
函数实现:
That seems to be possible with the friend/wrap-authorize
function:
我最接近的尝试在auth封装的路由上起作用,但在非/ dev /路由上不匹配:
My closest attempt works on the auth wrapped routes, but does not match on the non /dev/ routes:
(def routes ["/" [["dev/" [[true (friend/wrap-authorize (resources {:prefix "dev/"}) #{::user})]]]
[true (resources {:prefix "public/"})]]])
(match-route routes "/dev/index.html")
=>
{:handler #object[cemerick.friend$wrap_authorize$fn__24411
0x2400d0be
"cemerick.friend$wrap_authorize$fn__24411@2400d0be"]}
;correct
(match-route routes "/index.html")
=>
nil
;not correct
我认为我是路线的匹配部分模式 [true(资源{:prefix public /}))]
是错误的,因为当我将其更改为:key $ c $时c> index.html确实匹配。
I think I the Matched part for the Route Pattern [true (resources {:prefix "public/"})]
is wrong because when I change it to a :key
the `index.html' does match.
如何将非/ dev / *路由与公共资源匹配?
How can I match non /dev/* routes to the public resources?
推荐答案
这里的主要问题是资源路由应该是
The main problem here is that the resource route should be
["" (resources {:prefix "public/"})]
空字符串而不是 true
。
文档确实指出:模式匹配后,路径的其余部分被添加到
The documentation does state: After the pattern is matched, the remaining part of the path is added to the given prefix.
但是坦率地说,我认为这是非常令人惊讶的行为。
But frankly I consider this to be pretty surprising behavior.
我做了一个最小的示例项目在这里成功路由/index.html
https://github.com/timothypratley/bidi-r esources
I made a minimal example project here that successfully routes /index.html https://github.com/timothypratley/bidi-resources
值得注意的是,请求/index.html2会导致异常,这完全不是我所期望的。我期待的是404。o_O
Notably, requesting /index.html2 results in an exception, which is again not what I expected at all. I was expecting a 404. o_O
我真的很喜欢ClojureScript中的bidi,但到目前为止,我发现它在服务器端是一个艰难的口号……我得到了弄清楚为什么 true
不起作用的原因是,用我自己的版本覆盖资源定义,该版本打印出输入内容,并看到:remainder
为空。
I really like bidi in ClojureScript, but so far am finding it a tough slog on the server side... The way I got a clue into why true
doesn't work is by overwriting the Resources definition with my own version that printed out the inputs, and saw that :remainder
was empty.
这篇关于用bidi包装资源处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!