Kong Basic Authentication 插件详解

本文详细介绍了Kong API网关中基本认证插件的配置与使用方法,包括插件的属性设置、认证方式、作用域及适用协议。通过示例展示了如何创建消费者、凭证以及如何使用这些凭证进行API调用。

配置信息

  • 基本描述
属性描述
认证方式基本认证、用户名密码登录
插件作用域服务、路由、全局
适用协议http、https
  • 基本属性
属性描述
name插件名称、此处为basic-auth
service_id绑定的服务Id
route_id绑定的路由Id
enabled是否启用该插件,默认是true
config.hide_credentials是否隐藏请求中的凭证信息(如Authorization头),默认是false
config.anonymous在验证失败后是否启用匿名消费者,值可以配置为消费者Id

使用详情

  • 创建消费者
curl -d "username=user123&custom_id=SOME_CUSTOM_ID" http://kong:8001/consumers/
  • 创建凭证
curl -X POST http://kong:8001/consumers/{consumer}/basic-auth --data "username=Aladdin" --data "password=OpenSesame"
  • 使用凭证
curl http://kong:8000/{path matching a configured Route} -H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l'
  • 凭证接口
    查看所有basic-auths凭证信息
curl -X GET http://kong:8001/basic-auths

查看指定basic-auths凭证信息

curl -X GET http://kong:8001/consumers/{username or id}/basic-auths
目前提供的引用内容未提及在SAP SICF RESTful中启用Basic Authentication的方法。不过,一般来说在SAP SICF RESTful中启用Basic Authentication可以按以下常规步骤操作: 1. **激活服务节点**:在事务码 `SICF` 中找到对应的RESTful服务节点,激活该节点。 2. **配置安全设置**:在服务节点的属性中,找到安全相关的配置项。通常可以设置认证类型为 `Basic Authentication`。 3. **定义用户和密码**:可以在SAP系统中维护相应的用户,这些用户将用于Basic Authentication的验证。 4. **代码层面处理**:在ABAP代码中,当接收到请求时,需要对请求头中的 `Authorization` 字段进行解析和验证,提取出Base64编码的用户名和密码,并与系统中维护的用户信息进行比对。 以下是一个简单的ABAP代码示例,用于处理Basic Authentication: ```abap REPORT zrest_basic_auth. CLASS lcl_handler DEFINITION. PUBLIC SECTION. METHODS: handle_request FOR EVENT request OF if_http_extension IMPORTING iv_method = method iv_uri = uri io_request = request io_response = response. ENDCLASS. CLASS lcl_handler IMPLEMENTATION. METHOD handle_request. DATA: lv_auth_header TYPE string, lv_credentials TYPE string, lv_username TYPE string, lv_password TYPE string. " 获取请求头中的Authorization字段 io_request->get_header_field( EXPORTING name = 'Authorization' IMPORTING value = lv_auth_header ). IF lv_auth_header CA 'Basic '. " 提取Base64编码的用户名和密码 lv_credentials = COND #( WHEN lv_auth_header CA 'Basic ' THEN lv_auth_header+6 ). lv_credentials = cl_http_utility=>decode_base64( iv_input = lv_credentials ). " 分割用户名和密码 SPLIT lv_credentials AT ':' INTO lv_username lv_password. " 这里可以添加验证用户名和密码的逻辑 IF lv_username = 'valid_user' AND lv_password = 'valid_password'. io_response->set_status( code = 200 text = 'OK' ). io_response->set_text( 'Authentication successful' ). ELSE. io_response->set_status( code = 401 text = 'Unauthorized' ). io_response->set_header_field( name = 'WWW-Authenticate' value = 'Basic realm="SAP RESTful Service"' ). io_response->set_text( 'Authentication failed' ). ENDIF. ELSE. io_response->set_status( code = 401 text = 'Unauthorized' ). io_response->set_header_field( name = 'WWW-Authenticate' value = 'Basic realm="SAP RESTful Service"' ). io_response->set_text( 'Authentication required' ). ENDIF. ENDMETHOD. ENDCLASS. " 注册处理类 CALL FUNCTION 'SICF_SERVICE_CREATE_HANDLER' EXPORTING service_name = '/default_host/zrest_service' handler_class = 'lcl_handler'. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举个栗子の不容易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值