abp MicroserviceDemo swagger添加 OAuth
abp官方示例中的abp-samples,swagger并没有提供OAuth,这个在我们平时的开发过程中并不太友好,这里记录下在添加swagger OAuth遇到的一些问题,这里我们以IdentityService.Host这个接口为例
-
1、添加nuget包
Volo.Abp.Swashbuckle -
2、
ConfigureServices替换AddSwaggerGen为AddAbpSwaggerGenWithOAuthcontext.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 invalidgrantTypes、redirectUri这两个字段需要注意下赋值
4、依次运行AuthServer.Host、IdentityService.Host,swagger这里的client_id和client_secret是AuthServer.Host中初始化的种子数据


浙公网安备 33010602011771号