zoukankan      html  css  js  c++  java
  • asp.net 实现敏感词全局过滤

    1、Global.asax 文件中写一下代码

     void Application_BeginRequest(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码
            if (Request != null && Request.Headers != null)
            {
                if (Request.Headers.AllKeys.Contains("Accept"))
                {
                    if (!string.IsNullOrEmpty(Request.Headers["Accept"]))
                    {
                        if (Request.Headers["Accept"].ToLower().Contains("text/html"))
                        {
                            Response.Filter=new ResponseFilter(Response.Filter);
                        }
                    }
                }
            }
        }

    2、新增过滤器类 ResponseFilter.cs 代码如下

    using DotNet.Utilities;
    using Lucene.Net.Util.Cache;
    using System;
    using System.Activities.Expressions;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;
    
    /// <summary>
    /// ResponseFilter 的摘要说明
    /// </summary>
    
    
    public class ResponseFilter : Stream
    {
        #region properties 
        Stream responseStream;
        long position;
        StringBuilder html = new StringBuilder();
        #endregion
        #region constructor 
        public ResponseFilter(Stream inputStream)
        {
            responseStream = inputStream;
        }
        #endregion
        #region implemented abstract members 
        public override bool CanRead
        {
            get { return true; }
        }
        public override bool CanSeek
        {
            get { return true; }
        }
        public override bool CanWrite
        {
            get { return true; }
        }
        public override void Close()
        {
            responseStream.Close();
        }
        public override void Flush()
        {
            responseStream.Flush();
        }
        public override long Length
        {
            get { return 0; }
        }
        public override long Position
        {
            get { return position; }
            set { position = value; }
        }
        public override long Seek(long offset, System.IO.SeekOrigin direction)
        {
            return responseStream.Seek(offset, direction);
        }
        public override void SetLength(long length)
        {
            responseStream.SetLength(length);
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return responseStream.Read(buffer, offset, count);
        }
        #endregion
        #region write method 
        public override void Write(byte[] buffer, int offset, int count)
        {
            string sBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
            var pattern = new List<string>();//敏感词列表
    
            //
            //从配置文件 或任意其它地方 初始化 敏感词列表
            //
            foreach (string s1 in pattern)
            {
                if (!string.IsNullOrEmpty(s1))
                {
                    if (s1.Contains("^"))
                    {
                        var _ss = s1.Split(new[] { '^' }, StringSplitOptions.None);
                        sBuffer = sBuffer.Replace(_ss[0], _ss[1]);
                    }
                    else
                    {
                        sBuffer = sBuffer.Replace(s1, "***");
                    }
                }
    
            }
            byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(sBuffer);
            responseStream.Write(data, 0, data.Length);
        }
        #endregion
    }
  • 相关阅读:
    vue项目-百度地图-初始化展示覆盖范围(默认中心点和半径),点击切换中心点,地图落点(带数字)
    echarts地图下钻(全国到省)下钻一次
    JSON取值(key是中文或者数字)方式详解
    Chrome 80跨域cookie无法携带
    将博客搬至CSDN
    Django模板渲染,运行时如果报错:TemplateDoesNotExist at
    使用allure serve查看报告提示allure-results does not exists的解决方法
    jupyter lab不能自动打开浏览器和默认打开chrome浏览器的解决办法
    Chrome浏览器安装SwitchyOmega_Chromium插件,以及导入Burp Suite证书,抓取https请求包
    vue安装
  • 原文地址:https://www.cnblogs.com/lczblog/p/13494761.html
Copyright © 2011-2022 走看看