近期网站被腾讯安全拦截,微信,QQ也不能打开链接了,必须进行申诉,对网站进行解封,解封之前,我们必须要把可能违规的内容进行处理,处理之后才能解除限制。
如何查找违规内容
我们可以使用云厂商提供的内容安全接口,进行批量的内容检测,这里我使用腾讯云的内容安全进行处理,初次使用,有10000条免费检测条目,超过之后需要收费。
第一步,我们取出网站的所有内容,第二步,调用腾讯内容安全提供的接口,可以使用其提供的SDK,第三步,筛选出违规内容进行处理。这里使用.net构建内容检测程序,参考代码:
//ApiCheckController.cs
using ContentService.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using TencentCloud.Common.Profile;
using TencentCloud.Common;
using TencentCloud.Tms.V20201229;
using TencentCloud.Tms.V20201229.Models;
using ContentService.Domain.Model;
using System.Text;
using System.Buffers.Text;
using TencentCloud.Mrs.V20200910.Models;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace ContentService.WebApi.Controllers
{
[Route("Api/[controller]/[action]")]
[ApiController]
public class ApiCheckController : Controller
{
private readonly ContentDbContext _contentDbContext;
public ApiCheckController(ContentDbContext contentDbContext)
{
_contentDbContext = contentDbContext;
}
[HttpGet]
public async Task<IActionResult> CheckContents()
{
Credential cred = new Credential
{
SecretId = TxConfig.SecretId,
SecretKey = TxConfig.SecretKey
};
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("tms.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
// 实例化要请求产品的client对象,clientProfile是可选的
TmsClient client = new TmsClient(cred, "ap-beijing", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
TextModerationRequest req = new TextModerationRequest();
List<TextModerationResponse> resps = new List<TextModerationResponse>();
List<PostModel> posts = _contentDbContext.Posts.Where(x => x.post_type == "post" && x.post_status == "publish").ToList();
foreach (PostModel post in posts)
{
try
{
byte[] bytes = Encoding.UTF8.GetBytes(post.post_content);
req.Content = Convert.ToBase64String(bytes);
TextModerationResponse resp = client.TextModerationSync(req);
Console.WriteLine(AbstractModel.ToJsonString(resp));
resps.Add(resp);
}
catch (Exception ex)
{
continue;
}
}
return Json(resps);
}
}
}
//ContentDbContext.cs
using ContentService.Domain.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContentService.Infrastructure
{
public class ContentDbContext : DbContext
{
public DbSet<PostModel> Posts { get; set; }
public DbSet<PostMetaModel> PostsMetas { get; set; }
public DbSet<TermModel> Terms { get; set; }
public DbSet<TermTaxonomyModel> TermTaxonomies { get; set; }
public DbSet<TermRelationships> TermRelationships { get; set; }
public ContentDbContext(DbContextOptions<ContentDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostModel>().ToTable("wp_posts");
modelBuilder.Entity<PostMetaModel>().ToTable("wp_postmeta");
modelBuilder.Entity<TermModel>().ToTable("wp_terms");
modelBuilder.Entity<TermTaxonomyModel>().ToTable("wp_term_taxonomy");
modelBuilder.Entity<TermRelationships>().ToTable("wp_term_relationships");
}
}
}
//数据库服务注册
//ModuleInitializer.cs
using Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContentService.Infrastructure
{
public static class ModuleInitializer
{
public static void Initialize(this IServiceCollection services, ConfigurationManager configurationManager)
{
//数据库服务
IConfigurationSection configuration = configurationManager.GetSection("DataBase:ConnectStr");
var connectStr = configuration.Value;
services.AddDbContext<ContentDbContext>(oprions => oprions.UseMySql(connectStr, ServerVersion.AutoDetect(connectStr)));
}
}
}
统计结果
使用内容安全的默认策略,也可以自己设置,可根据明细查询,筛选违规内容,找到网站对应内容进行删除即可,这里的机器识别个人感觉没有那么智能,不能根据内容语义识别,只能识别词汇,所以处理并不需要全部进行删除处理。
![](https://www.codehello.net/wp-content/uploads/2023/03/image-1024x408.png)