gpt4 book ai didi

clojure - 具有基本身份验证的 Composure + Swagger

转载 作者:行者123 更新时间:2023-12-04 10:31:59 25 4
gpt4 key购买 nike

我正在尝试使用 compojure 和 swagger 以及非常简单的基本身份验证来创建 Web 服务。我希望身份验证是单一的,例如,“mylogin”和“mypassword”用于登录名和密码。到目前为止,我所拥有的是

(ns my-api.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[schema.core :as s]))

(s/defschema Request
{:SomeData s/Str})

(s/defschema Result
{:Results s/Str})

(defn dummy-return [request]
{:Results "This is a dummy return"})

(def app
(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {:info {:title "A testing API"
:description "Compojure Api example"}
:tags [{:name "api", :description "some apis"}]}}}

(context "/api" []
:tags ["api"]
(POST "/API-Example" []
:body [request Request]
:return Result
:summary "I want this to have a fixed basic authentication"
(ok (dummy-return request))))))


它按预期工作。但是我如何为一个固定用户使用基本身份验证制作相同的 API?我来自 python,使用 Flask 做到这一点非常容易。我想知道是否有一个简单而优雅的解决方案而不会过于冗长。

我试图包括一些像 :securityDefinitions {:login {:type "basic" :password "test"}} 这样的东西与 :data 处于同一级别但即使不进行身份验证,我也可以调用 API。

我用 lein new compojure-api my-api 开始了这个项目

谢谢您的帮助!

最佳答案

您可以添加提供基本身份验证的 Ring 处理程序。例如 ring-basic-authentication .

您可以将处理程序全局添加到您的应用程序中,如下所示:

(require '[ring.middleware.basic-authentication :refer [wrap-basic-authentication]])

(defn authenticated? [username pass]
(and (= username "foo")
(= pass "bar")))

(def app
(-> routes
..
(wrap-basic-authentication authenticated?))

关于clojure - 具有基本身份验证的 Composure + Swagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com