Ids4 的使用说明
ids4 全称:IdentityServer4 是专门为 。net core 而生产了一个中间件,目前是为了数据安全 做的鉴权中心
第一步:创建一个空的core api 项目:当然你要健 core mvc 也可以了,但是没有必要,
第二步:引用 IdentityServer4 当前我引用的是2.2版本的,这个后面如果有更新最新的,就引用最新的应该没有问题。
第三步:添加中间件
需要注意一点:ConfigureServices里面添加的是:
services
.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
Configure方法里面添加的是,
app.UseIdentityServer();
其他的代码跟ids4无关
需要注意的是services 里面的Config 是需要自己定义的类,然后在里面实例化 ids4的参数
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();services
.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseIdentityServer();if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
第四步 Config类的创建 这里整个类都复制出来,直接生产一个文件就可以使用了。
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace IdentityServer
{
public class Config
{
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>()
{
new ApiResource("api1", "My Api")
};
}public static IEnumerable<Client> GetClients()
{
return new List<Client>()
{
new Client()
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes =
{
"api1"
}
}
};
}
}
}
第五步:启动鉴权中心服务
该项目端口号自定义设置为 6000。所以访问是:http://localhost:6000/connect/token
注意: connect/token 这个是固定不变的,是 ids4最近的方法,不是开发自定义的,所以只需要直接去用就可以了
主要需要注意 他的调用方式 跟参数
1:post方式调用
2:参数 client_id client_secret grant_type
3:参数值:上面的参数 对应的值分别是 : 记得上面配置了config,这3个值就是上面的config里面配置的,
client_id 就是 ClientId ,但是在postman里面必须用 client_id 对应的值就是 client 了,这个自己设置
client_secret 这个就是ClientSecrets 对应的值就是自定义的: secret
grant_type 就是一个枚举了,AllowedGrantTypes 对应的值是 client_credentials 。
虽然枚举是:GrantTypes.ClientCredentials,但是 传的实际值是 client_credentials
调用接口如图:
以上就是整个鉴权中心服务,我们知道怎么获取 token了,但是不知道怎么用?
第二块 。因为ids4是专门为 core而生的,所以他也同时继承到了ocelot网关里面了。
第六步:新建一个api 做 ----关网 在nuget里面先引用 ocelot ,同时也引用下 IdentityServer4.AccessTokenValidation
如图:
第七步:在startup类中添加
这里中间件中加入 ocelot ,ocelot的配置 跟使用这里就不说了,默认你会,不是本章说的内容,如果不会单独去看 ocelot的文档。 这里需要添加 :
在ConfigureServices方法中添加
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication("TestKey", options =>
{
options.Authority = "http://localhost:6000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
跟
Configure类中添加
app.UseAuthentication();
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();services.AddOcelot(new ConfigurationBuilder()
.AddJsonFile("configuration.json")
.Build());services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication("TestKey", options =>
{
options.Authority = "http://localhost:6000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}await app.UseOcelot();
app.UseAuthentication();
app.UseMvc();
}
然后在网关的配置文件里面去添加 继承 ids4的配置:
例如: 这里大部分都是ocelot的配置,上游地址配置,下游地址配置
其中
"AuthenticationOptions": {
"AuthenticationProviderKey": "TestKey",
"AllowScopes": []
}
这个就是配置鉴权中心ids4的。所以只需要配置这个就可以了
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"UpstreamPathTemplate": "/webapia/values",
"UpstreamHttpMethod": [ "Get" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "TestKey",
"AllowScopes": []
}
},
第八步:启动网关服务
第九步:启动一个服务A,这个服务A就是网关里面的配置上写的 下有地址。这个要对上,
第十步:调式:
不走网关直接访问服务:
走网关 访问该服务:记住了走网关的时候,需要做权健校验的,
所以需要在authorization里面添加 通过http://localhost:6000/connect/token 获取的 access_token值
在Type里面选择 Oauth2.0 就有一个需要让你输入的地址,输入这串密文。就可以,就可以调用,
如果密文为空或者 不对,你可以试试,肯定是调不通接口,拿不到你需要的值。