CN118540169B - API safety realizing method, system, medium and API framework realizing method - Google Patents

API safety realizing method, system, medium and API framework realizing method Download PDF

Info

Publication number
CN118540169B
CN118540169B CN202411011776.3A CN202411011776A CN118540169B CN 118540169 B CN118540169 B CN 118540169B CN 202411011776 A CN202411011776 A CN 202411011776A CN 118540169 B CN118540169 B CN 118540169B
Authority
CN
China
Prior art keywords
api
csrf
token
version
directory
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202411011776.3A
Other languages
Chinese (zh)
Other versions
CN118540169A (en
Inventor
谢卓伟
王功喜
姜咏杰
郑相琴
罗凯
刘帅
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Chengdu Vinchin Science And Technology Co
Original Assignee
Chengdu Vinchin Science And Technology Co
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Chengdu Vinchin Science And Technology Co filed Critical Chengdu Vinchin Science And Technology Co
Priority to CN202411011776.3A priority Critical patent/CN118540169B/en
Publication of CN118540169A publication Critical patent/CN118540169A/en
Application granted granted Critical
Publication of CN118540169B publication Critical patent/CN118540169B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L63/00Network architectures or network communication protocols for network security
    • H04L63/14Network architectures or network communication protocols for network security for detecting or protecting against malicious traffic
    • H04L63/1441Countermeasures against malicious traffic
    • H04L63/145Countermeasures against malicious traffic the attack involving the propagation of malware through the network, e.g. viruses, trojans or worms
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L63/00Network architectures or network communication protocols for network security
    • H04L63/12Applying verification of the received information
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L9/00Cryptographic mechanisms or cryptographic arrangements for secret or secure communications; Network security protocols
    • H04L9/32Cryptographic mechanisms or cryptographic arrangements for secret or secure communications; Network security protocols including means for verifying the identity or authority of a user of the system or for message authentication, e.g. authorization, entity authentication, data integrity or data verification, non-repudiation, key authentication or verification of credentials
    • H04L9/3297Cryptographic mechanisms or cryptographic arrangements for secret or secure communications; Network security protocols including means for verifying the identity or authority of a user of the system or for message authentication, e.g. authorization, entity authentication, data integrity or data verification, non-repudiation, key authentication or verification of credentials involving time stamps, e.g. generation of time stamps

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Security & Cryptography (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Computer Hardware Design (AREA)
  • Computing Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Health & Medical Sciences (AREA)
  • General Health & Medical Sciences (AREA)
  • Virology (AREA)
  • Computer And Data Communications (AREA)

Abstract

The invention relates to an API safety realization method, an API safety realization system, a medium and an API framework realization method, and belongs to the technical field of computers. The method for the server side comprises the following steps: verifying configuration and sending, and extracting; a verification step; resending the CSRF-Token step. The method for the client side comprises the following steps: receiving an initial CSRF-Token; an interactive request step is sent; and receiving a response and a secondary CSRF-Token step. Compared with the prior art that only the time stamp is used for verification, the method can effectively defend replay attack, is convenient to configure, does not increase additional expenditure, and almost has no sense in the interaction and verification process.

Description

API安全实现方法、系统、介质及API框架实现方法API security implementation method, system, medium and API framework implementation method

技术领域Technical Field

本发明属于计算机技术领域,涉及一种API安全实现方法、系统、介质及API框架实现方法。The present invention belongs to the field of computer technology and relates to an API security implementation method, system, medium and API framework implementation method.

背景技术Background Art

重放攻击(Replay Attack),也称为复制攻击或回放攻击,是一种网络攻击技术,攻击者截获有效的通信数据,然后选择时间点重新发送这些数据,以尝试获取未经授权的访问权限或执行未授权的操作,是一种威胁性较大的攻击方式。Replay attack, also known as copy attack or playback attack, is a network attack technique in which the attacker intercepts valid communication data and then resends the data at a selected time point in an attempt to obtain unauthorized access rights or perform unauthorized operations. It is a more threatening attack method.

API(应用程序编程入口)是软件系统之间进行交互的重要方式,在一些应用场景中,客户有通过API与开发商的产品或服务进行交互完成自身业务的需求。客户调用API的活动是频繁的,API对外又常是暴露的,在该交互过程中进行重放攻击的防御对于API的安全来说至关重要。API (application programming interface) is an important way for software systems to interact with each other. In some application scenarios, customers need to interact with developers' products or services through API to complete their own business needs. Customers frequently call APIs, and APIs are often exposed to the outside world. Replay attack defense during this interaction is crucial to API security.

目前,传统技术是利用时间戳验证的方式来进行防御,客户端在生成请求时,加入当前的时间戳;服务器接收到请求,并从中提取时间戳,服务器检查时间戳与服务器当前时间的差异,服务器会设置一个时间窗口,例如5分钟或更短的时间,以确定请求是否在合理的时间内发起,如果时间戳显示请求是在时间窗口之外发送的(即与服务器当前时间的差异超出了设定的阈值),服务器将拒绝该请求,通过一个时间敏感的元素来进行安全验证,可以一定程度的防御重放攻击。但是,从客户端发出请求到服务器,始终有一定时间差,就算时间窗口设置很短,为1秒钟,攻击者也可能在该时间内并发很多请求,这就造成了时间戳验证方式无法完全防御重放攻击,给API的安全带来挑战。At present, the traditional technology is to use timestamp verification for defense. When the client generates a request, it adds the current timestamp; the server receives the request and extracts the timestamp from it. The server checks the difference between the timestamp and the current time of the server. The server will set a time window, such as 5 minutes or less, to determine whether the request is initiated within a reasonable time. If the timestamp shows that the request is sent outside the time window (that is, the difference with the current time of the server exceeds the set threshold), the server will reject the request. Security verification through a time-sensitive element can defend against replay attacks to a certain extent. However, there is always a certain time difference from the client sending a request to the server. Even if the time window is set to be very short, such as 1 second, the attacker may make many concurrent requests within this time. This makes the timestamp verification method unable to completely defend against replay attacks, which brings challenges to the security of the API.

因此,如何确保API交互过程的安全性,有效防御重放攻击,保护企业和用户的资产免受损失,是当前急需解决的技术问题。Therefore, how to ensure the security of the API interaction process, effectively defend against replay attacks, and protect the assets of enterprises and users from loss is a technical problem that needs to be solved urgently.

发明内容Summary of the invention

本发明为了解决上述背景技术中的技术问题,提供一种API安全实现方法、系统、介质及API框架实现方法。In order to solve the technical problems in the above-mentioned background technology, the present invention provides an API security implementation method, system, medium and API framework implementation method.

本发明解决上述技术问题的技术方案如下:The technical solution of the present invention to solve the above technical problems is as follows:

第一个方面,提供了一种API安全实现方法,用于服务器端,所述方法包括步骤:In a first aspect, a method for implementing API security is provided, which is used on a server side, and the method comprises the steps of:

验证配置及发送步骤,生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端;Verify the configuration and sending steps, generate a one-time initial CSRF-Token, set the time window for interactive requests, and then send the initial CSRF-Token to the client;

提取步骤,接收客户端交互请求,从客户端交互请求中提取时间戳和待验证CSRF-Token;Extraction step, receiving the client interaction request, extracting the timestamp and the CSRF-Token to be verified from the client interaction request;

验证步骤,验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御;Verification step: Verify whether the CSRF-Token to be verified carried in the interaction request is consistent with the initial CSRF-Token stored in the server, and whether the timestamp of the interaction request is within the time window; if both are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if both are not met at the same time, defense is initiated;

重新发送CSRF-Token步骤,生成一次性的二次CSRF-Token,将二次CSRF-Token随响应发送给客户端。Resend the CSRF-Token step to generate a one-time secondary CSRF-Token, and send the secondary CSRF-Token to the client along with the response.

第二个方面,提供了一种API安全实现方法,用于客户端,所述方法包括步骤:In a second aspect, a method for implementing API security is provided, which is used for a client, and the method comprises the steps of:

接收初始CSRF-Token步骤,接收并保存服务器端的初始CSRF-Token;Receiving the initial CSRF-Token step, receiving and saving the initial CSRF-Token on the server side;

发送交互请求步骤,向服务器端发送交互请求,交互请求携带有初始CSRF-Token和时间戳;Sending an interaction request step: sending an interaction request to the server, the interaction request carries the initial CSRF-Token and timestamp;

接收响应及二次CSRF-Token步骤,接收服务器端随响应的二次CSRF-Token,保存二次CSRF-Token;其中所述服务器端用于生成一次性的初始CSRF-Token、一次性的二次CSRF-Token和设置交互请求的时间窗口;所述服务器端还用于验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御。The step of receiving a response and a secondary CSRF-Token includes receiving a secondary CSRF-Token from the server side along with the response, and saving the secondary CSRF-Token; wherein the server side is used to generate a one-time initial CSRF-Token, a one-time secondary CSRF-Token, and set a time window for interaction requests; the server side is also used to verify whether the CSRF-Token to be verified carried by the interaction request is consistent with the initial CSRF-Token saved in the server, and whether the timestamp of the interaction request is within the time window; if both conditions are met at the same time, access is allowed, and the initial CSRF-Token saved in the server is destroyed; if both conditions are not met at the same time, defense is initiated.

第三个方面,提供了一种API安全实现系统,用于服务器端,所述系统包括:In a third aspect, an API security implementation system is provided, which is used on a server side, and the system includes:

验证配置及发送模块,用于生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端;Verify configuration and send module, used to generate a one-time initial CSRF-Token, set the time window for interactive requests, and then send the initial CSRF-Token to the client;

提取模块,用于接收客户端交互请求,从客户端交互请求中提取时间戳和待验证CSRF-Token;An extraction module is used to receive a client interaction request and extract a timestamp and a CSRF-Token to be verified from the client interaction request;

验证模块,用于验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御;The verification module is used to verify whether the CSRF-Token to be verified carried in the interaction request is consistent with the initial CSRF-Token saved in the server, and whether the timestamp of the interaction request is within the time window; if both are met at the same time, access is allowed and the initial CSRF-Token saved in the server is destroyed; if both are not met at the same time, defense is initiated;

重新发送CSRF-Token模块,用于生成一次性的二次CSRF-Token,将响应和二次CSRF-Token发送给客户端。The resend CSRF-Token module is used to generate a one-time secondary CSRF-Token and send the response and the secondary CSRF-Token to the client.

第四个方面,一种计算机可读存储介质,其上存储有计算机程序,该程序被处理器执行时实现上述的API安全实现方法。In a fourth aspect, a computer-readable storage medium stores a computer program, which implements the above-mentioned API security implementation method when executed by a processor.

第五个方面,提供了一种API框架实现方法,用于服务器端,利用上述的API安全实现方法进行API框架的API调用防护,还包括步骤;In a fifth aspect, an API framework implementation method is provided, which is used on the server side, and uses the above-mentioned API security implementation method to perform API call protection of the API framework, and also includes the steps of:

API框架创建步骤,在服务器创建具有统一API入口和路由池的API框架,API框架内设有若干相互隔离的主版本目录,每个主版本目录内设有若干相互隔离的次版本目录;路由池保存有请求匹配路径与指定版本目录之间的映射关系规则;The API framework creation step is to create an API framework with a unified API entry and a routing pool on the server. The API framework is provided with several mutually isolated main version directories, and each main version directory is provided with several mutually isolated sub-version directories; the routing pool stores the mapping relationship rules between the request matching path and the specified version directory;

版本调用请求接收步骤,API入口接收到客户端版本调用请求,版本调用请求包括URL路径和指定的HTTP头,或者URL路径和查询参数;版本调用请求还包括时间戳和待验证CSRF-Token;In the version call request receiving step, the API entry receives the client version call request, which includes the URL path and the specified HTTP header, or the URL path and query parameters; the version call request also includes a timestamp and a CSRF-Token to be verified;

解析路由步骤,API框架解析版本调用请求,再按照路由池映射关系规则将版本调用请求路由到目标版本目录;Parsing routing step: The API framework parses the version call request, and then routes the version call request to the target version directory according to the routing pool mapping relationship rules;

响应步骤,根据路由结果,API框架调用目标版本目录处理客户端版本调用请求,再形成响应发回客户端。In the response step, according to the routing result, the API framework calls the target version directory to process the client version call request, and then forms a response and sends it back to the client.

本发明实施例提供的一种API安全实现方法、系统、介质及API框架实现方法,基于一次性的CSRF-Token和时间戳验证用户交互请求,相比于现有技术中仅用时间戳进行验证,可以有效地防御重放攻击,且配置方便,不增加额外开销,交互及验证过程几乎无感。An API security implementation method, system, medium and API framework implementation method provided by the embodiments of the present invention verify user interaction requests based on a one-time CSRF-Token and timestamp. Compared with the prior art that only uses timestamps for verification, it can effectively defend against replay attacks, is easy to configure, does not increase additional overhead, and the interaction and verification process is almost imperceptible.

附图说明BRIEF DESCRIPTION OF THE DRAWINGS

为了更清楚地说明本发明实施例中的技术方案,下面将对实施例描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

图1为本发明实施例提供的用于服务器端中使用双重验证的API安全实现方法流程示意图。FIG1 is a flow chart of a method for implementing API security using double verification on a server side provided by an embodiment of the present invention.

图2为本发明实施例提供的用于服务器端中使用三重验证的API安全实现方法流程示意图。FIG2 is a flow chart of a method for implementing API security using triple verification on a server side provided by an embodiment of the present invention.

图3为本发明实施例提供的用于客户端中API安全实现方法流程示意图。FIG3 is a schematic flow chart of a method for implementing API security in a client provided by an embodiment of the present invention.

图4为本发明实施例提供的用于服务器端中使用三重验证API安全实现系统结构示意图。FIG4 is a schematic diagram of a system structure for implementing triple-verification API security in a server-side according to an embodiment of the present invention.

图5为本发明实施例提供的用于服务器端中API框架实现方法流程示意图。FIG5 is a flow chart of a method for implementing an API framework in a server-side according to an embodiment of the present invention.

图6为本发明实施例提供的API框架中主版本目录和次版本目录结构示意图。FIG6 is a schematic diagram of the structure of the main version directory and the secondary version directory in the API framework provided by an embodiment of the present invention.

附图中,各标号所代表的部件列表如下:In the accompanying drawings, the components represented by the reference numerals are listed as follows:

400、安全字典设置模块;410、验证配置及发送模块;420、提取模块;430、验证模块;440、重新发送CSRF-Token模块。400, security dictionary setting module; 410, verification configuration and sending module; 420, extraction module; 430, verification module; 440, resending CSRF-Token module.

具体实施方式DETAILED DESCRIPTION

为了使本发明的目的、技术方案及优点更加清楚明白,以下结合附图及实施例,对本发明进行进一步详细说明。应当理解,此处描述的具体实施例仅仅用以解释本发明,并不用于限定本发明。In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

重放攻击是一种通过截获并重新发送通信数据以获取未授权访问的网络攻击手段。在API交互中,传统的时间戳验证方法虽然能在一定程度上防御此类攻击,但由于客户端与服务器间存在时间差,这种方法并不能完全阻止攻击者在极短时间内发送大量并发请求。因此,开发更有效的技术以确保API交互的安全性,防止重放攻击,对保护企业和用户资产至关重要。Replay attacks are a type of network attack that intercepts and resends communication data to gain unauthorized access. In API interactions, traditional timestamp verification methods can defend against such attacks to a certain extent, but due to the time difference between the client and the server, this method cannot completely prevent attackers from sending a large number of concurrent requests in a very short period of time. Therefore, developing more effective technologies to ensure the security of API interactions and prevent replay attacks is crucial to protecting enterprise and user assets.

针对上述问题,本发明实施例提供一种API安全实现方法,用于服务器端。图1为本发明实施例提供的用于服务器端中API安全实现方法流程示意图,如图1所示,本方法包括:In view of the above problems, an embodiment of the present invention provides an API security implementation method for a server. FIG1 is a flow chart of an API security implementation method for a server provided by an embodiment of the present invention. As shown in FIG1 , the method includes:

步骤S101,生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端。Step S101, generate a one-time initial CSRF-Token, set a time window for interaction requests, and then send the initial CSRF-Token to the client.

跨站请求伪造攻击(Cross-Site Request Forgery,通常缩写为CSRF)是一种网络攻击方式,它利用了Web应用程序对用户浏览器的信任。攻击者通过欺骗用户点击一个链接,或在用户不知情的情况下通过一些技术手段如JavaScript,让浏览器向一个受信任的网站发送非预期的请求。Cross-Site Request Forgery (CSRF) is a network attack that exploits the trust of a web application in a user's browser. The attacker tricks the user into clicking a link or uses some technical means such as JavaScript without the user's knowledge to make the browser send an unexpected request to a trusted website.

CSRF-Token是一种专门防御CSRF的技术手段。CSRF-Token主要目的是在用户已经通过其他方式(如使用用户名和密码)登录后,保护用户在应用程序中的会话不被恶意利用。CSRF-Token is a technical means to defend against CSRF. The main purpose of CSRF-Token is to protect the user's session in the application from being maliciously exploited after the user has logged in through other means (such as using a username and password).

可以理解的是,此处提到的一次性的初始CSRF-Token,是指该CSRF-Token在服务器里生成是唯一的,也只给客户端发送一次。因此,可以确保客户端获得的是唯一的CSRF-Token。It is understandable that the one-time initial CSRF-Token mentioned here means that the CSRF-Token is generated uniquely in the server and is only sent to the client once. Therefore, it can be ensured that the client obtains a unique CSRF-Token.

可以理解的是,CSRF-Token的生成方式多样,可以采用随机值生成,使用随机数生成器创建一个不可预测的Token值;可以基于会话生成,Token的生成与用户的会话(Session)相关联,可能包含会话ID或其他会话特定的信息;还可以使用区块链技术生成,确保Token的唯一性和不可篡改性。因此,本发明实施例对此不作具体限定。It is understandable that there are various ways to generate CSRF-Token. It can be generated by random value, using a random number generator to create an unpredictable Token value; it can be generated based on session, and the generation of Token is associated with the user's session (Session), which may contain session ID or other session-specific information; it can also be generated using blockchain technology to ensure the uniqueness and non-tamperability of Token. Therefore, the embodiments of the present invention do not make specific limitations on this.

步骤S102,接收客户端交互请求,从客户端交互请求中提取时间戳和待验证CSRF-Token。Step S102: receiving a client interaction request, and extracting a timestamp and a CSRF-Token to be verified from the client interaction request.

步骤S103,验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御。Step S103, verify whether the CSRF-Token to be verified carried by the interaction request is consistent with the initial CSRF-Token stored in the server, and whether the timestamp of the interaction request is within the time window; if both are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if both are not met at the same time, defense is initiated.

值得说明的是,对于具体的验证过程,可以先进行CSRF-Token验证,再进行时间戳验证;也可以同时进行CSRF-Token验证和时间戳验证;还可以先进行时间戳验证,再进行CSRF-Token验证。因此,本发明实施例对此不作具体限定。不过,Token是唯一的,时间戳是允许一定的误差范围内允许访问的,作为优选,先验证CSRF-Token的正确性,再验证时间戳,更适应程序逻辑。It is worth noting that, for a specific verification process, CSRF-Token verification can be performed first, and then timestamp verification; CSRF-Token verification and timestamp verification can also be performed simultaneously; or timestamp verification can be performed first, and then CSRF-Token verification can be performed. Therefore, the embodiment of the present invention does not specifically limit this. However, the token is unique, and the timestamp is allowed to be accessed within a certain error range. As a preferred method, the correctness of the CSRF-Token is verified first, and then the timestamp is verified, which is more suitable for program logic.

此外,如果检测到CSRF攻击或时间戳异常,服务器可以立即采取防御措施,具体的防御措施包括但不限于拒绝请求、限制用户会话或提醒用户,及其它们的结合。因此,本发明实施例对此不作具体限定。In addition, if a CSRF attack or timestamp anomaly is detected, the server can immediately take defensive measures, including but not limited to rejecting the request, limiting the user session or reminding the user, and their combination. Therefore, the embodiment of the present invention does not specifically limit this.

我们还可以增加一些监控分析措施,如服务器可以利用日志监控和记录所有接收到的请求及其验证结果,便于事后审计和监控潜在的异常行为。We can also add some monitoring and analysis measures. For example, the server can use logs to monitor and record all received requests and their verification results to facilitate post-audit and monitoring of potential abnormal behavior.

步骤S104,生成一次性的二次CSRF-Token,将二次CSRF-Token随响应发送给客户端。Step S104, generate a one-time secondary CSRF-Token, and send the secondary CSRF-Token to the client along with the response.

现有技术中,客户端在发送请求时附加一个精确的时间戳,服务器接收请求后会立即比对时间戳与服务器当前时间,通过设置极短的时间窗口(如1秒内)来验证请求的时效性,以此机制抵御重放攻击。一方面,由于客户端到服务器的网络传输存在固有延迟,攻击者可能在时间窗口期间发送大量并发请求,这使得仅依赖时间戳的验证方法面临较大挑战;另一方面,在交互场景中,用户往往会进行频繁的操作,实时性強,设计的防御措施,需要平衡安全性与用户体验,避免因安全措施而影响用户的正常交互。In the prior art, the client attaches an accurate timestamp when sending a request. After receiving the request, the server will immediately compare the timestamp with the current time of the server, and verify the timeliness of the request by setting a very short time window (such as within 1 second), so as to resist replay attacks. On the one hand, due to the inherent delay in the network transmission from the client to the server, the attacker may send a large number of concurrent requests during the time window, which makes the verification method that relies only on the timestamp face great challenges; on the other hand, in the interactive scenario, users often perform frequent operations with strong real-time requirements. The designed defense measures need to balance security and user experience to avoid affecting the normal interaction of users due to security measures.

而本发明的实施例中,针对交互场景,一次性CSRF-Token为每个请求提供了一个额外的安全层,即使攻击者能够截获请求,没有有效的Token也无法重复使用该请求;时间戳与服务器当前时间的比较,确保了请求在合理的时间窗口内发出,减少了请求被延迟或重放的风险,采用这种CSRF-Token和时间戳双重验证方式可以有效防御重放攻击。另外,实施这种验证方法简化了配置过程,能够无缝地提升Web框架和API平台的安全性;由于CSRF-Token和时间戳的生成、验证过程都非常快速,对系统性能的影响微乎其微,用户在进行交互时,不会感受到任何延迟或中断,验证过程对他们来说是透明的,从而保证了流畅的交互体验,可以在不牺牲用户体验的前提下,增强了API的安全性,有效平衡了安全措施、应用性能以及用户体验。In the embodiment of the present invention, for interactive scenarios, a one-time CSRF-Token provides an additional security layer for each request. Even if an attacker can intercept a request, the request cannot be reused without a valid Token. The comparison of the timestamp with the current time of the server ensures that the request is issued within a reasonable time window, reducing the risk of the request being delayed or replayed. The use of this CSRF-Token and timestamp dual verification method can effectively defend against replay attacks. In addition, the implementation of this verification method simplifies the configuration process and can seamlessly improve the security of the Web framework and API platform. Since the generation and verification process of CSRF-Token and timestamp are very fast, the impact on system performance is minimal. Users will not feel any delays or interruptions when interacting, and the verification process is transparent to them, thereby ensuring a smooth interactive experience. The security of the API can be enhanced without sacrificing the user experience, effectively balancing security measures, application performance, and user experience.

基于上述实施例,该方法中,所述步骤S101,还包括:所述初始CSRF-Token是以隐蔽的方式发送给客户端,发送的通道为加密通道;Based on the above embodiment, in the method, the step S101 further includes: the initial CSRF-Token is sent to the client in a hidden manner, and the sending channel is an encrypted channel;

步骤S102,还包括:所述二次CSRF-Token是以隐蔽的方式发送给客户端,发送的通道为加密通道。Step S102 also includes: the secondary CSRF-Token is sent to the client in a hidden manner, and the sending channel is an encrypted channel.

值得说明的是,本实施例中提到的隐蔽的发送方式是多种多样的,包括但不限于:在HTML表单中使用隐藏的<input>字段来包含CSRF-Token,这样当用户提交表单时,Token会随表单数据一起发送;将CSRF-Token作为URL的查询参数附加在请求的URL上;通过HTTPOnly的Cookie来存储CSRF-Token,这样Token不会被客户端脚本访问,隐蔽性较好;使用自定义的HTTP请求头来发送Token,这需要客户端和服务器端的配合,并且不会暴露给最终用户。因此,本发明实施例对此不作具体限定。It is worth noting that the hidden sending methods mentioned in this embodiment are various, including but not limited to: using a hidden <input> field in an HTML form to include the CSRF-Token, so that when the user submits the form, the Token will be sent along with the form data; attaching the CSRF-Token to the requested URL as a query parameter of the URL; storing the CSRF-Token through HTTPOnly Cookies, so that the Token will not be accessed by client scripts and has better concealment; using a custom HTTP request header to send the Token, which requires the cooperation of the client and the server, and will not be exposed to the end user. Therefore, the embodiments of the present invention do not specifically limit this.

值得说明的是,本实施例中提到的加密通道是多种多样的,包括但不限于:使用HTTP Secure(HTTPS)协议来加密客户端和服务器之间的所有通信,确保数据在传输过程中的安全性;通过SSL(Secure Sockets Layer)或其继任者TLS(Transport Layer Security)协议来实现数据传输的加密;在企业环境中,客户端通过VPN(虚拟私人网络)连接到内部网络,然后再与服务器通信,VPN提供了加密的通信隧道;使用加密的API网关或服务,这些服务在将请求转发到后端服务之前,对请求进行加密处理。因此,本发明实施例对此不作具体限定。It is worth noting that the encryption channels mentioned in this embodiment are various, including but not limited to: using HTTP Secure (HTTPS) protocol to encrypt all communications between the client and the server to ensure the security of data during transmission; using SSL (Secure Sockets Layer) or its successor TLS (Transport Layer Security) protocol to encrypt data transmission; in an enterprise environment, the client connects to the internal network through VPN (Virtual Private Network) and then communicates with the server, and VPN provides an encrypted communication tunnel; using encrypted API gateways or services, which encrypt requests before forwarding them to backend services. Therefore, the embodiments of the present invention do not specifically limit this.

本实施例提供的方法,通过这些隐蔽的发送方式和加密通道,CSRF-Token的安全性得到了进一步加强,减少了Token在传输过程中被截获的风险,从而进一步防止了重放攻击。The method provided in this embodiment further enhances the security of the CSRF-Token through these covert sending methods and encrypted channels, reduces the risk of the Token being intercepted during transmission, and thus further prevents replay attacks.

基于上述实施例,如图2所示,本方法包括:Based on the above embodiment, as shown in FIG2 , the method includes:

步骤S201,为每个API端点设置安全字典,安全字典规定服务器仅接收offset和limit两个参数,且每个参数的值为1到100之间的正整数;Step S201, setting a security dictionary for each API endpoint, the security dictionary stipulates that the server only receives two parameters, offset and limit, and the value of each parameter is a positive integer between 1 and 100;

步骤S202,生成一次性的初始CSRF-Token,设置交互请求的时间窗口,将初始CSRF-Token与指定API端点的请求关联,再将初始CSRF-Token发送给客户端;Step S202, generating a one-time initial CSRF-Token, setting a time window for interactive requests, associating the initial CSRF-Token with the request of the specified API endpoint, and then sending the initial CSRF-Token to the client;

步骤S203,接收客户端交互请求,从客户端交互请求中提取时间戳、CSRF-Token、API入参和API入参值;Step S203, receiving a client interaction request, and extracting a timestamp, a CSRF-Token, an API input parameter, and an API input parameter value from the client interaction request;

步骤S204,验证交互请求携带的CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内,API入参和API入参值是否符合安全字典的规定;若三者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若三者不同时满足,则启动防御;Step S204: Verify whether the CSRF-Token carried by the interaction request is consistent with the initial CSRF-Token stored in the server, whether the timestamp of the interaction request is within the time window, and whether the API input parameter and the API input parameter value comply with the provisions of the security dictionary; if all three are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if all three are not met at the same time, defense is initiated;

步骤S205,生成一次性的二次CSRF-Token,将二次CSRF-Token随响应发送给客户端。Step S205, generate a one-time secondary CSRF-Token, and send the secondary CSRF-Token to the client along with the response.

值得说明的是,在步骤S201中,offset是指偏移量,表示从查询结果的开始位置跳过多少条记录;limit是限制数量,表示计划从查询结果中获取的记录数量。当确定每个API入口应该传的类型和值后,可以避免非法字符,进一步保证了API的安全;同时,在API入口就可以拦截,避免在每个业务逻辑中单独进行判断,影响系统开销。It is worth noting that in step S201, offset refers to the offset, which indicates how many records are skipped from the start position of the query result; limit is the limit number, which indicates the number of records planned to be obtained from the query result. After determining the type and value that should be transmitted by each API entry, illegal characters can be avoided, further ensuring the security of the API; at the same time, it can be intercepted at the API entry to avoid making separate judgments in each business logic, which affects system overhead.

还值得说明的是,在步骤S202中,将初始CSRF-Token与指定API端点的请求关联是指,服务器生成的CSRF-Token与用户会话相关联,而不是与特定的API端点永久绑定。当用户与应用程序进行交互,特别是通过表单或请求与API端点进行交互时,CSRF-Token用来确保这些请求是从用户的浏览器发出的,而不是来自恶意的第三方网站。CSRF-Token的目的是为用户在会话期间发出的所有请求提供一层额外的安全性。It is also worth noting that in step S202, associating the initial CSRF-Token with the request to the specified API endpoint means that the CSRF-Token generated by the server is associated with the user session, rather than being permanently bound to a specific API endpoint. When the user interacts with the application, especially with the API endpoint through a form or request, the CSRF-Token is used to ensure that these requests are issued from the user's browser, rather than from a malicious third-party website. The purpose of the CSRF-Token is to provide an additional layer of security for all requests made by the user during the session.

本实施例提供的方法,使用安全字典来验证API请求参数,它确保了只有符合预定规则的参数被接受和处理,结合CSRF-Token和时间戳可以有效提升整体的安全性;同时,验证参数和值对用户是透明的,不会直接影响用户的操作流程或感知到的响应时间,这种方法对于安全性的提升是较大的,可对系统开销和用户体验的影响是微小的,较好的实现了安全性与性能的平衡。The method provided in this embodiment uses a security dictionary to verify API request parameters, which ensures that only parameters that meet predetermined rules are accepted and processed. Combined with CSRF-Token and timestamp, it can effectively improve the overall security. At the same time, the verification parameters and values are transparent to the user and will not directly affect the user's operating process or perceived response time. This method can greatly improve security, but has little impact on system overhead and user experience, and better achieves a balance between security and performance.

如图3所示,本发明实施例提供一种API安全实现方法,用于客户端,所述方法包括步骤:As shown in FIG3 , an embodiment of the present invention provides an API security implementation method for a client, the method comprising the steps of:

步骤S301,接收并保存服务器端的初始CSRF-Token;Step S301, receiving and saving the initial CSRF-Token on the server side;

步骤S302,向服务器端发送交互请求,交互请求携带有初始CSRF-Token和时间戳;Step S302, sending an interaction request to the server, the interaction request carries an initial CSRF-Token and a timestamp;

步骤S303,接收服务器端随响应的二次CSRF-Token,保存二次CSRF-Token;其中所述服务器端用于生成一次性的初始CSRF-Token、一次性的二次CSRF-Token和设置交互请求的时间窗口;所述服务器端还用于验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御。Step S303, receiving the secondary CSRF-Token from the server in response, and saving the secondary CSRF-Token; wherein the server is used to generate a one-time initial CSRF-Token, a one-time secondary CSRF-Token and set a time window for the interaction request; the server is also used to verify whether the CSRF-Token to be verified carried by the interaction request is consistent with the initial CSRF-Token saved in the server, and whether the timestamp of the interaction request is within the time window; if both are satisfied at the same time, access is allowed, and the initial CSRF-Token saved in the server is destroyed; if both are not satisfied at the same time, defense is started.

本实施例提供的方法,在整个过程中,客户端依赖服务器对携带的CSRF-Token和时间戳进行严格验证,确保了只有符合要求的请求才能被服务器接受,从而有效防御重放攻击,同时为用户提供了无缝且安全的操作体验。In the method provided in this embodiment, during the entire process, the client relies on the server to strictly verify the carried CSRF-Token and timestamp, ensuring that only requests that meet the requirements can be accepted by the server, thereby effectively defending against replay attacks and providing users with a seamless and secure operating experience.

如图4所示,本发明实施例提供一种API安全实现系统,用于服务器端,所述系统包括:As shown in FIG4 , an embodiment of the present invention provides an API security implementation system for a server, the system comprising:

验证配置及发送模块410,用于生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端;Verification configuration and sending module 410, used to generate a one-time initial CSRF-Token, set a time window for interactive requests, and then send the initial CSRF-Token to the client;

提取模块420,用于接收客户端交互请求,从客户端交互请求中提取时间戳和待验证CSRF-Token;An extraction module 420 is used to receive a client interaction request and extract a timestamp and a CSRF-Token to be verified from the client interaction request;

验证模块430,用于验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御;Verification module 430, used to verify whether the CSRF-Token to be verified carried in the interaction request is consistent with the initial CSRF-Token stored in the server, and whether the timestamp of the interaction request is within the time window; if both are satisfied at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if both are not satisfied at the same time, defense is initiated;

重新发送CSRF-Token模块440,用于生成一次性的二次CSRF-Token,将响应和二次CSRF-Token发送给客户端。The resend CSRF-Token module 440 is used to generate a one-time secondary CSRF-Token and send the response and the secondary CSRF-Token to the client.

作为优选,如图4所示,在所述验证配置及发送模块410之前,还包括:Preferably, as shown in FIG4 , before the verification configuration and sending module 410, the method further includes:

安全字典设置模块400,用于为每个API端点设置安全字典,安全字典规定服务器仅接收offset和limit两个参数,且每个参数的值为1到100之间的正整数;The security dictionary setting module 400 is used to set a security dictionary for each API endpoint. The security dictionary stipulates that the server only receives two parameters, namely, offset and limit, and the value of each parameter is a positive integer between 1 and 100;

所述验证配置及发送模块410,还用于将初始CSRF-Token与指定API端点的请求关联;The verification configuration and sending module 410 is also used to associate the initial CSRF-Token with the request of the specified API endpoint;

所述提取模块420,还用于接收客户端交互请求,从客户端交互请求中提取时间戳、CSRF-Token、API入参和API入参值;The extraction module 420 is further used to receive a client interaction request, and extract a timestamp, a CSRF-Token, an API input parameter, and an API input parameter value from the client interaction request;

所述验证模块430,还用于验证交互请求携带的CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内,API入参和API入参值是否符合安全字典的规定;若三者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若三者不同时满足,则启动防御。The verification module 430 is also used to verify whether the CSRF-Token carried in the interaction request is consistent with the initial CSRF-Token stored in the server, whether the timestamp of the interaction request is within the time window, and whether the API input parameters and API input parameter values comply with the provisions of the security dictionary; if the three are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if the three are not met at the same time, defense is initiated.

本实施例提供的系统,通过这种基于一次性CSRF-Token和时间戳的验证形式,能够以一种几乎无感的方式,为用户提供了一个更加安全和可靠的交互环境。The system provided in this embodiment can provide users with a more secure and reliable interaction environment in an almost imperceptible manner through this verification form based on a one-time CSRF-Token and a timestamp.

在本实施例中,提供一种计算机可读存储介质,其上存储有计算机程序,该程序被处理器执行时实现上述实施例所述API安全实现方法。In this embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the program is executed by a processor, the API security implementation method described in the above embodiment is implemented.

如图5所示,本发明实施例提供一种API框架实现方法,用于服务器端,利用上述实施例的API安全实现方法进行API框架的API调用防护 ,包括步骤:As shown in FIG5 , an embodiment of the present invention provides an API framework implementation method for a server side, and uses the API security implementation method of the above embodiment to perform API call protection of the API framework, including the steps of:

步骤S501,生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端;Step S501, generate a one-time initial CSRF-Token, set a time window for interactive requests, and then send the initial CSRF-Token to the client;

步骤S502,在服务器创建具有统一API入口和路由池的API框架,API框架内设有若干相互隔离的主版本目录,每个主版本目录内设有若干相互隔离的次版本目录;路由池保存有请求匹配路径与指定版本目录之间的映射关系规则;Step S502: Create an API framework with a unified API entry and a routing pool on the server. The API framework is provided with a number of mutually isolated main version directories, and each main version directory is provided with a number of mutually isolated sub-version directories; the routing pool stores the mapping relationship rules between the request matching path and the specified version directory;

步骤S503,API入口接收到客户端版本调用请求,版本调用请求包括URL路径和指定的HTTP头,或者URL路径和查询参数,版本调用请求还包括时间戳和待验证CSRF-Token;Step S503: The API entry receives a client version call request, the version call request includes a URL path and a specified HTTP header, or a URL path and query parameters, and the version call request also includes a timestamp and a CSRF-Token to be verified;

步骤S504,从客户端交互请求中提取时间戳和待验证CSRF-Token;Step S504, extracting the timestamp and the CSRF-Token to be verified from the client interaction request;

步骤S505,验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御;Step S505: Verify whether the CSRF-Token to be verified carried by the interaction request is consistent with the initial CSRF-Token stored in the server, and whether the timestamp of the interaction request is within the time window; if both conditions are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if both conditions are not met at the same time, defense is initiated;

步骤S506,API框架解析版本调用请求,再按照路由池映射关系规则将版本调用请求路由到目标版本目录;Step S506, the API framework parses the version call request, and then routes the version call request to the target version directory according to the routing pool mapping relationship rule;

步骤S507,根据路由结果,API框架调用目标版本目录处理客户端版本调用请求,再形成响应和一次性的二次CSRF-Token发回客户端。Step S507, according to the routing result, the API framework calls the target version directory to process the client version call request, and then generates a response and a one-time secondary CSRF-Token and sends it back to the client.

而本发明的实施例中,所搭建的API框架本身具有高度模块化、灵活路由机制和稳定安全的API入口等特点,在此基础上配合特定的版本调用请求,即选择URL路径搭配指定的HTTP头或查询参数,一方面通过HTTP头或查询参数传递版本信息可以在不更改URL的情况下,动态地指定和切换所需版本,另一方面也提供了额外的扩展性,允许客户端根据需要请求特定版本的特定功能。因此,既可以满足开发商的版本迭代与维护需求,又满足客户通过API灵活调取所需版本代码需求。In the embodiments of the present invention, the constructed API framework itself has the characteristics of high modularity, flexible routing mechanism and stable and secure API entry, etc. On this basis, with the specific version call request, that is, selecting the URL path with the specified HTTP header or query parameter, on the one hand, the required version can be dynamically specified and switched without changing the URL by passing the version information through the HTTP header or query parameter, and on the other hand, it also provides additional scalability, allowing the client to request specific functions of a specific version as needed. Therefore, it can meet the version iteration and maintenance needs of developers, and meet the needs of customers to flexibly call the required version code through the API.

基于上述实施例,该方法中,所述步骤S502,包括:Based on the above embodiment, in the method, step S502 includes:

步骤S5021,在服务器端创建API框架,所述API框架具有统一的API入口,用于接收和响应外部请求。Step S5021, creating an API framework on the server side, wherein the API framework has a unified API entry for receiving and responding to external requests.

步骤S5022,在API框架内创建若干相互隔离的主版本目录,每个主版本目录内创建通用配置信息目录和次版本目录,通用配置信息目录与次版本目录之间相互隔离,次版本目录内设有第一控制器。Step S5022, create several mutually isolated main version directories in the API framework, create a general configuration information directory and a secondary version directory in each main version directory, the general configuration information directory and the secondary version directory are isolated from each other, and a first controller is set in the secondary version directory.

值得说明的是,在每个主版本目录内,创建一个通用配置信息目录。这个目录用于存放所有次版本以及修订版本共享的配置信息,如数据库连接信息、API密钥、日志配置等,这种集中管理便于维护和更新。同时,确保通用配置信息目录与次版本目录之间相互隔离,可以有效避免配置信息的冲突和误用。It is worth noting that a common configuration information directory is created in each main version directory. This directory is used to store configuration information shared by all sub-versions and revisions, such as database connection information, API keys, log configuration, etc. This centralized management facilitates maintenance and updates. At the same time, ensuring that the common configuration information directory is isolated from the sub-version directory can effectively avoid conflicts and misuse of configuration information.

此外,在每个次版本目录内,需要设置控制器。控制器是API框架中的核心组件,负责处理客户端请求并返回响应。控制器包含业务逻辑和数据访问逻辑。本实施例中,版本调用请求的路径是解析到控制器,控制器可以接收请求和输出响应。In addition, a controller needs to be set in each sub-version directory. The controller is the core component in the API framework, responsible for processing client requests and returning responses. The controller contains business logic and data access logic. In this embodiment, the path of the version call request is resolved to the controller, which can receive requests and output responses.

步骤S5023,在API入口路径中设置版本控制,用于支持操作不同版本的API。Step S5023, set version control in the API entry path to support operations of different versions of API.

可以理解的是,此处版本控制并不是指整体的方法,此处版本控制是在API的URL中实现版本控制的一种技术手段,更具体的,它可以明确区分API的URL路径中不同版本的API,以便于管理和调用。It is understandable that version control here does not refer to a holistic method. Version control here is a technical means to implement version control in the API URL. More specifically, it can clearly distinguish different versions of the API in the API URL path for easy management and calling.

对于用户来说,在API入口路径中设置版本控制可以通过URL路径明确地选择他们想要使用的API版本;对开发商来说,在API入口路径中设置版本控制可以更容易地管理和部署不同版本的API。For users, setting version control in the API entry path allows them to explicitly select the API version they want to use through the URL path; for developers, setting version control in the API entry path makes it easier to manage and deploy different versions of the API.

步骤S5024,在API框架内创建路由池,在路由池内保存若干请求匹配路径,将请求匹配路径与指定版本目录之间一一对应,形成映射关系规则。Step S5024, creating a routing pool in the API framework, saving a number of request matching paths in the routing pool, and making a one-to-one correspondence between the request matching paths and the specified version directories to form a mapping relationship rule.

本实施例提供的方法,利用版本目录的隔离确保了配置信息的集中管理和安全性,统一的API入口和版本控制机制为整个框架提供了清晰的结构和灵活性,而控制器的设置和路由池的创建则确保了请求能够被高效且准确地处理,进而实现了一个更加高效、更安全、可扩展且易于维护的API多版本控制框架。The method provided in this embodiment utilizes the isolation of version directories to ensure the centralized management and security of configuration information. The unified API entry and version control mechanism provide a clear structure and flexibility for the entire framework. The setting of the controller and the creation of the routing pool ensure that requests can be processed efficiently and accurately, thereby realizing a more efficient, more secure, scalable and easy-to-maintain API multi-version control framework.

基于上述实施例,该方法中,所述步骤S5022,还包括:Based on the above embodiment, in the method, step S5022 further includes:

步骤S50221,所述通用配置信息目录内创建有配置文件目录、说明目录和语言目录;Step S50221, a configuration file directory, a description directory and a language directory are created in the general configuration information directory;

步骤S50222,所述次版本目录内创建有控制器目录和业务逻辑扩展目录,所述业务逻辑扩展目录内创建有修订版本目录,修订版本目录存放有第二控制器。Step S50222: A controller directory and a business logic extension directory are created in the secondary version directory, a revised version directory is created in the business logic extension directory, and the revised version directory stores a second controller.

为了便于理解,如图6所示,我们展示了API框架中主版本目录(V1)和次版本目录(V1.0)结构,同时我们对主版本目录和次版本目录中子目录进行解释:For ease of understanding, as shown in Figure 6, we show the structure of the main version directory (V1) and the secondary version directory (V1.0) in the API framework, and explain the subdirectories in the main version directory and the secondary version directory:

(1)配置文件目录(config):在通用配置信息目录中创建配置文件目录,允许集中存储所有API版本共用的配置文件,如数据库配置、服务器端点等;(1) Configuration file directory (config): Create a configuration file directory in the common configuration information directory to allow centralized storage of configuration files shared by all API versions, such as database configuration, server endpoints, etc.;

(2)说明目录(description):包含API使用的文档说明,可能包括API的使用方法、参数说明、版本更新日志等,有助于用户和开发人员理解API的工作原理和使用方式;(2) Description: Contains documentation on the use of the API, which may include how to use the API, parameter descriptions, version update logs, etc., to help users and developers understand how the API works and how to use it;

(3)语言目录(lang):用于存放多语言支持的资源文件,使得API能够根据请求的语言偏好返回相应语言的响应;(3) Language directory (lang): used to store resource files for multi-language support, so that the API can return responses in the corresponding language based on the language preference of the request;

(4)控制器目录(controller):在次版本目录中创建控制器目录,用于存放处理特定业务逻辑的控制器代码;(4) Controller directory: Create a controller directory in the secondary version directory to store controller code that handles specific business logic;

(5)业务逻辑扩展目录(logic):提供了一个扩展点,允许开发者根据需要添加额外的业务逻辑处理模块。(5) Business logic extension directory (logic): provides an extension point that allows developers to add additional business logic processing modules as needed.

此外,我们是在业务逻辑扩展目录内创建修订版本目录,用于存放第二控制器,提供修订版本。另外,图6中,app是指应用程序(Application),代表着版本管理这部分内容,也即根目录;alarm通常指的是警报或通知系统,可以将配置信息目录和次版本目录设此目录下,用于在特定提醒。In addition, we create a revision directory in the business logic extension directory to store the second controller and provide revisions. In addition, in Figure 6, app refers to the application, which represents the version management part, that is, the root directory; alarm usually refers to the alarm or notification system, and the configuration information directory and the sub-version directory can be set in this directory for specific reminders.

此外,也可以根据具体的使用场景,增加一些功能目录,比如在配置文件目录中增加日志目录、环境目录等;在次版本目录内增加服务目录(service)、验证目录(validate)等。本发明实施例对此不作具体限定。In addition, some function directories may be added according to specific usage scenarios, such as adding a log directory and an environment directory to a configuration file directory, and adding a service directory (service) and a validation directory (validate) to a secondary version directory. This is not specifically limited in the embodiments of the present invention.

不同于之前的实施例方法,本实施例允许开发者在不影响主业务逻辑的情况下,对特定功能进行扩展和修订,进一步提高了API的灵活性和可维护性,同时通过在业务逻辑扩展目录中创建第二控制器,实现了控制器的模块化,使得不同的业务逻辑可以由不同的控制器处理,有助于降低代码的耦合度。另外,清晰的目录也使得管理更加条理化和专业化。Different from the previous implementation method, this embodiment allows developers to expand and revise specific functions without affecting the main business logic, further improving the flexibility and maintainability of the API. At the same time, by creating a second controller in the business logic extension directory, the controller is modularized, so that different business logics can be processed by different controllers, which helps to reduce the coupling of the code. In addition, a clear directory also makes management more organized and professional.

本发明实施例的计算机存储介质,可以采用一个或多个计算机可读的介质的任意组合。计算机可读介质可以是计算机可读信号介质或者计算机可读存储介质。计算机可读存储介质例如可以是——但不限于——电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。计算机可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式计算机磁盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦式可编程只读存储器(EPROM或闪存)、光纤、便携式紧凑磁盘只读存储器(CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。在本文件中,计算机可读存储介质可以是任何包含或存储程序的有形介质,该程序可以被指令执行系统、装置或者器件使用或者与其结合使用。The computer storage medium of the embodiment of the present invention can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it.

计算机可读的信号介质可以包括在基带中或者作为载波一部分传播的数据信号,其中承载了计算机可读的程序代码。这种传播的数据信号可以采用多种形式,包括但不限于电磁信号、光信号或上述的任意合适的组合。计算机可读的信号介质还可以是计算机可读存储介质以外的任何计算机可读介质,该计算机可读介质可以发送、传播或者传输用于由指令执行系统、装置或者器件使用或者与其结合使用的程序。Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.

计算机可读介质上包含的程序代码可以用任何适当的介质传输,包括——但不限于无线、电线、光缆、RF等等,或者上述的任意合适的组合。Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.

可以以一种或多种程序设计语言或其组合来编写用于执行本发明操作的计算机程序代码,所述程序设计语言包括面向对象的程序设计语言—诸如Java、Smalltalk、C++、Ruby、Go,还包括常规的过程式程序设计语言—诸如“C”语言或类似的程序设计语言。程序代码可以完全地在用户计算机上执行、部分地在用户计算机上执行、作为一个独立的软件包执行、部分在用户计算机上部分在远程计算机上执行、或者完全在远程计算机或服务器上执行。在涉及远程计算机的情形中,远程计算机可以通过任意种类的网络——包括局域网(LAN)或广域网(WAN)—连接到用户计算机,或者,可以连接到外部计算机(例如利用因特网服务提供商来通过因特网连接)。Computer program code for performing the operations of the present invention may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, Ruby, Go, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).

以上所述实施例仅表达了本发明的几种实施方式,其描述较为具体和详细,但并不能因此而理解为对本发明专利范围的限制。应当指出的是,对于本领域的普通技术人员来说,在不脱离本发明构思的前提下,还可以做出若干变形和改进,这些都属于本发明的保护范围。因此,本发明专利的保护范围应以所附权利要求为准。The above-mentioned embodiments only express several implementation methods of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the patent of the present invention. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.

Claims (4)

1.一种API框架实现方法,其特征在于,用于服务器端,包括步骤;1. A method for implementing an API framework, characterized in that it is used on a server side and comprises the steps of: API框架创建步骤,在服务器创建具有统一API入口和路由池的API框架,API框架内设有若干相互隔离的主版本目录,每个主版本目录内设有若干相互隔离的次版本目录;路由池保存有请求匹配路径与指定版本目录之间的映射关系规则;The API framework creation step is to create an API framework with a unified API entry and a routing pool on the server. The API framework is provided with several mutually isolated main version directories, and each main version directory is provided with several mutually isolated sub-version directories; the routing pool stores the mapping relationship rules between the request matching path and the specified version directory; 版本调用请求接收步骤,API入口接收到客户端版本调用请求,版本调用请求包括URL路径和指定的HTTP头,或者URL路径和查询参数;版本调用请求还包括时间戳和待验证CSRF-Token;In the version call request receiving step, the API entry receives the client version call request, which includes the URL path and the specified HTTP header, or the URL path and query parameters; the version call request also includes a timestamp and a CSRF-Token to be verified; 解析路由步骤,API框架解析版本调用请求,再按照路由池映射关系规则将版本调用请求路由到目标版本目录;Parsing routing step: The API framework parses the version call request, and then routes the version call request to the target version directory according to the routing pool mapping relationship rules; 响应步骤,根据路由结果,API框架调用目标版本目录处理客户端版本调用请求,再形成响应发回客户端;In the response step, according to the routing result, the API framework calls the target version directory to process the client version call request, and then forms a response and sends it back to the client; 其中,所述API框架创建步骤,还包括:The API framework creation step further includes: API框架初步创建步骤,在服务器端创建API框架,所述API框架具有统一的API入口,用于接收和响应外部请求;An API framework preliminary creation step is to create an API framework on the server side, wherein the API framework has a unified API entry for receiving and responding to external requests; 版本目录创建步骤,在API框架内创建若干相互隔离的主版本目录,每个主版本目录内创建通用配置信息目录和次版本目录,通用配置信息目录与次版本目录之间相互隔离,其中,通用配置信息目录用于存放所有次版本以及修订版本共享的配置信息,次版本目录内设有第一控制器,该控制器负责处理客户端请求并返回响应,包含业务逻辑和数据访问逻辑,版本调用请求的路径是解析到控制器;The version directory creation step is to create several mutually isolated main version directories in the API framework, and create a general configuration information directory and a sub-version directory in each main version directory. The general configuration information directory and the sub-version directory are mutually isolated. The general configuration information directory is used to store configuration information shared by all sub-versions and revisions. The sub-version directory is provided with a first controller, which is responsible for processing client requests and returning responses, including business logic and data access logic. The path of the version call request is resolved to the controller; 版本控制设置步骤,在API入口路径中设置版本控制,用于支持操作不同版本的API;Version control setting step: set version control in the API entry path to support operations on different versions of the API; 路由池设置步骤,在API框架内创建路由池,在路由池内保存若干请求匹配路径,将请求匹配路径与指定版本目录之间一一对应,形成映射关系规则;The routing pool setting step is to create a routing pool in the API framework, save several request matching paths in the routing pool, and correspond the request matching paths to the specified version directories one by one to form a mapping relationship rule; 其中,对于API框架的API调用防护还包括步骤:Among them, the API call protection for the API framework also includes the following steps: 验证配置及发送步骤,生成一次性的初始CSRF-Token,设置交互请求的时间窗口,再将初始CSRF-Token发送给客户端;Verify the configuration and sending steps, generate a one-time initial CSRF-Token, set the time window for interactive requests, and then send the initial CSRF-Token to the client; 提取步骤,接收客户端交互请求,从客户端交互请求中提取时间戳和待验证CSRF-Token;Extraction step, receiving the client interaction request, extracting the timestamp and the CSRF-Token to be verified from the client interaction request; 验证步骤,验证交互请求携带的待验证CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内;若两者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若两者不同时满足,则启动防御;Verification step: Verify whether the CSRF-Token to be verified carried in the interaction request is consistent with the initial CSRF-Token stored in the server, and whether the timestamp of the interaction request is within the time window; if both are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if both are not met at the same time, defense is initiated; 重新发送CSRF-Token步骤,生成一次性的二次CSRF-Token,将二次CSRF-Token随响应发送给客户端。Resend the CSRF-Token step to generate a one-time secondary CSRF-Token, and send the secondary CSRF-Token to the client along with the response. 2.根据权利要求1所述的API框架实现方法,其特征在于,所述验证配置及发送步骤,还包括:2. The API framework implementation method according to claim 1, characterized in that the verification configuration and sending steps further include: 所述初始CSRF-Token是以隐蔽的方式发送给客户端,发送的通道为加密通道;The initial CSRF-Token is sent to the client in a hidden manner, and the sending channel is an encrypted channel; 所述重新发送CSRF-Token步骤,还包括:The step of resending the CSRF-Token further includes: 所述二次CSRF-Token是以隐蔽的方式发送给客户端,发送的通道为加密通道。The secondary CSRF-Token is sent to the client in a hidden manner, and the sending channel is an encrypted channel. 3.根据权利要求1所述的API框架实现方法,其特征在于,在所述验证配置及发送步骤之前,还包括:3. The API framework implementation method according to claim 1 is characterized in that before the verification configuration and sending steps, it also includes: 安全字典设置步骤,为每个API端点设置安全字典,安全字典规定服务器仅接收offset和limit两个参数,且每个参数的值为正整数;Security dictionary setting step: Set a security dictionary for each API endpoint. The security dictionary stipulates that the server only receives two parameters: offset and limit, and the value of each parameter is a positive integer; 所述验证配置及发送步骤,还包括:将初始CSRF-Token与指定API端点的请求关联;The verification configuration and sending steps further include: associating the initial CSRF-Token with the request of the specified API endpoint; 所述提取步骤,还包括:接收客户端交互请求,从客户端交互请求中提取时间戳、CSRF-Token、API入参和API入参值;The extraction step further includes: receiving a client interaction request, extracting a timestamp, a CSRF-Token, an API input parameter, and an API input parameter value from the client interaction request; 所述验证步骤,还包括:验证交互请求携带的CSRF-Token与服务器内保存的初始CSRF-Token是否一致,交互请求的时间戳是否在时间窗口内,API入参和API入参值是否符合安全字典的规定;若三者同时满足,则允许访问,并销毁服务器内保存的初始CSRF-Token;若三者不同时满足,则启动防御。The verification step also includes: verifying whether the CSRF-Token carried by the interaction request is consistent with the initial CSRF-Token stored in the server, whether the timestamp of the interaction request is within the time window, and whether the API input parameters and API input parameter values comply with the provisions of the security dictionary; if the three are met at the same time, access is allowed and the initial CSRF-Token stored in the server is destroyed; if the three are not met at the same time, defense is initiated. 4.根据权利要求1所述的API框架实现方法,其特征在于,所述版本目录创建步骤,还包括:4. The API framework implementation method according to claim 1, characterized in that the version directory creation step further includes: 通用配置信息目录设置步骤,所述通用配置信息目录内创建有配置文件目录、说明目录和语言目录;A general configuration information directory setting step, wherein a configuration file directory, a description directory and a language directory are created in the general configuration information directory; 业务逻辑扩展目录设置步骤,所述次版本目录内创建有控制器目录和业务逻辑扩展目录,所述业务逻辑扩展目录内创建有修订版本目录,修订版本目录存放有第二控制器。A business logic extension directory setting step is performed, wherein a controller directory and a business logic extension directory are created in the secondary version directory, a revision version directory is created in the business logic extension directory, and the revision version directory stores a second controller.
CN202411011776.3A 2024-07-26 2024-07-26 API safety realizing method, system, medium and API framework realizing method Active CN118540169B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202411011776.3A CN118540169B (en) 2024-07-26 2024-07-26 API safety realizing method, system, medium and API framework realizing method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202411011776.3A CN118540169B (en) 2024-07-26 2024-07-26 API safety realizing method, system, medium and API framework realizing method

Publications (2)

Publication Number Publication Date
CN118540169A CN118540169A (en) 2024-08-23
CN118540169B true CN118540169B (en) 2024-10-29

Family

ID=92383064

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202411011776.3A Active CN118540169B (en) 2024-07-26 2024-07-26 API safety realizing method, system, medium and API framework realizing method

Country Status (1)

Country Link
CN (1) CN118540169B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106790238A (en) * 2017-01-19 2017-05-31 北京神州绿盟信息安全科技股份有限公司 It is a kind of to forge CSRF defence authentication method and device across station request
CN110858836A (en) * 2018-08-24 2020-03-03 阿里巴巴集团控股有限公司 Network attack defense method and device
CN112612508A (en) * 2020-12-24 2021-04-06 新华三云计算技术有限公司 API version control method and device in API gateway and storage medium
CN115357282A (en) * 2022-08-18 2022-11-18 杭州趣链科技有限公司 Application implementation method and device, electronic equipment and storage medium

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111030812A (en) * 2019-12-16 2020-04-17 Oppo广东移动通信有限公司 Token verification method, device, storage medium and server
US12489645B2 (en) * 2021-08-19 2025-12-02 Visa International Service Association System, method, and computer program product for securing authorization cookies and access tokens
CN117539841B (en) * 2024-01-09 2024-04-23 深圳市合纵天下信息科技有限公司 Metadata management system of distributed file system and operation method thereof

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106790238A (en) * 2017-01-19 2017-05-31 北京神州绿盟信息安全科技股份有限公司 It is a kind of to forge CSRF defence authentication method and device across station request
CN110858836A (en) * 2018-08-24 2020-03-03 阿里巴巴集团控股有限公司 Network attack defense method and device
CN112612508A (en) * 2020-12-24 2021-04-06 新华三云计算技术有限公司 API version control method and device in API gateway and storage medium
CN115357282A (en) * 2022-08-18 2022-11-18 杭州趣链科技有限公司 Application implementation method and device, electronic equipment and storage medium

Also Published As

Publication number Publication date
CN118540169A (en) 2024-08-23

Similar Documents

Publication Publication Date Title
US10212173B2 (en) Deterministic reproduction of client/server computer state or output sent to one or more client computers
US11831420B2 (en) Network application firewall
US9104849B2 (en) Network application security utilizing network-provided identities
US8904558B2 (en) Detecting web browser based attacks using browser digest compute tests using digest code provided by a remote source
Fett et al. Spresso: A secure, privacy-respecting single sign-on system for the web
Fett et al. An expressive model for the web infrastructure: Definition and application to the browser id sso system
EP2144420B1 (en) Web application security filtering
US10447726B2 (en) Mitigating attacks on server computers by enforcing platform policies on client computers
Fett et al. Analyzing the BrowserID SSO system with primary identity providers using an expressive model of the web
US20200210584A1 (en) Deterministic Reproduction of Client/Server Computer State or Output Sent to One or More Client Computers
US20160014077A1 (en) System, Method and Process for Mitigating Advanced and Targeted Attacks with Authentication Error Injection
CN115314231B (en) Network attack information processing method and device, electronic equipment and storage medium
CN118646736B (en) Domain name management method, device and readable storage medium
CN117439739A (en) Security protection method and system for interface requests
CN114978590B (en) API safety protection method, equipment and readable storage medium
CN118540169B (en) API safety realizing method, system, medium and API framework realizing method
US7974956B2 (en) Authenticating a site while protecting against security holes by handling common web server configurations
KR102449282B1 (en) Site replication device to enhance website security
CN115460010A (en) Access request authentication method and device, electronic device, storage medium
Kim et al. A Framework for managing user-defined security policies to support network security functions
US20160378982A1 (en) Local environment protection method and protection system of terminal responding to malicious code in link information
Salih et al. Addressing Security Gaps in MCP: Design of a Resilient Reference Architecture
Xie et al. An architecture for cross-cloud auditing
CN121771105A (en) Processing method, device, medium and product of network request
HK40052221A (en) Network application firewall

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant