在服务器上,有什么方法可以区分内部和外部 REST API 请求?
为什么?
我想区分这两个来源的原因是,根据受访者给出的建议,我可能想要返回不同的数据集,具体取决于谁试图提出请求。
概括
我对内部的定义可能不正确。在这种情况下,“内部”是指来自与处理请求的页面相同的域的 XHTTP 请求发出的请求。
外部调用可能是用户从另一个域创建 Curl 请求。
例如:
http.service.ts
内部角度 6 请求
fetchLogin(formData: any): Observable<any> {
let req = null;
let headers = null;
headers = {
reportProgress: false,
headers: new HttpHeaders({
'email': formData['email'],
'password': formData['password']
})
};
req = new HttpRequest('POST', this.restApiUrl + this.restApiUrlEndpoint + '/oauth/', '', headers);
return this.http.request(req)
.map( (data) => {
return 'body' in data ? data['body'] : null;
})
.pipe(
catchError(this.handleError)
);
}
模板.cfm
外部冷融合请求
<cfset httpUrl = request.restApiUrl & request.restApiUrlEndpoint & "/oauth/">
<cfhttp url="#httpUrl#" method="post" result="result" timeout="30">
<cfhttpparam type="header" name="email" value="foo@bar.com" />
<cfhttpparam type="header" name="password" value="foo" />
</cfhttp>
请理解,我已经简化了这两个代码片段以保持清晰。
当请求到达服务器时,我如何知道哪个请求是通过 XHTTP 来的,哪个是通过 CFHTTP [Curl] 发送的?
我正在使用 Taffy.io REST API 框架,所以这是一个简化的方法,在“资源”CFC 中,我可能会使用它来处理请求:
资源/oauthMember.cfc
<cfcomponent extends="taffy.core.resource" taffy_uri="/oauth">
<cffunction name="post">
<cfset var local = StructNew()>
<cfset local.data['email'] = "">
<cfset local.data['password'] = "">
<cfset local.requestBody = getHttpRequestData().headers>
<cftry>
<cfset local.data['email'] = Trim(local.requestBody['email'])>
<cfset local.data['password'] = Trim(local.requestBody['password'])>
<cfcatch>
</cfcatch>
</cftry>
...processing code
<cfreturn representationOf(local.data) />
</cffunction>
</cfcomponent>
向其中一个调用添加额外的标头是不可行的,因为这很容易被欺骗。
有任何想法吗?
环境
Windows 2008R2 Lucee 4.5 IIS7+