abp MicroserviceDemo swagger添加 OAuth
abp
官方示例中的abp-samples
,swagger并没有提供OAuth
,这个在我们平时的开发过程中并不太友好,这里记录下在添加swagger OAuth
遇到的一些问题,这里我们以IdentityService.Host
这个接口为例
-
1、添加nuget包
Volo.Abp.Swashbuckle
-
2、
ConfigureServices
替换AddSwaggerGen
为AddAbpSwaggerGenWithOAuth
context.Services.AddAbpSwaggerGenWithOAuth( configuration["AuthServer:Authority"], new Dictionary<string, string> { { "IdentityService","IdentityService API"} }, options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "Identity Service API", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); });
-
3、AuthServer.Host 添加种子数据,在
AuthServerDataSeeder.cs
中的CreateClientsAsync
方法下添加identity.Host的swagger初始化数据await CreateClientAsync( name: "swagger-identity-client", scopes: new[] { "IdentityService", "InternalGateway", "TenantManagementService" }, grantTypes: new List<string> { "authorization_code"}, secret: commonSecret.Sha256(), redirectUri: $"https://localhost:44368/swagger/oauth2-redirect.html",
这里需要注意一点:commonSecret这里官方是没有进行
,这里的SHA256
加密,我已经向官方Demo提交了pull request**,经过沟通这里的commonSecret是已经加密过的字符串**
,如果没有加密的话,在后续的操作中会提示client secret invalid
grantTypes
、redirectUri
这两个字段需要注意下赋值
4、依次运行AuthServer.Host
、IdentityService.Host
,swagger这里的client_id
和client_secret
是AuthServer.Host
中初始化的种子数据