保护PHP Web服务(SOAP)的安全
我有一个PHP web服务,它实际上帮助CMS对数据库进行一些更新。我一直试图在互联网上寻找解决方案,但还没有得到答案 有没有办法检查向Web服务发出请求的用户是否有权这样做,即检查用户会话? 这是我的虚拟服务(services.php)保护PHP Web服务(SOAP)的安全,php,ajax,web-services,soap,Php,Ajax,Web Services,Soap,我有一个PHP web服务,它实际上帮助CMS对数据库进行一些更新。我一直试图在互联网上寻找解决方案,但还没有得到答案 有没有办法检查向Web服务发出请求的用户是否有权这样做,即检查用户会话? 这是我的虚拟服务(services.php) 下面是使用服务[使用SOAP信封]的JavaScript(jQuery) function GetMyUName() { var req1 = ""; req1 += '<soap:Envelope xmlns:soap="http:
下面是使用服务[使用SOAP信封]的JavaScript(jQuery)
function GetMyUName() {
var req1 = "";
req1 += '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">\n';
req1 += '<soap:Body xmlns:m="includes/services.php">\n';
req1 += "<m:GetMyUserName>\n";
req1 += "</m:GetMyUserName>\n";
req1 += "</soap:Body>\n";
req1 += "</soap:Envelope>";
$.ajax({
url: "includes/services.php",
type: "POST",
data: req1,
cache: false
}).done(function (data) {
$("#UsernameTb").html($(data).find("return").text());
});
}
函数GetMyUName(){
var req1=“”;
需求1+='\n';
需求1+='\n';
请求1+=“\n”;
请求1+=“\n”;
请求1+=“\n”;
需求1+=”;
$.ajax({
url:“includes/services.php”,
类型:“POST”,
数据:需求1,
缓存:false
}).完成(功能(数据){
$(“#UsernameTb”).html($(data.find(“return”).text());
});
}
在ASP的WCF中是否有类似的东西允许Web服务中的会话
如果此信息有帮助,我将使用它进行身份验证。概述
坦白说,UserCake看起来像是十年前写的。只需查看UserCake中的login.php
,它调用die()
使用标题(“Location:account.php”)
重定向客户端和echo
s HTML
这些东西都不适合服务范例。最重要的是,UserCake对重定向的依赖将使它实际上不可能在没有实质性修改的情况下利用SOAP或REST接口。只需查看securePage
功能
//Check if a user has access to a page
function securePage($uri){
// earlier code omitted for readability here...
elseif(!isUserLoggedIn())
{
header("Location: login.php");
return false;
}
// ...
}
服务通常不会重定向。它们返回格式化的响应有效负载(例如JSON或XML),并可能使用HTTP代码来指示服务查询的结果。也有类似OAuth的例外,但这与当前的问题完全无关
如果你想要简单的东西,如果你想要健壮的东西,我建议你选择一个合适的框架。两者都完全能够支持纯HTML页面、API请求和身份验证/授权。但如果一定是用户蛋糕
不屈不挠
快速搜索告诉我们Javascript。客户端需要确定响应是HTML还是JSON,然后进行相应的处理。代码摘自answer,并说明了如何使用jQuery实现这一点
$.ajax({
type: "POST",
url: "services.php",
data: {action: 'someSecuredAction', data: 'some data for the command'},
success: function(response, status, xhr){
var ct = xhr.getResponseHeader("content-type") || "";
if (ct.indexOf('html') > -1) {
// If data looks like HTML, this is some page from UserCake
// likely served by a redirect, just display it
var newDoc = document.open("text/html", "replace");
newDoc.write(response);
newDoc.close();
}
if (ct.indexOf('json') > -1) {
// If data looks like JSON we have a
// successful 'service' response from the server
}
}
});
理论上,浏览器将随请求传递任何相关cookie,因此会话应该在服务器上可用。请随意纠正我的这一点,但通过阅读,它听起来像会话cookie将通过AJAX请求传递
然后您的示例“service”(services.php)文件可能会变成这样(开关只是一些人为的东西,让您知道如何从请求中访问数据)
你在用什么?另外,您是否打算让SOAP服务使用与UserCake页面在CMS中保护的帐户相同的帐户?最后,为什么是肥皂?我认为服务器对服务器的通信更有效。如果您在浏览器中使用Javascript,通过RESTful界面交换JSON数据可能不会那么痛苦。@quickshiftin,是的,SOAP服务打算使用相同的帐户,因为我使用UserCake作为我的网站CMS的登录模型,一旦用户登录,UserCake对象将始终用于检查进程的授权。你能分享一个使用REST的类似东西的参考吗?看看UserCake的代码,我认为你将面临REST或SOAP的一些同样的挑战。。。从本质上讲,它不是设计成API的。只需查看UserCake中的login.php
,它调用die()
使用标题(“Location:account.php”)
重定向客户端并回显HTML。我将在回答中提供更多细节。这非常有帮助,我将尝试实现您提到的内容。但我担心这将需要很多更改,因为我的项目几乎处于开发的最后阶段:(
$.ajax({
type: "POST",
url: "services.php",
data: {action: 'someSecuredAction', data: 'some data for the command'},
success: function(response, status, xhr){
var ct = xhr.getResponseHeader("content-type") || "";
if (ct.indexOf('html') > -1) {
// If data looks like HTML, this is some page from UserCake
// likely served by a redirect, just display it
var newDoc = document.open("text/html", "replace");
newDoc.write(response);
newDoc.close();
}
if (ct.indexOf('json') > -1) {
// If data looks like JSON we have a
// successful 'service' response from the server
}
}
});
// If this ends up redirecting,
// the client should just pass it on through
securePage($_SERVER['PHP_SELF']);
switch($_POST['action']) {
case 'someSecuredAction':
// Again, if this redirects the client should be able to cope...
$loggedInUser->checkPermission(['someSecuredAction']);
// Do secured stuff here
$aResponse = someSecureAction($_POST['data']);
break;
}
// Now be nice enough to return the response as JSON
// like we might expect from an actual service
header('Content-Type: application/json;');
echo json_encode($aResponse);