做项目不得不考虑安全问题,但是在Remoting应用中我似乎没有找到象WebService那样现成可用的防止非授权人员随意调用的方法和验证安全机制。当然,简单一点的可以通过Remoting客户端传输一些验证代码,然后在服务端验证这些代码的合法性,以实现是否安全授权调用,但是总觉得这样比较麻烦。我也见有些人主动把客户端的IP地址获取后主动送入到Remotin g服务端验证以便识别是否合法,其实原理跟上面的是一样的道理,我觉得这样传送很容易让别人仿造一些数据送入,从而轻易获得合法调用。当然以上数据可以通过把数据特殊的加密和解密也能得到比较好的安全性。在这里我是想介绍另外一种安全机制,即构建Remoting“防火墙”。
其实我所说的Remoting“防火墙”也是最基本的。由于Remoting的TcpChannel没有提供内建的认证机制,所以没有现成获取客户端的方法,我们可以在Remoting Server端注册上自定义的Server Channel Sink,通过Transport Headers来获取request的IP,以下是自定义的Server Channel Sink类的代码,(
注:原代码不是我写的,原出处我忘记了,所以无法标注来源)
1
using System;
2
using System.Collections;
3
using System.IO;
4
using System.Runtime.Remoting;
5
using System.Runtime.Remoting.Messaging;
6
using System.Runtime.Remoting.Channels;
7
using System.Threading;
8
using System.Net;
9![None.gif](/Images/OutliningIndicators/None.gif)
10![None.gif](/Images/OutliningIndicators/None.gif)
11
namespace Colorful.RemoteObject
12![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
13
public class ClientIPServerSinkProvider : IServerChannelSinkProvider
14![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
private IServerChannelSinkProvider next = null;
17![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
public ClientIPServerSinkProvider()
19![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
}
21![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
public ClientIPServerSinkProvider(IDictionary properties, ICollection providerData)
23![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
}
25![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
public void GetChannelData(IChannelDataStore channelData)
27![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
}
29![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
public IServerChannelSink CreateSink(IChannelReceiver channel)
31![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
IServerChannelSink nextSink = null;
33![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
if (next != null)
35![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
nextSink = next.CreateSink(channel);
37
}
38![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
return new ClientIPServerSink(nextSink);
40
}
41![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
public IServerChannelSinkProvider Next
45![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return next; }
47![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ next = value; }
48
}
49
}
50![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase
52![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
private IServerChannelSink _next;
54![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
public ClientIPServerSink(IServerChannelSink next)
56![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
_next = next;
58
}
59![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
public void AsyncProcessResponse(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers, System.IO.Stream stream)
61![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
}
63![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
64
public Stream GetResponseStream(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers)
65![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
return null;
67
}
68![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
69
public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)
70![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
if (_next != null)
72![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;
74![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75
CallContext.SetData("IP", ip);
76![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
77
ServerProcessing spres = _next.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
78![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
79
return spres;
80
}
81
else
82![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
responseMsg = null;
84![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
85
responseHeaders = null;
86![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
87
responseStream = null;
88![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
89
return new ServerProcessing();
90
}
91
}
92![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
93![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
94![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
95
public IServerChannelSink NextChannelSink
96![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _next; }
98![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _next = value; }
99
}
100
}
101
}
102
以下是
远程对象主程序代码
1
class Program
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
static void Main(string[] args)
4![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
int TcpPort = ConfigInfo.RemoteSocketPort;
6
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
7
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
8
//实例化自定义的Server Channel Sink类
9
Colorful.RemoteObject.ClientIPServerSinkProvider IpInjProvider = new Colorful.RemoteObject.ClientIPServerSinkProvider();
10
provider.Next = IpInjProvider;//加入接收链
11
12
IDictionary props = new Hashtable();
13
props["port"] = TcpPort;
14
15
TcpChannel chan1 = new TcpChannel(props,null,provider);
16![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
17
ChannelServices.RegisterChannel(chan1, false);
18![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
LifetimeServices.LeaseTime = TimeSpan.Zero;//租用周期
20
RemotingConfiguration.RegisterWellKnownServiceType
21
(
22
typeof(Colorful.RemoteObject.Server),
23
ConfigInfo.RemoteSocketURI,
24
WellKnownObjectMode.Singleton
25
);
26
Console.WriteLine(DateTime.Now.ToString() + "服务通道注册成功!等待数据服务请求![dot.gif](/Images/dot.gif)
");
27![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
Console.ReadLine();
29
}
30
}
然后通过配置许可的IP地址XML文件,读取XML验证IP地址列表,然后调用以下函数验证客户端调用的合法性,非授权的IP拒绝访问
验证客服端的合法性
#region 验证客服端的合法性
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检查Remoting客户端的合法性
/// </summary>
/// <returns></returns>
public bool CheckRemotingClient()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IPAddress data = (IPAddress)CallContext.GetData("ClientIPAddress");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if (clientIPAddress.ContainsKey(data))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string msg = "接受远程地址:" + data.ToString() + "的服务请求";
Console.WriteLine(msg);
return true;
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string msg = "远程地址:" + data.ToString() +"不在许可范围内,服务请求被拒绝";
Console.WriteLine(msg);
SQL2005DAL.CommDAL.WriteLogFile("RemotingClientTrace", msg, "Warring", false);
return false;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
基本上就这么多,详细内容,请参看
VB.NET Remoting技术手册,其他安全请参看.
NET Remoting 安全性