clojure - 带有基本身份验证的 Composure + Swagger
问题描述
我正在尝试通过非常简单的基本身份验证使用 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?))
推荐阅读
- node.js - 我正在尝试安装 npm 包,但它不起作用。错误:EPERM:不允许操作,mkdir 'C:\Users\HAMZA~1'
- loops - 如何传递具有不同参数的场景大纲?
- android - BottomsheetDialog 未设置样式或本地化
- android - 无法在 android studio 中创建资产文件夹
- c# - selenium webdriver 错误抛出一个带有空响应的异常,向远程 WebDriver 服务器发送 HTTP 请求以获取 URL
- c++ - 如何配置 clang-format 以采用 UTF-8 字符的长度,而不是其实际字节长度?
- reactjs - Adding new property to state and setting state and then destructing the state object i am unable to get the property added
- excel - Execute both custom function and commands function in a shared runtime using office js
- javascript - 如何使用 Firestore 中的数据在 React 中创建一组唯一字段值?
- amazon-web-services - Amplify publish doesn't remove files from S3