這是我的控制器簡單的登錄表單使用symfony的
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request,AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('blog/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
這是我security.yml
providers:
our_db_provider:
entity:
class: AppBundle:user
property: uname
in_memory:
memory:
users:
clement:
password: $2y$12$Z2B4JTnglzaYs4z73DBh9u/hIDN/E56CCrLKIjQxP6Q7aeLb5S6LO
roles: 'ROLE_ADMIN'
admin:
password: symfony
roles: 'ROLES_ADMIN'
ryan:
password: ryan1234
roles: 'ROLES_USER'
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
AppBundle\Entity\User:
algorithm: bcrypt
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
http_basic: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
form_login:
login_path: login
check_path: login
secured_area:
pattern: ^/
provider: our_db_provider
anonymous: ~
logout: true
access_control:
# require ROLE_ADMIN for /admin*
- { path: ^/admin, roles: ROLE_ADMIN }
這是我的看法
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
而且我得到這個錯誤。
控制器「AppBundle \ Controller \ SecurityController :: loginAction()」要求您提供「$ authUtils」參數的值。參數是可爲空的,並且沒有提供空值,沒有提供默認值,或者因爲在這之後有一個非可選參數。
您正在使用多個防火牆,您確定使用正確的登錄表單嗎? 'secured_area'例如沒有login_form,並且匹配'^ /'。 – ccKep
我現在改成了main,而且仍然一樣 –
錯誤消息來自$ authUtils沒有設置的事實。將authUtils作爲動作參數傳遞給S3.3是新的。我懷疑你使用的是早期版本?按照3.2的例子:https://symfony.com/doc/3.2/security/form_login_setup.html是的,你的防火牆看起來不尋常。 – Cerad