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 ...
随机推荐
- hdu 2485 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- 杭电 1002 A + B Problem II【大数相加】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 解题思路:就是把大的数用数组存放起来,像小学的时候用竖式加法来算两个数相加那样算: 反思:思路很 ...
- mindmanager 2018 中文破解版_注册码_免激活
MindManager 2018是一个可视化的工具,可以用在脑力风暴(brainstorm)和计划(plan)当中.为商务人士提供更有效的.电子化手段捕捉.组织和联系信息(information)和想 ...
- 小白学习Spark系列三:RDD常用方法总结
上一节简单介绍了Spark的基本原理以及如何调用spark进行打包一个独立应用,那么这节我们来学习下在spark中如何编程,同样先抛出以下几个问题. Spark支持的数据集,如何理解? Spark编程 ...
- HTTPie:一个不错的 HTTP 命令行客户端
转自:http://top.jobbole.com/9682/ HTTPie:一个不错的 HTTP 命令行客户端 HTTPie (读aych-tee-tee-pie)是一个 HTTP 的命令行客户端. ...
- 工作中常见的Git本地分支与远程分支同步场景
Git 是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理. 一直以来本人使用 Git 处理分支都是现用现查,一是因为怕出错,二还是因为懒,作为一名四年开发经验的前端 ...
- nyoj303-序号交换
序号互换 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字编号 ...
- HDU 1215
由算术基本定理, 直接使用公式就好 #include <iostream> #include <cstdio> #include <algorithm> #incl ...
- Apache Kylin高级部分之使用Hive视图
本章节我们将介绍为什么须要在Kylin创建Cube过程中使用Hive视图.而假设使用Hive视图.能够带来什么优点.解决什么样的问题.以及须要学会怎样使用视图.使用视图有什么限制等等. 1. ...
- 《C++编程思想》第四章 初始化与清除(原书代码+习题+解答)
相关代码: 1. #include <stdio.h> class tree { int height; public: tree(int initialHeight); ~tree(); ...