我在AppController.php中声明了一个标准的Form身份验证:
public String rStoJason(ResultSet rs) throws SQLException
{
if(rs.first() == false) {return "[]";} else {rs.beforeFirst();} // empty rs
StringBuilder sb=new StringBuilder();
Object item; String value;
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
sb.append("[{");
while (rs.next()) {
for (int i = 1; i < numColumns + 1; i++) {
String column_name = rsmd.getColumnName(i);
item=rs.getObject(i);
if (item !=null )
{value = item.toString(); value=value.replace('"', '\'');}
else
{value = "null";}
sb.append("\"" + column_name+ "\":\"" + value +"\",");
} //end For = end record
sb.setCharAt(sb.length()-1, '}'); //replace last comma with curly bracket
sb.append(",{");
} // end While = end resultset
sb.delete(sb.length()-3, sb.length()); //delete last two chars
sb.append("}]");
return sb.toString();
}
现在我想在web服务中基于api_key进行身份验证。 doc解释了这样做:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
]
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
所以现在我想知道如何在我的webservice中加载第二种身份验证机制。我试着这样做:
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
但Cake抱怨我尝试重新加载一个不同的Auth组件。 因此,正确的方法是在AppController.php中加载两种身份验证机制,如下所示:
class DeviceconnectionsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
}
....
}
但似乎不正确,因为两种身份验证都使用不同的 $this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
],
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
和storage
设置。
怎么办?