【问题标题】:Symfony protect with HTTP Basic AuthenticationSymfony 使用 HTTP 基本身份验证保护
【发布时间】:2019-03-01 22:53:09
【问题描述】:

我有一个使用 Symfony 2.8 构建的现有网站,我想通过启用 HTTP Basic Auth仅当参数设置为 true 在 parameters.yml 中添加额外的安全层。有可能吗?

站点已经启用了表单登录,但如果参数为true,我想完全隐藏站点并使用基本身份验证。

这是我的 security.yml:

main:
    pattern:             .*
    context:             user
    form_login:
        provider:       fos_userbundle
        login_path:     /user/login
        use_forward:    false
        check_path:     /user/login_check
        failure_path:   null
        default_target_path: /
    logout:
        path:           /user/logout
        target:         /user/login
    anonymous:          true

【问题讨论】:

    标签: symfony security basic-authentication


    【解决方案1】:

    由于我不想干扰现有的身份验证,我最终使用了 Apache:

    <VirtualHost *:80>
        ServerName mysite.com
        ServerAlias www.mysite.com
    
        DocumentRoot /var/www/html/mysite/current/web
        <Directory /var/www/html/mysite/current/web>
            AllowOverride None
            Order Allow,Deny
            Allow from All
    
            FallbackResource /app.php
    
            # THIS IS THE INTERESTING PART
            # --->
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
            # <---
        </Directory>
    
        # uncomment the following lines if you install assets as symlinks
        # or run into problems when compiling LESS/Sass/CoffeeScript assets
        # <Directory /var/www/project>
        #     Options FollowSymlinks
        # </Directory>
    
        # optionally disable the fallback resource for the asset directories
        # which will allow Apache to return a 404 error when files are
        # not found instead of passing the request to Symfony
        <Directory /var/www/html/mysite/current/web/bundles>
            FallbackResource disabled
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
        CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
    </VirtualHost>
    

    我用这个命令创建了 HTTP 用户和密码:

    sudo htpasswd -c /etc/apache2/.htpasswd stage
    

    -c 参数只需要在您第一次创建文件时出现。

    欲了解更多信息:https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-apache-on-ubuntu-14-04

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多