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 ...
随机推荐
- JAVA代理方式使用示例总结
JAVA代理方式使用示例总结 一. 代理方式概括 Java的代理方式主要包含了静态代理,动态代理两种方式,其中,动态代理根据实现的方式不同,又可以划分为jdk动态代理和cglib动态代理. 二. ...
- Ubuntu安装配置Python.pyDev
一:安装ECLipse Eclipse官网下载软件tar包: 使用解压命令: sudo tar xzvf xxxxx.tar.gz -c /opt/ 创建快捷方式到桌面. 二:安装python-Num ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
http://www.cnblogs.com/wuhuacong/p/3871991.html 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览 在博客园很多文章 ...
- day03深浅拷贝、文件操作和函数初识
一.赋值.浅拷贝与深拷贝 直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法, ...
- 如何修改wifi为家庭网络
一不小心手快,把新链接的 wifi 选择成“公用网络”了,使用过程中导致某些应用无法联网,那个恨呐!!! 幸好,咱们可以进行手工更改,哈哈,跟哥一起来操作: 进入”网络与共享中心界面": 选 ...
- Centos 搭建activemq
Centos 搭建activemq 1,官方下载 http://activemq.apache.org/activemq-5122-release.html apache-activemq-5.15 ...
- Swagger在 NETcore 中的使用
请参考 https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=asp ...
- CodeForcesGym 100548G The Problem to Slow Down You
The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...
- 线程锁的机制Lock
java.util.concurrent.locks 接口Lock publci interface Lock Lock 实现提供了比使用synchronized方法和语句可获得的更加广泛的锁定操作, ...
- 洛谷 1144 最短路计数 bfs
洛谷1144 最短路计数 传送门 其实这道题目的正解应该是spfa里面加一些处理,,然而,,然而,,既然它是无权图,,那么就直接bfs了,用一个cnt记录一下每一个点的方案数,分几种情况讨论一下转移, ...