我安装了FOSUserBundle
的Symfony2项目。当用户尝试访问http://myhost.com/forum时,系统会将其重定向到http://myhost.com/login。
这是我的security.yml
文件:
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
login:
pattern: ^/demo/secured/login$
security: false
secured_area:
pattern: ^/demo/secured/
form_login:
check_path: /demo/secured/login_check
login_path: /demo/secured/login
logout:
path: /demo/secured/logout
target: /demo/
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: /login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /admin/, role: ROLE_ADMIN }
- { path: /forum/, role: ROLE_USER }
现在,我已经重新生成了URL,以便在本教程之后进行翻译:https://coderwall.com/p/eiqd_g
一切正常,除非我尝试访问http://myhost.com/en/forum,因为它重定向到http://myhost.com/login而不是http://myhost.com/en/login
我错过了什么?
更新
因为我在main
(login_path)添加了一行:
main:
pattern: ^/
form_login:
login_path: /%locale%/login
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
重定向到http://myhost.com/es/login而不是http://myhost.com/en/login,而其他网址则保留/en/
网址。实际上,/es/
并不存在,因为它是默认语言。有什么建议吗?
答案 0 :(得分:0)
您还应该为登录路径创建一个路径,如下所示:
login_path:
path: /{_locale}/login
你不需要控制器定义,在你的security.yml
中只需将login_path:/ demo ....的行替换为login_path:login_path
答案 1 :(得分:0)
将main
防火墙更改为下一个代码段非常完美。
main:
pattern: ^/
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout:
path: fos_user_security_logout
target: fos_user_security_login
anonymous: true