步骤 3(可选):指定所检查的爬网 URL 数的可配置限制
本演练介绍如何使用 Microsoft Visual Studio 2005 创建、部署和注册 Microsoft Office SharePoint Server 2007 企业级搜索的自定义安全修整程序。
使用“stsadm”实用工具注册自定义安全修整程序时,可以使用“registersecuritytrimmer”操作指定可选的配置属性。它支持使用可配置的设置创建自定义安全修整程序。
例如,您可以使用自定义安全修整程序所检查项数的可配置限制来创建自定义安全修整程序。虽然这是可选的,但是建议您在自定义安全修整程序实现中包含某种类型的限制。有关详细信息,请参阅企业级搜索结果的自定义安全修整概述。
步骤 3 介绍如何使用所检查文档数的可配置限制创建并注册一个自定义安全修整程序,该步骤包括以下任务:
编写自定义安全修整程序代码
注册自定义安全修整程序
此示例中所描述的自定义安全修整程序类似于步骤 1:创建自定义安全修整程序和步骤 2:部署和注册自定义安全修整程序 中描述的安全修整程序。但是,在此自定义安全修整程序中,我们添加了其他步骤以在实现中设置限制,并更改用于注册自定义安全修整程序的“stsadm”实用工具命令。
编写自定义安全修整程序代码
在开始此处所描述的任务之前,请创建“CustomSecurityTrimmer”项目并修改 CustomSecurityTrimmer 类,如 步骤 1:创建自定义安全修整程序 中所述。
备注
本主题未重复在步骤 1 和 2 中已指定的步骤;它仅描述了不同的任务。有关前面步骤中的信息,请参阅 步骤 1:创建自定义安全修整程序 和 步骤 2:部署和注册自定义安全修整程序。
编写自定义安全修整程序代码
在为实现 ISecurityTrimmer 接口而修改类声明之后,请为限制值声明一个变量,如下所示。
class CustomSecurityTrimmer : ISecurityTrimmer { /* Sets a default limit of 200. You can change this to a value that meets your specific requirements. */ private int intCheckLimit = 200;
创建一个用于比较所检查项数与所配置限制的方法,并添加以下代码。
private bool CheckLimit(IDictionary<String, Object> sessionProperties, int numChecks) { Object currentCount; sessionProperties.TryGetValue("currentCheckCount", out currentCount); if (currentCount == null) { sessionProperties["currentCheckCount"] = numChecks; return (true); } int currentCountInt = Convert.ToInt32(currentCount); currentCountInt += numChecks; sessionProperties["currentCheckCount"] = currentCountInt; if (currentCountInt <= intCheckLimit) { return true; } else { return false; } }
若要完成自定义安全修整程序示例的代码,您必须修改步骤 1:创建自定义安全修整程序中描述的 Initialize() 和 CheckAccess() 方法实现。
修改 ISecurityTrimmer 接口方法实现
将以下代码添加到 Initialize() 方法。
if (trimmerProps["CheckLimitProperty"] != null) { intCheckLimit = Convert.ToInt32(trimmerProps["CheckLimitProperty"]); }
此代码将安全修整程序将检查的文档的最大数目限制设置为名为 CheckLimitProperty 的配置属性的值。
备注
当您注册安全修整程序时,您可以指定配置属性。
将以下代码添加到 CheckAccess() 方法中,并且紧接在方法声明之后。
if (!this.CheckLimit(sessionProperties, crawlURLs.Count)) { throw (new PluggableAccessCheckException("<Display Message>")); }
此代码将调用 CheckLimit 方法。如果已达到限制,则此方法将返回 false。当发生这种情况时,将会触发 PluggableAccessCheckException 异常,并在搜索结果用户界面中显示 PluggableAccessCheckException 构造函数中指定的文本,而不是显示搜索结果。
注册自定义安全修整程序
在开始此步骤之前,必须将 CustomSecurityTrimmerSample.dll 部署到全局程序集缓存中,如 步骤 2:部署和注册自定义安全修整程序 中所述。然后,可以使用 stsadm 实用工具注册自定义安全修整程序。
以下过程介绍如何注册自定义安全修整程序,以将“SharedServices1”共享服务提供程序 (SSP) 的 ID 设置为 1,并将其应用于名为“FileServer1”的服务器的文件共享上的内容。它还将 CheckLimitProperty 配置设置的值设置为 300。
使用 CheckLimitProperty 配置设置注册自定义安全修整程序
在命令提示符下键入以下命令。
stsadm -o registersecuritytrimmer -ssp SharedServices1 -id 1 -typeName "CustomSecurityTrimmerSample.clsCustomSecurityTrimmer, CustomSecurityTrimmerSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<token>" -rulepath file://FileServer1/* -configprops CheckLimitProperty~300
示例
以下是本步骤所述 CustomSecurityTrimmerSample 类的完整示例代码。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;
//For Windows Authentication
using System.Security.Principal;
//For Forms Authenticaion
using System.Web;
using System.Collections.Specialized;
using System.Collections;
namespace CustomSecurityTrimmerSample
{
class CustomSecurityTrimmer : ISecurityTrimmer
{
private int intCheckLimit = 200;
public void Initialize(NameValueCollection trimmerProps, SearchContext searchCxt)
{
if (trimmerProps["CheckLimit"] != null)
{
intCheckLimit = Convert.ToInt32(trimmerProps["CheckLimit"]);
}
}
public BitArray CheckAccess(IList<String> crawlURLs, IDictionary<String, Object> sessionProperties)
{
BitArray retArray = new BitArray(crawlURLs.Count);
if (!this.CheckLimit(sessionProperties, crawlURLs.Count))
{
throw (new PluggableAccessCheckException("Reached Limit"));
}
//For Windows authentication, uncomment the next line:
//string strUser = WindowsIdentity.GetCurrent().Name;
//For Forms authentication, uncomment the next line:
//string strUser = HttpContext.Current.User.Identity.Name;
for (int x = 0; x < crawlURLs.Count; x++)
{
/*
Add code here to check if
strUser can access crawlURLs[x].
If so:
retArray[x] = true;
If not:
retArray[x] = false;
*/
}
return retArray;
}
private bool CheckLimit(IDictionary<String, Object> sessionProperties, int numChecks)
{
Object currentCount;
sessionProperties.TryGetValue("currentCheckCount", out currentCount);
if (currentCount == null)
{
sessionProperties["currentCheckCount"] = numChecks;
return (true);
}
int currentCountInt = Convert.ToInt32(currentCount);
currentCountInt += numChecks;
sessionProperties["currentCheckCount"] = currentCountInt;
if (currentCountInt <= intCheckLimit)
{
return true;
}
else
{
return false;
}
}
}
}
See Also
参考
Microsoft.Office.Server.Search.Query.ISecurityTrimmer
Microsoft.Office.Server.Search.Query.PluggableAccessCheckException