我有以下设置:
我希望通过将数据提交给后端的处理程序,使存储在该原子中的数据持久。最直接的方法似乎是直接从原子获取数据,并执行以下操作:
(http/post "https://post.here.url"
{:edn-params
@my-atom})但是:默认情况下,Macchiato要求POST请求包含一个防伪造令牌(我目前将它作为元素的一个属性存储;如果这是不好的实践,请告诉我)。因此,我尝试了以下几点:
(http/post "https://post.here.url"
{:edn-params
{:data @my-atom
:__anti-forgery-token "SuperSecretToken"}})但是,这不起作用,因为令牌被拒绝为无效。只有当我将数据声明为:form-params时,才能正确处理防伪令牌:
(http/post "https://post.here.url"
{:form-params
{:data (str @my-atom)
:__anti-forgery-token "SuperSecretToken"}})上面的方法是有效的,但是,当然,MIME类型没有被正确地设置,我必须做一些hula环来使EDN数据在服务器端可用。这种做法看起来确实是错误的。是否有一种方法可以正确地序列化EDN数据并仍然传输防伪令牌?。
我对这些东西还是很陌生的,所以我可能错过了一些基本的东西。一般来说,我是不是弄错了防伪信物的目的?只有在传输表单数据时才有意义(我的数据实际上就是这样;只是直接发送原子会使重新加载存储的数据更加容易)。
非常感谢您给我的任何投入!
奥利弗
发布于 2020-09-29 07:26:14
现在如何设置文档是有点棘手的,但是这里有3条有用的信息:
如果您使用wrap-defaults中间件包装器,那么您可以传入一个选项值,该值是the (启用默认的防伪行为,正如您观察到的那样),或者是一个选项映射,其中1中的文档表示它接受一个可选的:read-token函数。
综合起来,加上3中的rest-middleware,这里有一种方法可以实现反伪造检查,该检查接受默认(表单params)或json有效负载(body-params):
(-> handler
;; note: the wrapped middleware request processing
;; is handled _bottom-up_ in this chain
(defaults/wrap-defaults
(-> defaults/site-defaults
(assoc-in [:security :anti-forgery]
{:read-token (fn [request]
(or
;; check the custom key we set upstream (but in the block below)
(get-in request [:anti-forgery-payload])
;; default behavior
(anti-forgery/default-request-token request)))})))
(middleware/wrap
(fn extract-anti-forgery-payload [handler]
(fn [request respond raise]
(handler
(if-let [anti-forgery-payload
(get-in request [:body "__anti-forgery-token"])]
(-> request
;; add our custom anti-forgery key to the request
(assoc :anti-forgery-payload anti-forgery-payload)
;; remove the special key from the request to downstream handlers
(update :body dissoc "__anti-forgery-token"))
request)
respond raise)))
{})
;; decodes the json input into maps
(macchiato.middleware.restful-format/wrap-restful-format))https://stackoverflow.com/questions/60787614
复制相似问题