欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WebAPi接口安全之公钥私钥加密

程序员文章站 2022-07-11 10:38:39
...

WebAPi使用公钥私钥加密介绍和使用

随着各种设备的兴起,WebApi作为服务也越来越流行。而在无任何保护措施的情况下接口完全暴露在外面,将导致被恶意请求。最近项目的项目中由于提供给APP的接口未对接口进行时间防范导致短信接口被怒对造成一定的损失,临时的措施导致PC和app的防止措施不一样导致后来前端调用相当痛苦,选型过oauth,https,当然都被上级未通过,那就只能自己写了,就很,,ԾㅂԾ,,。下面就此次的方式做一次记录。最终的效果:传输过程中都是密文,别人拿到请求串不能更改请求参数,通过接口过期时间防止同一请求串一直被调用。

   第一步重写MessageProcessingHandler中的ProcessRequest和ProcessResponse


 

无论是APi还是Mvc请求管道都提供了我们很好的去扩展,本次说的是api,其实mvc大概意思也是差不多的。我们现在主要写出大致流程

WebAPi接口安全之公钥私钥加密

从图中可以看出我们需要在MessageProcessingHandlder上做处理。我们继承MessageProcessingHandlder重写ProcessRequest和ProcessResponse方法,从方法名可以看出一个是针对请求值处理,一个是针对返回值处理代码如下:

public class CustomerMessageProcesssingHandler : MessageProcessingHandler
    {
        protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var contentType = request.Content.Headers.ContentType;

            if (!request.Headers.Contains("platformtype"))
            {
                return request;
            }
            //根据平台编号获得对应私钥
            string privateKey = Encoding.UTF8.GetString(Convert.FromBase64String(ConfigurationManager.AppSettings["PlatformPrivateKey_" + request.Headers.GetValues("platformtype").FirstOrDefault()]));
            if (request.Method == HttpMethod.Post)
            {
                // 读取请求body中的数据
                string baseContent = request.Content.ReadAsStringAsync().Result;
                // 获取加密的信息
                // 兼容 body: 加密数据  和 body: sign=加密数据
                baseContent = Regex.Match(baseContent, "(sign=)*(?<sign>[\\S]+)").Groups[2].Value;
                // 用加密对象解密数据
                baseContent = CommonHelper.RSADecrypt(privateKey, baseContent);
                // 将解密后的BODY数据 重置
                request.Content = new StringContent(baseContent);
                //此contentType必须最后设置 否则会变成默认值
                request.Content.Headers.ContentType = contentType;
            }
            if (request.Method == HttpMethod.Get)
            {
                string baseQuery = request.RequestUri.Query;
                // 读取请求 url query数据
                baseQuery = baseQuery.Substring(1);
                baseQuery = Regex.Match(baseQuery, "(sign=)*(?<sign>[\\S]+)").Groups[2].Value;
                baseQuery = CommonHelper.RSADecrypt(privateKey, baseQuery);
                // 将解密后的 URL 重置URL请求
                request.RequestUri = new Uri($"{request.RequestUri.AbsoluteUri.Split('?')[0]}?{baseQuery}");
            }
            return request;
        }
        protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
        {
            return response;
        }
    }

上面的代码大部分已经有注释了,但这里说明三点第一:platformtype用来针对不同的平台设置不同的公钥和私钥;第二:在post方法中ContentType一定要最后设置,否则会成为默认值,这个问题会导致webapi不能进行正确的参数绑定;第三:有人可能会问这里ProcessResponse是不是可以不用重写?答案是必须重写如果不想对结果操作直接返回就如上当然你也可以在此对返回值进行加密,但是个人认为意义不大,看具体情况因为大部分数据加密后前端还是需要解密然后展示所以此处不做任何处理。在这一步我们已经对前端请求的加密串在handler中处理成明文重新赋值给HttpRequestMessage。
 

  第二步重写AuthorizeAttribute中OnAuthorization和HandleUnauthorizedRequest


 


WebAPi接口安全之公钥私钥加密
上图是Api中Filter的请求顺序,我们在第一个Filter上处理,代码如下:
public class CustomRequestAuthorizeAttribute : AuthorizeAttribute
    {

        public override void OnAuthorization(HttpActionContext actionContext)
        {
            //action具有[AllowAnonymous]特性不参与验证
            if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>().Any(x => x is AllowAnonymousAttribute))
            {
                base.OnAuthorization(actionContext);
                return;
            }
            var request = actionContext.Request;
            string method = request.Method.Method, timeStamp = string.Empty, expireyTime = ConfigurationManager.AppSettings["UrlExpireTime"], timeSign = string.Empty, platformType = string.Empty;
            if (!request.Headers.Contains("timesign") || !request.Headers.Contains("platformtype") || !request.Headers.Contains("timestamp") || !request.Headers.Contains("expiretime"))
            {
                HandleUnauthorizedRequest(actionContext);
                return;
            }
            platformType = request.Headers.GetValues("platformtype").FirstOrDefault();
            timeSign = request.Headers.GetValues("timesign").FirstOrDefault();
            timeStamp = request.Headers.GetValues("timestamp").FirstOrDefault();
            var tempExpireyTime = request.Headers.GetValues("expiretime").FirstOrDefault();
            string privateKey = Encoding.UTF8.GetString(Convert.FromBase64String(ConfigurationManager.AppSettings[$"PlatformPrivateKey_{platformType}"]));
            if (!SignValidate(tempExpireyTime, privateKey, timeStamp, timeSign))
            {
                HandleUnauthorizedRequest(actionContext);
                return;
            }
            if (tempExpireyTime != "0")
            {
                expireyTime = tempExpireyTime;
            }
            //判断timespan是否有效
            double ts2 = ConvertHelper.ToDouble((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds, 2), ts = ts2 - ConvertHelper.ToDouble(timeStamp);
            bool falg = ts > int.Parse(expireyTime) * 1000;
            if (falg)
            {
                HandleUnauthorizedRequest(actionContext);
                return;
            }
            base.IsAuthorized(actionContext);
        }
        protected override void HandleUnauthorizedRequest(HttpActionContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);

            var response = filterContext.Response = filterContext.Response ?? new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.Forbidden;
            var content = new
            {
                BusinessStatus = -10403,
                StatusMessage = "服务端拒绝访问"
            };
            response.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
        }
        private bool SignValidate(string expiryTime, string privateKey, string timestamp, string sign)
        {
            bool isValidate = false;
            var tempSign = CommonHelper.RSADecrypt(privateKey, sign);
            if (CommonHelper.EncryptSHA256($"expiretime{expiryTime}" + $"timestamp{timestamp}") == tempSign)
            {
                isValidate = true;
            }
            return isValidate;
        }
    }

请求头部增加参数expiretime使用此参数作为本次接口的过期时间如果没有则表示使用平台默认的接口时间,是我们可以针对不同的接口设置不同的过期时间;timestamp请求时间戳来防止别人拿到接口后一直调用timesign是过期时间和时间戳通过hash然后在通过公钥加密的串来防止别人修改前两个参数。重写HandleUnauthorizedRequest来设置返回内容。


 

至此整个验证过程就结束了,我们在使用过程中可以建立BaseApi将特性标记上让其他APi继承,当然我们的接口中可能有的action不需要验证看OnAuthorization第一行代码 增加相应的特性跳过此验证。在整个过程中其实我们已经使用了两种加密方式。一是本文中的CustomerMessageProcesssingHandler;另外一种就是timestamp+QueryString然后hash 在公钥加密 这样就不需要CustomerMessageProcesssingHandler其实就是本文中的头部加密方式。

补充:园友建议增加请求端实例,确实是昨天有所遗漏。趁不忙补充上:

本次以HttpClient调用方式为例,展示Get,Post请求加密到执行的相应的action的过程;首先看一下Get请求如下:

WebAPi接口安全之公钥私钥加密

可以看到我们的请求串url已经是密文,头部时间sign也是密文,除非别人拿到我们的私钥不然是不能修改其参数的。然后请求到达我们的CustomerMessageProcesssingHandler中我们看下Get中得到的参数是:

WebAPi接口安全之公钥私钥加密

这是我们得到的前端传过来的querystring的参数他的值就是我们前端加密后传过来的下一步我们解密应该要得到未加密之前的参数也就是客户端中id=1同时重新给requesturi赋值;

WebAPi接口安全之公钥私钥加密

结果中我们可以看到id=1已被正确解密得到。接下来进入我们的CustomRequestAuthorizeAttribute

WebAPi接口安全之公钥私钥加密

在这一步我们进行对timeSign的解密对请求只进行hash对比然后验证时间戳是否在过期时间内最终我们到达相应的action:

WebAPi接口安全之公钥私钥加密

这样整个请求也就完成了Post跟Get区别不大重要的在于拿到传递参数的地方不一样这里我只贴一下调用的代码过程同上:

public static void PostTestByModel() {

            HttpClient http = new HttpClient();
            var timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds;
            var expiretime = "600";
            var timesign = RSAEncrypt(publicKey, EncryptSHA256($"expiretime{expiretime}timestamp{timestamp}"));
            var codeValue = RSAEncrypt(publicKey, JsonConvert.SerializeObject(new Tenmp { Id = 1, Name = "cl" }));
            http.DefaultRequestHeaders.Add("platformtype", "Web");
            http.DefaultRequestHeaders.Add("timesign", $"{timesign}");
            http.DefaultRequestHeaders.Add("timestamp", $"{string.Format("{0:N2}", timestamp.ToString()) }");
            http.DefaultRequestHeaders.Add("expiretime", expiretime);
            var url1 = string.Format($"{host}api/Values/PostTestByModel");
            HttpContent content = new StringContent(codeValue);
            MediaTypeHeaderValue typeHeader = new MediaTypeHeaderValue("application/json");
            typeHeader.CharSet = "UTF-8";
            content.Headers.ContentType = typeHeader;
            var response1 = http.PostAsync(url1, content).Result;
        }

最后当验证不通过得到的返回值:

WebAPi接口安全之公钥私钥加密

这也就是重写HandleUnauthorizedRequest的目的 当然你也可以不重写此方法那么返回的就是401 英文的未通过验证。

转自:http://www.cnblogs.com/clly/p/7384008.html
相关标签: webapi