using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
namespace TestRealProxy
{ public class SqlVerifyProxy : RealProxy
{
MarshalByRefObject _target = null;
public SqlVerifyProxy(Type type, MarshalByRefObject target)
: base(type)
{
this._target = target;
}
//覆写Invoke,处理RealProxy截获的各种消息,
//此种方式最简捷,但不能截获远程对象的激活,好在我们并不是真的要Remoting
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage call = (IMethodCallMessage)msg;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
IMethodReturnMessage back = null;
//构造函数,只有ContextBoundObject(Inherit from MarshalByRefObject)对象才能截获构造函数
if (ctr != null)
{
RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
//如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
//这样的话,会导致没有直正构建对象。
defaultProxy.InitializeServerObject(ctr);
//本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, (MarshalByRefObject)GetTransparentProxy());
}
//MarshalByRefObject对象就可截获普通的调用消息,
//MarshalByRefObject对象告诉编译器,不能将其内部简单的成员函数优化成内联代码,
//这样才能保证函数调用都能截获。
else
{ IDictionary<string, object> dic = new Dictionary<string, object>(); //过滤参数
for (int i = ; i < call.Args.Length;i++ )
{
if (call.Args[i].ToString().Contains(""))
call.Args[i] = call.Args[i].ToString().Replace("", "123--");
}
//dic = actionContext.ActionArguments;
//if (dic != null && dic.Count > 0)
//{
// foreach (var m in dic)
// {
// string o = m.Value;//.ToJson();
// // Utils.Filter(o);
// }
//} System.IO.File.AppendAllText(System.Web.HttpContext.Current.Server.MapPath("~/1.txt"), string.Join(",", call.Args) + "=================");
back = RemotingServices.ExecuteMessage(_target, call);
}
return back;
}
} //从ProxyAttribute继承,自动实现RealProxy植入
[AttributeUsage(AttributeTargets.Class)]
class SqlVerifyProxyAttribute : ProxyAttribute
{
//覆写CreateInstance函数,返回我们自建的代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject obj = base.CreateInstance(serverType);
SqlVerifyProxy proxy = new SqlVerifyProxy(serverType, obj);
return (MarshalByRefObject)proxy.GetTransparentProxy();
}
} /// <summary>
/// 业务代码,需要继承 ContextBoundObject ,凡是调用 Test方法下的方法都会AOP
/// </summary>
[SqlVerifyProxy]
public class Test : ContextBoundObject
{ public void say(string msg)
{ } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestRealProxy; namespace TestRealProxy2.Controllers
{ [HandleError]
public class HomeController : Controller
{ public ActionResult Index(string id)
{
//只要调用Test了类下的方法,都会过滤参数
Test t = new Test();
t.say(id);
return View();
} public ActionResult About()
{
return View();
}
}
}

RealProxy AOP过滤方法的参数的更多相关文章

  1. Spring AOP获取方法的参数名称和参数值

    aop配置: <aop:aspectj-autoproxy expose-proxy="true" /> @Before(value = "execution ...

  2. 动态代理AOP实现方法过滤

    上一节实现了动态代理,接下来 有时候,我不需要在每一个方法都要记录日志,做权限验证 等等. 所有就有了这样的需求.AOP实现特定方法过滤,有选择性的来对方法实现AOP 拦截.就是本节标题所示. 举个例 ...

  3. 使用Spring Aop验证方法参数是否合法

    先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.ann; import java.lang.annot ...

  4. Spring AOP中使用args表达式访问目标方法的参数

    Spring AOP 的使用过程理解 首先,aop的使用场景介绍: 1.处理一些通用的非功能性的需求,不影响业务流程,比如说打印日志.性能统计.推送消息等: 2.aop无法拦截static.final ...

  5. js进阶 11-15 jquery过滤方法有哪些

    js进阶 11-15  jquery过滤方法有哪些 一.总结 一句话总结:jquery方法中的参数一般是什么:选择器.元素或 jQuery 对象. 1.jquery方法中的参数一般是什么? 选择器.元 ...

  6. 2. Bean Validation声明式校验方法的参数、返回值

    你必须非常努力,才能干起来毫不费力.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众 ...

  7. js--数组的filter()过滤方法的使用

    前言 你还在通过for循环遍历数组吗?你还在遍历之后一项一项的通过if判断过滤你需要的数据吗?你还在写着一大堆代码实现一个简单的过滤数据功能吗?那么,今天他来了.他就是这里要介绍的es6中数组filt ...

  8. Java:方法的参数是传值还是传引用

    Java中方法的参数总是采用传值的方式. 下列方法欲实现对象的交换,但实际上是不能实现的. public void swap(simpleClass a,simpleClass b){ simpleC ...

  9. MVC学习系列2--向Action方法传递参数

    首先,新建一个web项目,新建一个Home控制器,默认的代码如下: public class HomeController : Controller { // GET: Home public Act ...

随机推荐

  1. Web常用方法

    1.返回一个json格式报文 /**     * 返回json格式字符串或普通字符串     *      * @param jsonString     */    protected void w ...

  2. Git&GitHun 命令合集

    Git&GitHun 命令合集 基本操作 git --version 查看git版本信息 git add 本地库初始化 设置签名 git config user.name xxx git co ...

  3. JSK 11: 移除数组中的重复元素

    题目描述 给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 $A = \{1, 1, 2\}$,你的程序应该输出 $2$ 即新数组的长度,新数组为 $\{1, 2\}$. 要求 ...

  4. RandomeAccessFile - read write

    RandomeAccessFile use write replace writeBytes public class RandomAccessFileTest { public static voi ...

  5. 信息批量提取工具bulk-extractor

    信息批量提取工具bulk-extractor   在数字取证中,通常需要面对海量的数据,如几百GB甚至TB级别的数据.从这些海量数据中,提取有价值的数据是一个漫长.枯燥.繁琐的过程.Kali Linu ...

  6. 【bzoj2839】【集合计数】容斥原理+线性求阶乘逆元小技巧

    (上不了p站我要死了,侵权度娘背锅) Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取 ...

  7. sqlserver日志文件缩小

    原文:sqlserver日志文件缩小        最近装了个500g的固态硬盘,导入我原来的数据库后发现有60多个G的内存不见了, 最后发现我的某个数据库有60多个G的日志文件(.ldf文件)文件, ...

  8. redis--AOF

    Redis 分别提供了 RDB 和 AOF 两种持久化机制: RDB 将数据库的快照( snapshot)以二进制的方式保存到磁盘中. 相当于MySQL binlog 的 raw模式 AOF 则以协议 ...

  9. The Art of Mocking

    One of the challenges developers face when writing unit tests is how to handle external dependencies ...

  10. [置顶] kubernetes资源类型--Volume

    在Docker的设计实现中,容器中的数据是临时的,即当容器被销毁时,其中的数据将会丢失.如果需要持久化数据,需要使用Docker数据卷挂载宿主机上的文件或者目录到容器中.在K8S中,当Pod重建的时候 ...