使用 Symfony 强制使用 HTTPS

问题描述:

我正在尝试强制所有对 example.com/checkout/的请求使用 HTTPS.我阅读了 Symfony docs 并在我的 security.yml 文件中有这个:

I'm trying to force all requests to example.com/checkout/ to use HTTPS. I read the Symfony docs and have this in my security.yml file:

access_control:
    - { path: ^/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

但是,当我转到 http://example.com/checkout/ 时,我没有被重定向到 HTTPS.我做错了什么?

However, when I go to http://example.com/checkout/, I am NOT redirected to HTTPS. What am I doing wrong?

原来我没有设置防火墙.我需要在 security.yml 的防火墙部分添加它:

Turns out I did not have a firewall setup. I needed to add this under the firewalls section of security.yml:

checkout:
    pattern: ^/checkout
    anonymous: ~

这奏效了:

access_control:
    - { path: ^/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }