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;
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
{
});
}
}
}
$(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'));
}
});
}


参考文档链接:http://www.jianshu.com/p/3329b4126886
Swagger中添加Token验证的更多相关文章
- 用Retrofit发送请求中添加身份验证
用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...
- webapi中使用token验证(JWT验证)
本文介绍如何在webapi中使用JWT验证 准备 安装JWT安装包 System.IdentityModel.Tokens.Jwt 你的前端api登录请求的方法,参考 axios.get(" ...
- Django中csrf token验证原理
我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...
- c# asp.net 中使用token验证
基于token的鉴权机制类似于http协议也是无状态的,它不需要在服务端去保留用户的认证信息或者会话信息.这就意味着基于token认证机制的应用不需要去考虑用户在哪一台服务器登录了,这就为应用的扩展提 ...
- swagger请求参数在header中添加token
网友大部分说的是如下配置 参照配置然而没有作用 注掉改红框内的配置,在方法上加如下注释就可以用 @ApiImplicitParams({ @ApiImplicitParam(paramType = & ...
- Vue中实现token验证
前后端流程分析 前端页面进行登录操作,将用户名和密码发给服务器 服务器进行校验,通过后生成token,包含信息有密匙.uid.过期时间等,然后返回给前端 前端将token保存在本地(建议在localS ...
- adding validation annotators to model classes 在linq to EntityFrame的Model中添加前台验证validation annotators
The same solution can be applied for LINQ to SQL. The snippet the article shows for using the Metada ...
- Asp.Net Core 3.1 学习3、Web Api 中基于JWT的token验证及Swagger使用
1.初始JWT 1.1.JWT原理 JWT(JSON Web Token)是目前最流行的跨域身份验证解决方案,他的优势就在于服务器不用存token便于分布式开发,给APP提供数据用于前后端分离的项目. ...
- [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则
目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...
随机推荐
- 团体程序设计天梯赛-练习集-L1-024. 后天
L1-024. 后天 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就是星期一.我们用数字1到7对应星期一到星期日.给定某一天,请你输出那天的“后天”是星期几. 输入格式: 输入第一行给出一 ...
- Django02 Django基础知识
一.内容回顾 1.web应用程序 2.HTTP协议 a.http协议特性 b.http请求格式 c.http响应格式 3.wsgiref模块 4.Django下载与简单应用 a.Django简介(MT ...
- 【leecode】小练习(简单8题)
def twoSum(nums, target): """ 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[ ...
- elasticsearch聚合函数
计算每个tag下的商品数量 GET /ecommerce/product/_search { "aggs": { //聚合 "group_by_tags": ...
- Python笔记23------Python统计列表中的重复项出现的次数的方法
https://www.cnblogs.com/hester/p/6197449.html
- android的listview的addheaderView总是出现空指针的错误
android的listview的addheaderView总是出现空指针的错误, 网上的处理方法如下: // This doesn't work... nullPointerException Li ...
- Python 安装 numpy 以及 matplotlib 的过程
系统:ubuntu 16.04 版本:Python3.5 步骤: 安装 pip sudo apt install python3-pip 查看 pip list 是否有 numpy 以及 matplo ...
- soapui测试接口使用步骤
1.新建项目 2. 定义接口 url输入接口 3.新建测试集 选择项目,右键 4.在测试集下新建测试用例 5.在测试步骤中导入要测试的请求 6.run
- MySQL 的各种查询列子
一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组).order by(排序).limit(限制结果数) 1.whe ...
- [ES6] Proxy & Reflect
Proxy and Reflect API works nicely together. About how to use Proxy, check this post. Let's see abou ...