- 初始化 security.yml 文件
# app/config/security.yml
security:
providers:
in_memory:
memory: ~
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: ~
新建symofny项目的时候,上面的配置文件已经配置好了,上面的配置文件是配置用户如何被授权的,比如是用login form 或者 HTTP 或者 API token等等
- HTTP 认证授权方式
security:
providers:
in_memory:
memory: ~
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
default:
anonymous: ~
http_basic: ~
access_control:
- { path: ^/login, roles: ROLE_USER }
报错1:
InvalidConfigurationException in ArrayNode.php line 317:
Unrecognized option "0" under "security.firewalls.access_control"
解决:
对于.yml 文件空格也是起作用的, access_control中空格位置应该和 firewalls一样的
报错2:
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
The service "profiler" has a dependency on a non-existent service "debug.security.access.decision_manager".
解决:
如果env=prod就会报这个错误,在app.php, 或者 app_dev.php 禁用一个就可以了
app.php 中 $kernel = new AppKernel('dev', false); false 改为 true 就好了
参考: https://github.com/symfony/symfony/issues/19022
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RwpsController extends Controller
{
/**
* @Route("/login")
*/
public function indexAction(Request $request)
{
//return $this->render('rwps/index.html.twig');
return new Response('<html><body>Admin page!</body></html>');
}
}
实现HTTP 认证了
login.jpg- 除了HTTP 认证方式以外,还有database 的认证方式,等等! 后面持续更新!
网友评论