abp MicroserviceDemo swagger添加 OAuth

abp官方示例中的abp-samples,swagger并没有提供OAuth,这个在我们平时的开发过程中并不太友好,这里记录下在添加swagger OAuth遇到的一些问题,这里我们以IdentityService.Host这个接口为例

  • 1、添加nuget包 Volo.Abp.Swashbuckle

  • 2、ConfigureServices替换AddSwaggerGenAddAbpSwaggerGenWithOAuth

     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,这里的 grantTypesredirectUri这两个字段需要注意下赋值

4、依次运行AuthServer.HostIdentityService.Host,swagger这里的client_idclient_secretAuthServer.Host中初始化的种子数据

作者:Darren

出处:https://www.cnblogs.com/jesn/p/16354805.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   jesn  阅读(403)  评论(1编辑  收藏  举报
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示