问题描述
我正在尝试遵循以下文档:
我想测试一个视图,该视图仅在通过身份验证后才能访问。 我的问题是我无法正常工作。它不断返回到登录页面的301重定向。我将按照之前的文档向您展示代码:
class DefaultControllerTest extends WebTestCase {
private $client = null;
public function setUp()
{
$this->client = static::createClient();
}
public function testSecuredHello()
{
$this->logIn();
$crawler = $this->client->request('GET','panel/');
self::assertSame(Response::HTTP_OK,$this->client->getResponse()->getStatusCode());
self::assertSame('Mi Panel',$crawler->filter('h1')->text());
}
private function logIn()
{
$session = self::$container->get('session');
// somehow fetch the user (e.g. using the user repository) *MOCKUP
$user = new User();
$user->setId(1);
$user->setEmail('sefhi@gmail.com');
$user->setPassword('qwerty');
$user->setRoles($user->getRoles());
$user->setName('Sefhi');
$user->setActive(true);
$user->setFlgClientBlocked(true);
$user->setDocumentOnline('asdsadasd');
$user->setIdAtenea(123213);
$user->setPhone('600000000');
$user->setIdTypeDocument(1);
$firewallName = 'main';
// if you don't define multiple connected firewalls,the context defaults to the firewall name
// See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
$firewallContext = 'main';
// you may need to use a different token class depending on your application.
// for example,when using Guard authentication you must instantiate PostAuthenticationGuardToken
$token = new UsernamePasswordToken($user,null,$firewallName,$user->getRoles());
$session->set('_security_' . $firewallContext,serialize($token));
$session->save();
$cookie = new Cookie($session->getName(),$session->getId());
$this->client->getCookieJar()->set($cookie);
}}
我向您展示了来自security.yml的配置
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
app_user_provider:
id: App\Security\UserProvider
encoders:
App\Security\User:
algorithm: argon2i
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
json_login:
check_path: login_ajax
remember_me: true
form_login:
provider: app_user_provider
csrf_token_generator: security.csrf.token_manager
login_path: /login
logout:
path: app_logout
target: main
remember_me:
secret: '%kernel.secret%'
lifetime: 604800 # 1 week in seconds
path: /
user_providers: [app_user_provider]
guard:
authenticators:
- App\Security\LoginFormAuthenticator
- App\Security\TokenAuthenticator
entry_point: App\Security\LoginFormAuthenticator
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ ROLE_USER,ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH ]
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
#- { path: ^/register,roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/main,roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/panel,roles: ROLE_USER }
- { path: ^/propuesta,roles: ROLE_USER }
- { path: ^/cliente,roles: ROLE_USER }
- { path: ^/cita,roles: ROLE_USER }
搜寻器总是向我返回301,因此身份验证不起作用。我不知道如何解决这个问题。我认为这不是路由问题,因为如果是路由问题,它将返回404。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)