- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试用了位于 http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html 的“简单 Acl 控制的应用程序 1&2”教程。 .
完成此操作后,我尝试激活 BasicAuth 而不是 FormAuth。
我在我的 UsersController 中重新实现了 login() 函数,如下所示:
public function login() {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Not able to login');
}
}
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authenticate' => array('Basic')
),
'DebugKit.Toolbar',
'Session'
);
public function beforeFilter() {
//Configure AuthComponent
$this->Auth->allow('display');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}
public function beforeFilter() {
parent::beforeFilter();
}
/users/
使用教程中描述的 FormAuth 就像一个魅力,所以不会有权限问题。登录数据对于测试(管理员:管理员)非常简单,因此也应该没有问题。
IP - - [16/Apr/2013:18:08:37 +0200] "GET /users/login HTTP/1.0" 401 5179 "-" "Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:23.0) Gecko/20130414 Firefox/23.0"
/lif/Cake/Controller/Auth/BasicAuthenticate
到以下,它的工作原理!
public function authenticate(CakeRequest $request, CakeResponse $response) {
$_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = "admin";
$result = $this->getUser($request);
if (empty($result)) {
$response->header($this->loginHeaders());
$response->statusCode(401);
$response->send();
return false;
}
return $result;
}
var_dump($this->Session->read('Auth.User'));
NULL
print "<pre>";
print_r($this->Session->read('Auth.User'));
print "</pre>";
Array
(
[id] => 1
[username] => admin
[group_id] => 1
[created] => 2013-04-12 12:54:26
[modified] => 2013-04-16 14:27:24
[is_active] => 1
[Group] => Array
(
[id] => 1
[name] => Admin
[created] => 2013-04-12 12:46:42
[modified] => 2013-04-12 12:46:42
)
)
/users/login
的访客用户来进行“假注销”。和
/pages/home
:
http://guest:guest@my.domain/users/login
/users/logout
也可以工作,因为我在那里使用这段代码:
public function logout() {
$user = $this->User->find('first', array('conditions' => array('username' => 'guest')));
$this->Auth->login($user['User']['id']);
}
http://admin:admin@my.domain/users/login
再次登录不同的用户。 .不完美,但至少适用于 Firefox。
/users/login
时强制使用 BasicAuth 的任何建议?这样我就可以随时使用任何客户端轻松切换用户。
public function beforeFilter($redirectlogin = true) {
//Configure AuthComponent
$this->Auth->allow('display', '/users/login');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'home');
$this->Auth->unauthorizedRedirect = array('controller' => 'HTTPCODE', 'action' => 'c403');
if($redirectlogin && $this->Session->read('Auth.needs_reauthenticate')) {
if(!($this->request->params['controller'] == $this->Auth->loginRedirect['controller'] && $this->request->params['pass'][0] == $this->Auth->loginRedirect['action'])) {
$this->redirect('/users/login');
}
}
}
public function beforeFilter() {
parent::beforeFilter(false);
}
public function login() {
$this->autoRender = false;
$this->Session->write('Auth.needs_reauthenticate', true);
if(!$this->Session->check('Auth.count')) {
$count = 1;
} else {
$count = $this->Session->read('Auth.count') + 1;
}
$this->Session->write('Auth.count', $count);
if($this->Session->read('Auth.needs_reauthenticate')) {
if((isset($_SERVER['HTTP_AUTHORIZATION']) && $this->Session->read('Auth.count') == 1) || (!isset($_SERVER['HTTP_AUTHORIZATION']) || empty($_SERVER['HTTP_AUTHORIZATION']) || !$this->Session->check('Auth.sent_header_step') || $this->Session->read('Auth.sent_header_step') < 1)) {
unset($_SERVER['HTTP_AUTHORIZATION']);
$this->Session->write('Auth.redirectTo', $this->Auth->redirect());
$this->response->header(sprintf('WWW-Authenticate: Basic realm="%s"', env('SERVER_NAME')));
$this->response->statusCode(401);
$this->response->send();
$this->Session->write('Auth.sent_header_step', 1);
}
if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
$this->Session->write('Auth.sent_header_step', 0);
$base64string = base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6));
if(!(strlen($base64string) > 1 && substr($base64string, -1, 1) != ":")) {
$_SERVER['PHP_AUTH_USER'] = "";
$_SERVER['PHP_AUTH_PW'] = "";
}
$data = true;
}
$this->Auth->logout();
if(isset($data) && $this->Session->read('Auth.count') > 1) {
if($this->Auth->login()) {
$this->Session->write('Auth.needs_reauthenticate', false);
if($this->Session->check('Auth.redirectTo')) {
$redirectTo = $this->Session->read('Auth.redirectTo');
$this->Session->delete('Auth.redirectTo');
$this->Session->delete('Auth.count');
return $this->redirect($redirectTo);
} else {
return $this->redirect($this->Auth->redirect());
}
} else {
$this->response->statusCode(403);
// my 403 message
}
} else {
if(!isset($_SERVER['HTTP_AUTHORIZATION']) && $this->Session->read('Auth.count') > 1 && isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && trim($_SERVER['PHP_AUTH_USER']) != "" && trim($_SERVER['PHP_AUTH_PW']) != "") {
if($this->Auth->login()) {
$this->Session->write('Auth.needs_reauthenticate', false);
if($this->Session->check('Auth.redirectTo')) {
$redirectTo = $this->Session->read('Auth.redirectTo');
$this->Session->delete('Auth.redirectTo');
$this->Session->delete('Auth.count');
unset($_SERVER['HTTP_AUTHORIZATION']);
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
return $this->redirect($redirectTo);
} else {
return $this->redirect($this->Auth->redirect());
}
} else {
$this->response->statusCode(403);
// my 403 message
}
}
$this->response->statusCode(403);
// my 403 message
}
}
}
最佳答案
将 PHP 作为(快速)CGI 运行时使用基本身份验证
您的网站可能配置为将 PHP 作为(快速)CGI 运行,在这种情况下 PHP_AUTH_USER
和 PHP_AUTH_PWD
$_SERVER
中不存在 key 多变的。 BasicAuthenticate AuthComponent 依赖于这些键。
更改 Plesk 中的域/webhosting 设置以将 php 作为此网站/域的“apache 模块”运行,或者扩展 BasicAuthenticate 组件以通过其他方式获取这些变量。
关于这个主题的更多信息可以在这个问题中找到:
PHP_AUTH_USER not set?
对于 Symfony 框架,似乎有人编写了一个解决方法,在这种情况下也可能有用;
https://github.com/symfony/symfony/issues/1813
更新:使用基本身份验证时注销
使用 basic authentication
时注销是不可能的。基本身份验证是一种“无状态”身份验证机制,这基本上意味着浏览器会随每个请求发送用户凭据。换句话说;服务器不保持“状态”,浏览器 做。使用基本身份验证,您需要浏览器发送用户凭据,并且只要浏览器发送有效凭据,您就允许浏览器访问 protected 页面。
注销的唯一方法是关闭浏览器,或告诉浏览器关闭事件 session /登录。
在此处阅读更多信息:
http://en.wikipedia.org/wiki/Basic_access_authentication
http basic authentication "log out"
笔记
Base Authentication 不是一种安全的身份验证机制;用户名 和密码随每个请求发送到服务器。密码发送未加密 (仅 base64 编码以防止特殊字符出现问题)。
虽然表单例份验证也发送未加密的密码,但它(更安全一点)因为它只会在登录时发送用户名/密码。后续请求只会发送 Session-id,它可以设置为过期并限制为特定的 IP 和/或浏览器类型。
在所有情况下,通过 SSL 保护连接显然很重要。
在登录页面强制重新认证
这只是“大声思考”,未经测试且高度实验性:)
试试这个;
$this->Auth->user('username');
?,则使 session 无效(不破坏)并通过发送登录 header 强制用户重新验证;AppController::isAuthorized()
内的 session 是否仍然“有效”。 (见
Using ControllerAuthorize)
if ("usercredentials sent by browser" === "current logged in user in session") {
// Mark session as 'needs-to-reauthenticate'
$this->Session->write('Auth.needs_reauthenticate', true);
// Need to find a clean approach to get the BasicAuth loginHeaders()
// *including* the right settings (realm)
$this->response->header(/*BasicAuth::loginHeaders()*/);
// Access denied status
$this->response->statusCode(401);
return $this->response->send();
}
if ($this->Session->read('Auth.needs_reauthenticate')) {
return false;
} else {
// Normal 'isAuthorized()' checks here
}
Session.cookieTimeout
在浏览器关闭时将其删除至
0
(见
Session Configuration
关于CakePHP 2.3.2 BasicAuthentication 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16039263/
我正在尝试完成一个简单的任务:使用 BASIC Auth 保护我的 ASP.NET WEBAPI(建立在 Katana 之上)。我知道我可以实现自己的中间件或消息处理程序或其他任何东西。但是我想知道这
我试用了位于 http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simpl
我使用 Indy TIdHTTP 通过 BasicAuthentication 进行获取请求。 代码工作正常,但如果用户使用正确的登录密码重新输入凭据并再次发送请求,TIdHTTP 在第一个 401
您好,我想打开这个网址 http://3864.cloud-matic.net/从我的 android webview 中,我尝试了很多方法,但该应用程序甚至没有打开 mainActivity。我尝试
我正在构建一个名为 Git Crx 的 Chrome 打包应用程序,顾名思义,它需要使用 HTTPS(智能协议(protocol))向远程 git repos 发送网络请求,但是如果你尝试对一个 ur
为什么以下结果会导致“未经授权”响应: webClient .getAbs("http://hello.com") .basicAuthenticat
我正在尝试调试不断收到的 System.Web.HttpException。它可能与我正在尝试实现的 BaiscAuthentication 自定义 HttpModule 有关。 BasicAuthe
好吧,我正在使用 netcoreapp2.2 ,并且我已将我的项目配置为使用“BasicAuthentication”,一切似乎都很好。 services.AddAuthent
我是一名优秀的程序员,十分优秀!