我对 Symfony 4 很陌生,这主要是我第一次使用某些东西来处理与管理面板的连接,或者使用 yaml 文件来设置参数。按照文档中的建议,我正在使用安全配方。由于整个站点都在我的计算机上运行,我将它部署在网上,连接问题开始了。
所以这是我的问题:
我在使用用户名开发网站时创建了一个用户:用户和密码:密码。我按照建议使用了 argon2i。在远程服务器中切换到生产模式时,我只是转储数据库并将其复制到服务器上。登录页面未重定向到管理面板。
我认为这可能是因为 argon2 算法基于操作系统资源或类似的东西生成了盐。所以我为用户重新生成了密码。连接仍然无法正常工作。我尝试了另一种算法(每次都直接在数据库中更改密码)。
我认为无法为不同的角色设置生产模式,所以我将安全设置角色降低到IS_AUTHENTICATED_FULLY
我试图在我的计算机的生产和开发模式下重新创建相同的设置,现在它不再工作了。
我首先认为这是因为服务器配置,但显然不是因为我尝试使用 Apache 服务器、服务器配方提供的 PHP 服务器和 PHP 原始服务器。当我得到所有服务器的 HTTP 代码 401 时,我认为这是由于 Symfony 将数据库中的散列密码与用户输入的明文密码进行了比较。我可能错了,但这似乎是最有可能的问题。
我也不知道如何解决这个问题。我尝试使用自动生成的表单(当表单有效并提交时,我不知道如何放置一些逻辑),我尝试使用“老派”弹出,所以我认为安全配置可以处理这个......
这是我的代码:
config/packages/security.yaml
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
db_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
logout:
path: /logout
target: /
prod:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
logout:
path: /logout
target: /
main:
anonymous: ~
http_basic: ~
provider: db_provider
# form_login:
# login_path: login
# check_path: login
# default_target_path: projects_index
# always_use_default_target_path: true
# remember_me:
# secret: '%kernel.secret%'
# lifetime: 604800 # 1 week in seconds
# path: /
# always_remember_me: true
logout:
path: /logout
target: /
pattern: ^/admin
encoders:
Symfony\Component\Security\Core\User\User: plaintext
App\Entity\User:
algorithm: sha512
# 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: ^/admin, roles: IS_AUTHENTICATED_FULLY }
# - { path: ^/user, roles: ROLE_ADMIN }
session_fixation_strategy: migrate
hide_user_not_found: true
always_authenticate_before_granting: false
erase_credentials: true
access_decision_manager:
strategy: affirmative # One of affirmative, consensus, unanimous
allow_if_all_abstain: false
allow_if_equal_granted_denied: true
src/Controller/SecurityController.php
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils, UserPasswordE$
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
src/Form/UserType.php
use App\Entity\User;
use Doctrine\DBAL\Types\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('alias')
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
));
}
}
我希望我足够具体,并且您会帮助我找到解决方案。非常感谢 !