Swagger中添加Token验证
1、该连接链接到api中基本的swagge功能:http://www.cnblogs.com/hhhh2010/p/5234016.html
2.在swagger中使用验证(这里使用密码验证模式)http://www.cnblogs.com/WJ--NET/p/7195124.html <---搭建自己的web api验证服务器
3.在项目中会自动生成SwaggerConfig和Startup文档,如果是有验证服务器的话,swagger的配置就不需要在swaggerconfi中配置了,直接在Startup中配置swagger;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | using System; using System.IO; using System.Reflection; using System.Web.Http; using Microsoft.Owin; using Microsoft.Owin.Security.OAuth; using Owin; using Swashbuckle.Application; [assembly: OwinStartup( typeof (LogicServer.ResourceServer.Startup))] namespace LogicServer.ResourceServer { /// <summary> /// The assebmly should use owin middleware and start at running the Startup method. /// </summary> public class Startup { /// <summary> /// Configuration /// </summary> /// <param name="app">app</param> public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); config.EnableSwagger( "docs/{apiVersion}/swagger" , c => { c.SingleApiVersion( "v1" , "昊天企业端接口" ); var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var fileName = Assembly .GetExecutingAssembly() .GetName() .Name + ".XML" ; var commentsFile = Path.Combine(baseDirectory, "bin" , fileName); c.IncludeXmlComments(commentsFile); }).EnableSwaggerUi(c => { var thisAssembly = typeof (SwaggerConfig).Assembly; c.InjectJavaScript(thisAssembly, "LogicServer.bearerAuth.BearerAuth.js" ); //注入js,在js中写入根据用户名和密码获取token然后添加到接口的Http header中 c.DisableValidator(); }); ConfigureOAuth(app); // set api authentication schema. UnityConfig.RegisterComponents(config); // Use unity as ioc container. Global dependency resolver. WebApiConfig.Register(config); // Setup web api route policy. // SwaggerConfig.Register(); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // Use Cors in message handling. app.UseWebApi(config); } private void ConfigureOAuth(IAppBuilder app) { // Token Consumption app.UseOAuthBearerAuthentication( new OAuthBearerAuthenticationOptions { }); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $( function () { var basicAuthUI = '<div class ="input"> 用户名:<input placeholder ="username" id ="input_username" onchange="addAuthorization();" name ="username" type ="text" size ="15"> </ div>' + '<div class ="input"> 密码:<input placeholder ="password" id ="input_password" onchange="addAuthorization();" name ="password" type ="password" size ="20"> </ div>' ; $( '#input_apiKey' ).hide(); $( '#api_selector' ).html(basicAuthUI); }); function addAuthorization() { var username = document.getElementById( "input_username" ).value; var password = document.getElementById( "input_password" ).value; var data = "grant_type=password&username=" + username + "&password=" + password; $.ajax({ url: "http://168.33.162.189:8889/token" , type: "post" , contenttype: 'x-www-form-urlencoded' , data:data, success: function (response) { var bearerToken = 'Bearer ' + response.access_token; console.log(bearerToken); swaggerUi.api.clientAuthorizations.add( 'key' , new SwaggerClient.ApiKeyAuthorization( 'Authorization' , bearerToken, 'header' )); } }); } |
本文作者:WangJunZzz
本文链接:https://www.cnblogs.com/WangJunZzz/p/7284573.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步