我有一个大问题,请帮助我。所以我想创建一个只使用以下用户的登录系统:
List<WebElement> elements = dr.findElements(By.xpath("//div[@id='menu']"));
所以我的全球路由:providers:
in_memory:
memory:
users:
:
/app/config/routing.yml
我在AdminBundle中的路由:
app_admin:
resource: "@AppAdminBundle/Resources/config/routing.yml"
prefix: /admin
我的LoginController:
app_admin_homepage:
path: /
defaults: { _controller: AppAdminBundle:Login:index }
login:
path: /login
defaults: { _controller: AppAdminBundle:Login:login }
login_check:
path: /login_check
我的login.html.twig:
<?php
namespace App\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class LoginController extends Controller
{
public function indexAction()
{
return $this->render('AppAdminBundle:Member:login.html.twig');
}
public function loginAction()
{
$request = $this->get('request');
$session = $this->get('session');
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('AppAdminBundle:Member:login.html.twig', array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
我的security.yml:
<form class="login-form" action="{{ path('login_check') }}">
<div class="login-wrap">
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" placeholder="Username" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<input type="password" class="form-control" id="password" name="_password" placeholder="Password">
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
</div>
</form>
当我尝试访问时:security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_FMTI: ROLE_FMTI
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, role: IS_AUTHENTICATED_ANONYMOUSLY }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
secured_area:
pattern: ^/admin
anonymous: ~
form_login:
login_path: /admin/login
check_path: /admin/login_check
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
providers:
in_memory:
memory:
users:
admin: { password: 111111, roles: 'ROLE_FMTI' }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
我收到此错误:/admin/login
答案 0 :(得分:13)
而不是这个
$this->get('request')
使用
$this->get('request_stack')->getCurrentRequest()
答案 1 :(得分:8)
这是因为您试图在控制器中获得不存在的服务。在symfony控制器中,您可以在方法调用中获取请求:
在您的Logincontroller中,添加以下用语:
use Symfony\Component\HttpFoundation\Request;
在你的函数声明中
public function loginAction(Request $request)
{
// you can directly use $request
....
}
您现在可以删除尝试获取不存在服务的行:
// remove this line
$request = $this->get('request');
此外,我不确定您是否会使用get(&#39; session&#39;)进行会话。基于Symfony&#39;文档,您应该执行以下操作:
$session = $request->getSession();
请参阅http://symfony.com/doc/current/book/controller.html#managing-the-session
答案 2 :(得分:1)
正是@jiboulex所说的,补充它:
由于内部使用D:\home\site\wwwroot\home\pkp_analyse\https:\web.config
的{{1}}已弃用。
如果您检查迁移指南,则明确进行此更改:
https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#frameworkbundle
关于会话,您可以通过文档中的$this->getRequest()
获取该会话:
http://symfony.com/doc/current/book/controller.html#managing-the-session