热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无声地将api资源移动到另一个url-Silentlymoveapiresourcestoanotherurl

IvegotapiwrittenwithWepApi2tightlycoupledwithmainwebsite.Ivedecidedtodecoupleitt

I've got api written with WepApi 2 tightly coupled with main web site. I've decided to decouple it to another web app to keep things more isolated.

我已经用WepApi 2编写了api,它与主网站紧密耦合。我决定将它与另一个web应用程序解耦,以使其更加独立。

I've followed such steps:

我跟着这些步骤:

  1. Extract all the API controllers to another project
  2. 将所有API控制器提取到另一个项目中
  3. Creat attribute to redirect all the users currently using our old URL to the new one. For such reasons I've used 307 status code because we should keep user's request's verb and request payload.

    Creat属性将当前使用旧URL的所有用户重定向到新URL。出于这种原因,我使用了307状态码,因为我们应该保留用户请求的谓词和请求负载。

        var respOnse= request.CreateResponse(HttpStatusCode.TemporaryRedirect); //307
        response.Headers.Location = new Uri($"{appConfig.ApiAppDomain}" + "/" + request.RequestUri.AbsolutePath + request.RequestUri.Query);
        return response;
    

In common it works nice. Client got 307 and then follows to the URL in Location header.

通常它工作得很好。客户端获得307,然后跟踪到位置头中的URL。

The problem is here: the main web app is https and the new api is http. When I'm using postman it behave strange and do replace the POST request with GET request with all request's body cutting. Not good at all and strange because 307 doesn't allow to change the method and the payload.

问题在于:主要的web应用程序是https,而新的api是http。当我在使用postman时,它的行为很奇怪,并且用所有请求的body cut替换POST请求。不太好,很奇怪,因为307不允许改变方法和有效载荷。

So here are the couple of questions:

下面是几个问题:

  1. What is the best way to handle this https -> http redirection?
  2. 处理这个https -> http重定向的最佳方式是什么?
  3. Whether it is good solution at all or not?
  4. 它到底是不是一个好的解决方案?
  5. What is the best solution to silently move our users to new api url?
  6. 无声地将用户移动到新的api url的最佳解决方案是什么?

1 个解决方案

#1


3  

302,301 etc Redirects for that matter will be GET requests only .However for 307 technically the browser can make a POST request .More details here .But it is not a good idea to have a redirect which will make unnecessary round trips for every request.Also it may cause other issues like cross domain calls(If you are making Ajax REST API call or browsers will verify all the resources are loaded from https only(Mixed content warning)

302,301等重定向只会收到请求。但是对于307浏览器来说,技术上来说,浏览器可以发出一个POST请求。这里有更多的细节。但是重定向不是一个好主意,它会对每个请求造成不必要的往返。它还可能导致其他问题,如跨域调用(如果您正在进行Ajax REST API调用,或者浏览器将验证所有资源都是仅从https加载的(混合内容警告)

What is the best way to handle this https -> http redirection?

处理这个https -> http重定向的最佳方式是什么?

we should not do redirection as it can cause many issues as I was explaining above

正如我上面所解释的,我们不应该重定向,因为它会引起许多问题

Whether it is good solution at all or not?

它到底是不是一个好的解决方案?

Redirection is not a good solution in this scenario.

在这种情况下,重定向不是一个好的解决方案。

What is the best solution to silently move our users to new api url?

无声地将用户移动到新的api url的最佳解决方案是什么?

The best solution in this scenario in my opinion is to setup a transparent proxy which will do https offloading as well. This will make zero change in your client side also.Here's how we can set it up.

在我看来,在这个场景中最好的解决方案是设置一个透明的代理,它也将执行https卸载。这将使您的客户端也零更改。我们可以这样设置。

  • Setup reverse proxy in IIS for any request which goes to your API.

    在IIS中设置反向代理来处理任何发送到API的请求。

    • refer this ,setup reverse proxy for api and this -Once you follow any of the above article ,you will have a urlrewrite rule like this
    • 请参考这个,设置反向代理api,如果您遵循以上任何一篇文章,您将会有一个这样的url重写规则。

    > <<系统。webserver> <重写> <动作类型="重写" url="http://server2/api/{R:1}" logRewrittenUrl="true" /> <条件> 配置网络服务器>

In this above urlrewrite rule,you will be forwarding the request to server2 if the requested url contains api which will be over http.So till this server the request will come on https and from there it will go to server 2 on http.but this happens wihout the client's knowledge. And this will be local request(not over internet) and the latency is negligible.So the flow will be like this

在上面的urlrewrite规则中,如果请求的url包含http上的api,那么就会将请求转发给server2。因此,在此服务器之前,请求将会出现在https上,然后它将在http上运行到服务器2。但这种情况会影响客户的知识。这将是本地请求(不是通过internet),延迟是可以忽略的。所以流是这样的

Browser =>https(https://example.com/api/products/2) => http(http://server2/api/products/2)

浏览器= > https(https://example.com/api/products/2)= > http(http://server2/api/products/2)

  • Remove the API code from your original website completely. Including the redirect logic ,this will make your website completely free of API implementation
  • 完全从原始网站上删除API代码。包括重定向逻辑,这将使您的网站完全没有API实现

just summarizing the advantages of this approach

总结一下这种方法的优点。

  1. At the client side there is no change required, Clients will not even know that something like this is happening. So no extra round trips on the client side.
  2. 在客户端,不需要任何更改,客户甚至不知道正在发生这样的事情。所以在客户端没有额外的往返行程。
  3. Your traffic will be full https and no https to http outside your main website.
  4. 您的流量将是完全https,没有https到http以外的您的主要网站。
  5. If you have the main website and API calls in the main website, it does not create cross domain calls or mixed content warnings. 4.You have completely isolated your main website from API code.
  6. 如果您在主网站上有主网站和API调用,它不会创建跨域调用或混合内容警告。4所示。您已经将主站点与API代码完全隔离。

推荐阅读
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • JavaWeb中读取文件资源的路径问题及解决方法
    在JavaWeb开发中,读取文件资源的路径是一个常见的问题。本文介绍了使用绝对路径和相对路径两种方法来解决这个问题,并给出了相应的代码示例。同时,还讨论了使用绝对路径的优缺点,以及如何正确使用相对路径来读取文件。通过本文的学习,读者可以掌握在JavaWeb中正确找到和读取文件资源的方法。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 20211101CleverTap参与度和分析工具功能平台学习/实践
    1.应用场景主要用于学习CleverTap的使用,该平台主要用于客户保留与参与平台.为客户提供价值.这里接触到的原因,是目前公司用到该平台的服务~2.学习操作 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
author-avatar
阿魏空荡荡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有