过滤webservice的请求日志,做权限验证功能等。

1.

namespace WebApplication1
{
public class SimpleWSInvokeMonitorExtension : SoapExtension
{
Stopwatch stopWatch = null;
string startLoginfo = ""; public override Stream ChainStream(Stream stream)
{
return stream;
}
public override object GetInitializer(Type serviceType)
{
//throw new NotImplementedException();
return null;
} public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
//throw new NotImplementedException();
return null;
} public override void Initialize(object initializer)
{
//throw new NotImplementedException();
} public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
stopWatch.Stop();
var sec = stopWatch.ElapsedMilliseconds;
string endLogInfo = string.Format("{0},总花费时间{1}ms,请求ip{2}",
startLoginfo,
stopWatch.ElapsedMilliseconds.ToString(),
GetClientIp()); break;
case SoapMessageStage.BeforeDeserialize:
break;
//about to call method;
case SoapMessageStage.AfterDeserialize:
CertficateSoap(message);
string soapMessage = string.Empty;
var stream = message.Stream;
// Just making sure again that we have got a stream which we
// can read from AND after reading reset its position
//------------------------------------------------------------
if (stream.CanRead && stream.CanSeek && stream.Length < * * )
{
stream.Position = ;
StreamReader rdr = new StreamReader(stream);
soapMessage = rdr.ReadToEnd(); // IMPORTANT!! - Set the position back to zero on the original
// stream so that HTTP pipeline can now process it
//------------------------------------------------------------
stream.Position = ;
}
startLoginfo = GetStartLogInfo(soapMessage, message.MethodInfo.Name);
//采集时间
stopWatch = new Stopwatch();
stopWatch.Start();
break;
} }
/// <summary>
/// 权限验证
/// </summary>
/// <param name="message"></param>
public void CertficateSoap(SoapMessage message)
{
if (message.MethodInfo.CustomAttributeProvider.IsDefined(typeof(AllAnonymous), false))
return; bool check = false;
foreach (SoapHeader header in message.Headers)
{
if (header is CertficateSoapHeader)
{
CertficateSoapHeader myHeader = (CertficateSoapHeader)header; if (myHeader.UserName == null || myHeader.PassWord == null)
{
break;
} if (myHeader.UserName.Equals("LY") && myHeader.PassWord.Equals("LY"))
{
check = true;
break;
}
}
} if (!check)
{
throw new SoapHeaderException(string.Format("认证失败{0}", message.MethodInfo.Name), SoapException.ClientFaultCode);
}
}
public string GetStartLogInfo(string soapMessage, string methodName)
{
XDocument doc = XDocument.Parse(soapMessage);
var body = doc.Descendants().Where(p => p.Name.LocalName == methodName).First();
StringBuilder sb = new StringBuilder();
foreach (XElement el in body.Nodes())
{
sb.Append(string.Format("{0}:{1},", el.Name.LocalName, el.Value));
}
string strLog = string.Format("外部系统请求方法:开始时间{0},方法{1},参数{2}", DateTime.Now.ToString(), methodName, sb.ToString());
return strLog;
}
private static string GetClientIp(string ip = null)
{
if (String.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
if (String.IsNullOrEmpty(ip) || ip.Equals("unknown", StringComparison.OrdinalIgnoreCase))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (ip == "::1")
ip = "127.0.0.1";
return ip;
}
}
}

2.在服务端的公开方法增加特性

 /// <summary>
/// BotWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[ExtensionAttribute]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService] public class BotWebService : System.Web.Services.WebService
{
public CertficateSoapHeader soapHeader;
[WebMethod(Description = "")]
[SoapHeader("soapHeader", Direction = SoapHeaderDirection.In)]
//[AllAnonymous] 有特性表示不需要验证权限,没就需要
public string GetString(string name,int age,byte[] remark,bool isfalg)
{
return name + age;
}
}

3.

    /// <summary>
/// 该特性表示该方法不需要验证调用者的信息
/// </summary>
[AttributeUsage(AttributeTargets.Method,AllowMultiple = true,Inherited = false)]
public class AllAnonymous:Attribute
{ }

4.

namespace WebApplication1
{
/// <summary>
/// 用于webservice认证
/// </summary>
public class CertficateSoapHeader : SoapHeader
{
/// <summary>
/// 属性
/// </summary>
public string UserName { get; set; }
public string PassWord { get; set; } public CertficateSoapHeader() { }
/// <summary>
/// 构造函数认证
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="passWord">密码</param>
public CertficateSoapHeader(string userName, string passWord)
{
this.UserName = userName;
this.PassWord = passWord;
}
}
}

5.client

class Program
{
static void Main(string[] args)
{
localhost.BotWebService s = new localhost.BotWebService();
localhost.CertficateSoapHeader header = new localhost.CertficateSoapHeader();
header.UserName = "LY";
header.PassWord = "LY"; s.CertficateSoapHeaderValue = header;
var by = Encoding.UTF8.GetBytes("你好啊,ewqeqwewq");
//Console.WriteLine(s.HelloWorld()); Console.WriteLine(s.GetString("", ,by,true)); Console.ReadKey();
}
}
}

6.web.config配置

 <system.web>
<webServices>
<soapExtensionTypes>
<add type="WebApplication1.SimpleWSInvokeMonitorExtension,WebApplication1" priority=""/>
</soapExtensionTypes>
</webServices>
</system.web>

实现webservice过滤器,请求日志和权限等的更多相关文章

  1. cxf client在后台不通且chunk设置为false的时候不能在控制台输出请求日志

    场景: 服务编排框架支持编排webservice服务.call webservice的client是基于cxf做的.为了使用服务编排的开发者调试与定位问题方便,需要将webservice的请求与响应报 ...

  2. Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  3. spring boot aop 自定义注解 实现 日志检验 权限过滤

    核心代码: package com.tran.demo.aspect; import java.lang.reflect.Method; import java.time.LocalDateTime; ...

  4. 46. Spring Boot中使用AOP统一处理Web请求日志

    在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...

  5. 异常详细信息: System.Security.SecurityException: 未找到源,不过,未能搜索部分或所有事件日志。 若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的。 不可访问的日志: Security。

    “/”应用程序中的服务器错误. 安全性异常 说明: 应用程序尝试执行安全策略不允许的操作.要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别. 异常详细信息: Sy ...

  6. 如何从Serilog请求日志记录中排除健康检查终结点

    这是在ASP.NET Core 3.X中使用Serilog.AspNetCore系列文章的第四篇文章:. 第1部分-使用Serilog RequestLogging减少日志详细程度 第2部分-使用Se ...

  7. 用SignalR实现实时查看WebAPI请求日志

    实现的原理比较直接,定义一个MessageHandler记录WebAPI的请求记录,然后将这些请求日志推送到客户端,客户端就是一个查看日志的页面,实时将请求日志展示在页面中. 这个例子的目的是演示如何 ...

  8. 其他信息: 未找到源,不过,未能搜索部分或所有事件日志。 若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的。 不可访问的日志: Security。

    其他信息: 未找到源,不过,未能搜索部分或所有事件日志.  若要创建源,您需要用于读取所有事件日志的权限以确保新的源名称是唯一的.  不可访问的日志: Security. System.Diagnos ...

  9. nginx记录响应与POST请求日志

    生产环境中的某些api出现故障,但是问题无法重现,但是又很想解决掉问题以及我们新项目上线,需要跟踪请求与响应的信息,可以预先找到一些bug,减少大面积的损失. 安装nginx与ngx_lua 响应日志 ...

随机推荐

  1. hash(散列函数)

    一直对哈希不太理解,今天上网搜了一下,总结出以下几点,希望可以对大家的理解有所帮助 1)概念 哈希就是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列 ...

  2. 论文阅读笔记(七)YOLO

    You Only Look Once: Unified, Real-Time Object Detection Joseph Redmon, CVPR, 2016 1. 之前的目标检测工作将分类器用作 ...

  3. 论文笔记:Mask R-CNN

    之前在一次组会上,师弟诉苦说他用 UNet 处理一个病灶分割的任务,但效果极差,我看了他的数据后发现,那些病灶区域比起整张图而言非常的小,而 UNet 采用的损失函数通常是逐像素的分类损失,如此一来, ...

  4. anylogic 使用

    1.智能体群的用法有人会问:请问怎么给生成的两个对象赋予属性,比如在分叉的时候一个进入sink1,另一个进入sink2?我想source生成不同的实体,而且各个实体都有不同的属性,请问应该怎么设置呢? ...

  5. python(random模块)取10以内的随机数

    上面有个selenium-webdriver循环点击百度搜索结果以及获取新页面的handler文章,随机获取百度搜索结果中不同id的结果,实现代码如下: #coding:utf- import ran ...

  6. Numpy 多维数组简介

     NumPy是一个功能强大的Python库,主要用于对多维数组执行计算.NumPy这个词来源于两个单词-- Numerical和Python.NumPy提供了大量的库函数和操作,可以帮助程序员轻松地 ...

  7. pwnable.tw dubblesort

    (留坑,远程没打成功) int __cdecl main(int argc, const char **argv, const char **envp) { int t_num_count; // e ...

  8. layUI 实现自定义弹窗

    需求描述:点击表格中的数据,弹出一张具体信息表.描述的不是很清楚,放效果图,就明白了,上图 放心,能看到的数据,都不是生产数据,我造的假数据,但是功能效果就是这样,点击列表中的一行,弹出某些要展示的信 ...

  9. Spring出现事务代理的原因

    JdbcTemplate 在配置事务代理之前,JdbcTemplate 的关闭策略,就是操作完立刻关闭!意味着,默认情况是JdbcTemplate操作是不支持事务的!!! 但是我们的程序是需要支持事务 ...

  10. day20包

    https://www.cnblogs.com/Eva-J/articles/7292109.html 一.模块: 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名 ...