IdentityServer4【Topic】之保护APIs
2024-08-22 17:39:39
Protecting APIs 保护api
默认情况下IdentityServer将access token发布成JWT(json web token)格式的。
现在,每个相关的平台都支持验证JWT令牌,这里可以找到一个很好的JWT库列表。流行的库如:
- JWT bearer authentication handler for ASP.NET Core
- JWT bearer authentication middleware for Katana
- IdentityServer authentication middleware for Katana
- jsonwebtoken for nodejs
保护基于Asp.net core的api需要做的事情就是在DI中配置jwt bearer authentication handler。并且在管道上面添加authentication中间件:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.Audience = "api1";
});
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseAuthentication();
app.UseMvc();
}
}
The IdentityServer authentication handler
我们的身份验证处理程序(指IdentityServer4中提供的handler)与上面的处理程序(handler)有相同的用途(实际上它在内部使用Microsoft JWT库),但是添加了一些额外的特性:
- support for both JWTs and reference tokens //同时支持JWTs和引用token
- extensible caching for reference tokens//为引用token支持缓存
- unified configuration model//统一了配置模型
- scope validation//范围的检查
对于最简单的场景,我们的handler配置过程看起来和上面的代码片段非常相似:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//api保护是在api里面做的,需要引入IdentityServer4.AccessTokenValidation这个nuget包。比如下面这个参数来自包中
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.ApiName = "api1";
});
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseAuthentication();
app.UseMvc();
}
}
Supporting reference tokens支持引用token
如果进来的令牌不是JWT,我们的中间件将会联系发现文档(discovery document,就是./well-known/openid-configuration端点)中找到的内省端点(introspection endpoint)来验证令牌。由于自省端点需要认证,所以你需要提供配置的API秘密,例如:
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.ApiName = "api1";
options.ApiSecret = "secret";
})
通常,你不希望为每个传入的请求对自省端点进行一次往返。中间件有一个内置的缓存,可以这样启用:
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.ApiName = "api1";
options.ApiSecret = "secret";
options.EnableCaching = true;
options.CacheDuration = TimeSpan.FromMinutes(); // that's the default
})
//处理程序将使用在DI容器中注册的任何i分布式缓存实现(例如标准的memory分布式缓存)。
Validating scopes检查请求的范围
ApiName属性(上面的代码提到的这个属性)检查传过来的token的audience(或者叫做aud)的claim,查看是否匹配。
在IdentityServer中你可以将API继续细分成多个范围。如果你需要这种粒度的分化那么你可以使用ASP.NET Core 中的授权策略(authorization policy system)来检查scope:
- 创建全局的策略(基于MVCOpitons进行配置,添加到过滤器上)
services.AddMvcCore(options =>
{
// require scope1 or scope2。create是扩展方法,在内部
//调用了requireclaim。
var policy = ScopePolicy.Create("scope1", "scope2");
options.Filters.Add(new AuthorizeFilter(policy));
})
.AddJsonFormatters()
.AddAuthorization();
- 构建一个范围策略(基于AuthorizationOptions进行构建)
services.AddAuthorization(options =>
{
options.AddPolicy("myPolicy", builder =>
{
// 来自identityserver的扩展方法
builder.RequireScope("scope1");
// and require scope2 or scope3
builder.RequireScope("scope2", "scope3");
});
});
最新文章
- 基于注解的bean配置
- Enum是如何用的?
- git之三
- jenkins邮件通知功能
- 使用Html5+C#+微信 开发移动端游戏详细教程 :(二)准备工作&;开发环境
- z/os上的tar和gzip
- 使用myeclipse建立maven项目(重要)
- (8)Launcher3客制化之ContentProvider内容提供者,实现其它应用改动数据库更新等操作
- java--多线程之前台幕后
- 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(2)-查询实现
- MySQL 数据库 Query 的优化
- servlet文件上传及下载
- 如何使用微信小程序云函数发送短信验证码
- python 对excel操作用法详解
- 解决Win10 PowerShell无法激活Anaconda环境的问题
- logging模块初识
- sshd_config 配置文件参数详解
- $Django Rest Framework-认证组件,权限组件 知识点回顾choices,on_delete
- ARM中几个典型的汇编指令解析
- 学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator
热门文章
- Spring的jdbc模板2:使用开源的连接池
- Java面试知识点之计算机网络篇(一)
- Thread.interrupt()
- centos7下kubernetes(5。部署kubernetes dashboard)
- C++ 参数传值 与 传引用
- P1705 爱与愁过火(背包)
- day2-安装python以及基本使用
- Spring Security(二十七):Part II. Architecture and Implementation
- Egg入门学习(二)---理解service作用
- JavaEE学习之Spring Security3.x——模拟数据库实现用户,权限,资源的管理