这是我的security.yml文件:
# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
encoders:
Symfony\Component\Security\Core\User\User: plaintext
# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
# the login page has to be accessible for everybody
demo_login:
pattern: ^/demo/secured/login$
security: false
# secures part of the application
demo_secured_area:
pattern: ^/demo/secured/
# it's important to notice that in this case _demo_security_check and _demo_login
# are route names and that they are specified in the AcmeDemoBundle
form_login:
check_path: _demo_security_check
login_path: _demo_login
logout:
path: _demo_logout
target: _demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
# http://symfony.com/doc/current/book/security.html#security-book-access-control-matching-options
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
http_basic:
realm: "Secured Demo Area"
access_control:
- { path: ^/game/, roles: ROLE_USER }
# Include the following line to also secure the /admin path itself
# - { path: ^/admin$, roles: ROLE_ADMIN }
providers:
in_memory:
memory:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
security:
firewalls:
secured_area:
pattern: ^/game
anonymous: ~
form_login:
login_path: /game/login
check_path: /game/login_check
这是我的路由文件:
login:
path: /login
defaults: { _controller: LoginSecurityBundle:Security:login }
login_login_signup:
path: /signup
defaults: { _controller: LoginLoginBundle:Default:signup }
login_login_logout:
path: /logout
defaults: { _controller: LoginLoginBundle:Default:logout }
login_login_managerPage:
path: /managerPage
defaults: { _controller: LoginLoginBundle:Default:manager }
login_check:
path: /login_check
这是我的安全控制器:
<?php
namespace Login\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
SecurityContextInterface::AUTHENTICATION_ERROR
);
} elseif (null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
} else {
$error = '';
}
// last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);
return $this->render(
'LoginLoginBundle:Default:login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
}
这是我的默认控制器:
<?php
namespace Login\LoginBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Login\LoginBundle\Entity\User;
use Login\LoginBundle\Entity\Stadium;
use Login\LoginBundle\Entity\Team;
use Login\LoginBundle\Entity\Player;
use Login\LoginBundle\Models\Login;
class DefaultController extends Controller {
public function indexAction(Request $request) {
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('LoginLoginBundle:User');
if ($request->getMethod() == 'POST') {
$session->clear();
$username = $request->get('username');
$password = sha1($request->get('password'));
$remember = $request->get('remember');
$user = $repository->findOneBy(array('username' => $username, 'password' => $password));
if ($user) {
if ($remember == 'remember-me') {
$login = new Login();
$login->setUsername($username);
$login->setPassword($password);
$session->set('login', $login);
}
return $this->render('LoginLoginBundle:Default:welcome.html.twig', array('user' => $user));
} else {
return $this->render('LoginLoginBundle:Default:login.html.twig', array('name' => 'Login error'));
}
} else {
if ($session->has('login')) {
$login = $session->get('login');
$username = $login->getUsername();
$password = $login->getPassword();
$user = $repository->findOneBy(array('username' => $username, 'password' => $password));
if ($user) {
return $this->render('LoginLoginBundle:Default:welcome.html.twig', array('user' => $user));
}
}
return $this->render('LoginLoginBundle:Default:login.html.twig');
}
}
/*public function indexAction(Request $request) {
$session = $this->getRequest()->getSession();
//get the login error if there is one
if($request->attributes->has(\Symfony\Component\Security\Core\SecurityContextInterface::AUTHENTICATION_ERROR)){
$error = $request->attributes->get(
SecurityContextInterface::AUTHENTICATION_ERROR
);
} elseif(null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)){
$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
}else{
$error='';
}
//last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);
return $this->render('LoginLoginBundle:Default:login.html.twig',
array(
//last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}*/
public function signupAction(Request $request) {
if ($request->getMethod() == 'POST') {
$firstNames = array(1 => "Kevin", 2 => "Tommy", 3 => "Bert", 4 => "Daan", 5 => "Marouane", 6 => "Gert", 7 => "Steven", 8 => "Mohammed", 9 => "Jaimy", 10 => "Guy", 11 => "Bavo", 12 => "Cameron", 13 => "Ben", 14 => "Carl");
$lastNames = array("Aerts", "Peeters", "Maes", "Sprengers", "Janssens", "Eelen", "Norm");
$username = $request->get('username');
$email = $request->get('email');
$password = $request->get('password');
$teamname = $request->get('teamname');
$stadiumname = $request->get('stadiumname');
$user = new User();
$user->setEmail($email);
$user->setPassword(sha1($password));
$user->setUsername($username);
$user->setMoney(1000000);
$team = new Team();
$team->setName($teamname);
$user->setTeamTeamid($team);
$stadium = new Stadium();
$stadium->setName($stadiumname);
$stadium->setTeamTeamid($team);
$stadium->setAdvertisingboards(4);
$stadium->setBars(5);
$stadium->setCupprice(10);
$stadium->setEastsection(1000);
$stadium->setFriendlycupprice(10);
$stadium->setFriendlyprice(10);
$stadium->setLeagueprice(10);
$stadium->setNortheastlight(1);
$stadium->setNorthsection(1000);
$stadium->setNorthwestlight(1);
$stadium->setParking(2000);
$stadium->setPitchstatus(100);
$stadium->setPitchtype(1);
$stadium->setScoreboard1(1);
$stadium->setScoreboard2(1);
$stadium->setSoutheastlight(1);
$stadium->setSouthsection(1000);
$stadium->setSouthwestlight(1);
$stadium->setToilets(3000);
$stadium->setWestsection(1000);
$stadium->setYouthcentre(1);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($team);
$em->flush();
$em->persist($user);
$em->flush();
$em->persist($stadium);
$em->flush();
for ($i = 0; $i < 2; $i++) {
$player = new Player();
$firstName = rand(0, count($firstNames));
$lastName = rand(0, count($lastNames));
$player->setName($firstName . " " . $lastName); //nog veranderen
$player->setTeamTeamid($team);
$player->setType("GK");
$player->setAggression(rand(60, 100));
$player->setBallcontrol(rand(60, 100));
$player->setBirthday(date_create(date('Y-m-d H:i:s')));
$player->setCourage(rand(60, 100));
$player->setExperience(rand(60, 100));
$player->setFitness(rand(60, 100));
$player->setFlexibility(rand(60, 100));
$player->setGoalkicks(rand(60, 100));
$player->setMorale(100);
$player->setPlayalong(rand(60, 100));
$player->setReflex(rand(60, 100));
$player->setSpeed(rand(60, 100));
$player->setJumping(rand(60, 100));
$player->setStamina(rand(60, 100));
$rating = ($player->getBallcontrol() + $player->getCourage() + $player->getExperience() + $player->getFitness() + $player->getFlexibility() + $player->getGoalkicks() + $player->getPlayalong() + $player->getReflex() + $player->getSpeed() + $player->getJumping() + $player->getStamina()) / 12;
$player->setRating($rating);
$em->persist($player);
$em->flush();
}
for ($i = 0; $i < 7; $i++) {
$player = new Player();
$firstName = rand(0, count($firstNames));
$lastName = rand(0, count($lastNames));
$player->setName($firstName . " " . $lastName); //nog veranderen
$player->setTeamTeamid($team);
$typeRandom = rand(1, 3);
switch ($typeRandom) {
case 1:
$player->setType("LB");
break;
case 2:
$player->setType("CB");
break;
case 3:
$player->setType("RB");
}
$player->setAggression(rand(60, 100));
$player->setBallcontrol(rand(60, 100));
$player->setBirthday(date_create(date('Y-m-d H:i:s')));
$player->setPassing(rand(60, 100));
$player->setExperience(rand(60, 100));
$player->setFitness(rand(60, 100));
$player->setShooting(rand(60, 100));
$player->setPlayitout(rand(60, 100));
$player->setMorale(100);
$player->setStrength(rand(60, 100));
$player->setHeading(rand(60, 100));
$player->setSpeed(rand(60, 100));
$player->setTackling(rand(60, 100));
$player->setStamina(rand(60, 100));
$rating = ($player->getBallcontrol() + $player->getPassing() + $player->getExperience() + $player->getFitness() + $player->getShooting() + $player->getPlayitout() + $player->getStrength() + $player->getHeading() + $player->getSpeed() + $player->getTackling() + $player->getStamina()) / 12;
$player->setRating($rating);
$em->persist($player);
$em->flush();
}
for ($i = 0; $i < 5; $i++) {
$player = new Player();
$firstName = rand(0, count($firstNames));
$lastName = rand(0, count($lastNames));
$player->setName($firstName . " " . $lastName); //nog veranderen
$player->setTeamTeamid($team);
$typeRandom = rand(1, 3);
switch ($typeRandom) {
case 1:
$player->setType("LM");
break;
case 2:
$player->setType("CM");
break;
case 3:
$player->setType("RM");
}
$player->setAggression(rand(60, 100));
$player->setBallcontrol(rand(60, 100));
$player->setBirthday(date_create(date('Y-m-d H:i:s')));
$player->setPassing(rand(60, 100));
$player->setExperience(rand(60, 100));
$player->setFitness(rand(60, 100));
$player->setShooting(rand(60, 100));
$player->setPlaymaking(rand(60, 100));
$player->setMorale(100);
$player->setStrength(rand(60, 100));
$player->setHeading(rand(60, 100));
$player->setSpeed(rand(60, 100));
$player->setTackling(rand(60, 100));
$player->setStamina(rand(60, 100));
$rating = ($player->getBallcontrol() + $player->getPassing() + $player->getExperience() + $player->getFitness() + $player->getShooting() + $player->getPlaymaking() + $player->getStrength() + $player->getHeading() + $player->getSpeed() + $player->getTackling() + $player->getStamina()) / 12;
$player->setRating($rating);
$em->persist($player);
$em->flush();
}
for ($i = 0; $i < 5; $i++) {
$player = new Player();
$firstName = rand(0, count($firstNames));
$lastName = rand(0, count($lastNames));
$player->setName($firstName . " " . $lastName); //nog veranderen
$player->setTeamTeamid($team);
$typeRandom = rand(1, 3);
switch ($typeRandom) {
case 1:
$player->setType("LF");
break;
case 2:
$player->setType("CF");
break;
case 3:
$player->setType("RF");
}
$player->setAggression(rand(60, 100));
$player->setBallcontrol(rand(60, 100));
$player->setBirthday(date_create(date('Y-m-d H:i:s')));
$player->setPassing(rand(60, 100));
$player->setExperience(rand(60, 100));
$player->setFitness(rand(60, 100));
$player->setShooting(rand(60, 100));
$player->setInsight(rand(60, 100));
$player->setMorale(100);
$player->setStrength(rand(60, 100));
$player->setHeading(rand(60, 100));
$player->setSpeed(rand(60, 100));
$player->setTackling(rand(60, 100));
$player->setStamina(rand(60, 100));
$rating = ($player->getBallcontrol() + $player->getPassing() + $player->getExperience() + $player->getFitness() + $player->getShooting() + $player->getInsight() + $player->getStrength() + $player->getHeading() + $player->getSpeed() + $player->getTackling() + $player->getStamina()) / 12;
$player->setRating($rating);
$em->persist($player);
$em->flush();
}
return $this->render('LoginLoginBundle:Default:succesfullSignup.html.twig');
}
return $this->render('LoginLoginBundle:Default:signup.html.twig');
}
public function logoutAction(Request $request) {
$session = $this->getRequest()->getSession();
$session->clear();
return $this->render('LoginLoginBundle:Default:login.html.twig');
}
public function managerAction(Request $request) {
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('LoginLoginBundle:User');
if ($session->has('login')) {
$login = $session->get('login');
$username = $login->getUsername();
$password = $login->getPassword();
$user = $repository->findOneBy(array('username' => $username, 'password' => $password));
if ($user) {
return $this->render('LoginLoginBundle:Default:manager.html.twig', array('user' => $user));
}
}
//return $this->render('LoginLoginBundle:Default:manager.html.twig', array('user' => $user));
return $this->render('LoginLoginBundle:Default:login.html.twig');
}
}
我无法让我的登录系统工作。这是我的网站:
使用此代码,我收到以下错误:
Unable to find the controller for path "/login_check". Maybe you forgot to add the matching route in your routing configuration?
我重读了文档,从头开始再做一切,但我无法让它工作......我做错了什么?
答案 0 :(得分:2)
问题是你的security.yml文件,这是一个烂摊子!老实说,我不知道你想要完成什么
您应该创建security.yml文件,使其只有一个security
块,并且您最多只需要2个防火墙用于您的目的。您还有2个名为相同的防火墙,因此无法使用。您还将http basic auth与基于表单的身份验证混合使用。
如果你想要的是基于表单的身份验证,这里有一个应该有效的security.yml示例。
security:
encoders: Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/ # everything will go through this firewall
form_login:
login_path: login # the route name for your login route
check_path: login_check # the rout name for your login check route
logout:
path: /logout
target: /
anonymous: true # only paths that require a non anonymous role will be password protected
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/game/, role: ROLE_USER }
给出一个镜头,然后告诉我们你是否还有其他问题。