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
中初始化的种子数据
作者:Darren
出处:https://www.cnblogs.com/jesn/p/16354805.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步