3

/account ルートの下のページにアクセスするときに、強制的な https を実現しようとしています。この質問ZF2 toRoute with httpsを見つけましたが、うまくいきます...部分的に。私のルート:

'router' => array(
    'routes' => array(
        'account' => array(
            'type' => 'Scheme',
            'options' => array(
                'route' => '/account',
                'scheme' => 'https',
                'defaults' => array(
                    'controller' => 'Account\Controller\Account',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/',
                        'defaults' => array(
                            'controller' => 'Account\Controller\Account',
                            'action' => 'index',
                        ),
                    ),
                ),
                'signin' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/signin[/:type]',
                        'defaults' => array(
                            'controller' => 'Account\Controller\Account',
                            'action' => 'signin',
                        ),
                        'constraints' => array(
                            'type' => '[a-zA-Z][a-zA-Z0-9-_]*',
                        ),
                    ),
                ),
                'signout' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/signout',
                        'defaults' => array(
                            'controller' => 'Account\Controller\Account',
                            'action' => 'signout',
                        ),
                    ),
                ),
                'register' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/register[/:step]',
                        'defaults' => array(
                            'controller' => 'Account\Controller\Account',
                            'action' => 'register',
                        ),
                        'constraints' => array(
                            'step' => '[a-zA-Z][a-zA-Z0-9-_]*',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

およびスケルトン アプリケーション (github から複製) のアプリケーション モジュールからのホーム ルート。/account のサブルートにアクセスするたびに、404 がスローされます。

http(s)://domain.my/account/signin = 404, wrong
http(s)://domain.my/account/* = 404, wron
https://domain.my/signin = signin page, wrong should be /account/signin
http://domain.my/ = ok, main page
http://domain.my/account = 404, wrong
https://domain.my/ = wrong, account page should be main page

通常、私の問題は次のとおりです。ページにはhttpまたはhttps BUT /accountでアクセスする必要があり、サブルートにはhttpsのみでアクセスする必要があります。

編集

わかりました、chained_routes を試しましたが、これは私が達成したかったことではありません。私はこのようなことをしたい:

ユーザーがログインしていません: タイプ: http://domain.my/account -> https://domain.my/account/loginにリダイレクトされます (私は でこれを達成できることを知っています$authService->hasIdentity()) その後、 https://domain.myにリダイレクトします/アカウント

タイプ: http://domain.my/account/login -> https://domain.my/account/loginにリダイレクト

タイプ: http://domain.my/account/edit -> https://domain.my/account/login にリダイレクトされ 、次にhttps://domain.my/account/editにリダイレクトされます

ログに記録されたユーザーと同じように、/account ルートから何かにアクセスすると、同じ URL にリダイレクトされますが、https が使用されます。

4

4 に答える 4

4

同様の問題がありましたが、イベントを使用してアプローチしました。リダイレクトは分野横断的な問題です。各コントローラーに含めると、維持が難しくなります。このコードは、すべての http 要求を https にリダイレクトする方法を示しています。一部だけが必要な場合は、doHttpsRedirect() にロジックを追加できます。どのアクションをリダイレクトするかを示す構成ファイル内の配列は、このロジックを追加する簡単な方法です。

class Module
{
    ...

    public function onBootstrap(MvcEvent $e){
        $em = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($em);
        $em->attach('route', array($this, 'doHttpsRedirect'));
        ...
    }

    public function doHttpsRedirect(MvcEvent $e){
        $sm = $e->getApplication()->getServiceManager();
        $uri = $e->getRequest()->getUri();
        $scheme = $uri->getScheme();
        if ($scheme != 'https'){
            $uri->setScheme('https');
            $response=$e->getResponse();
            $response->getHeaders()->addHeaderLine('Location', $uri);
            $response->setStatusCode(302);
            $response->sendHeaders();
            return $response;
        }
    }

    ... 
}
于 2014-01-07T08:24:47.573 に答える
1

TBH、セキュリティの観点から、いずれかのページが https を超えている場合、すべてのページが https であるべきです。

http://www.troyhunt.com/2013/05/your-login-form-posts-to-https-but-you.htmlを参照してください。

あなたが実際の問題を解決することについては、スキームルートがどのように機能するかについて誤解していると思います。 https を使用してルートをチェーンする例については、dasprid のこの pr を参照してください https://github.com/zendframework/zf2 /プル/3999

于 2013-08-09T08:15:57.123 に答える
0

わかりました、ジュリアン・スライマンが言ったことと、インターネットを掘り下げて見つけたことに基づいて、コントローラープラグインを使用してこのようなもので終わりました。

モジュール構成:

return array(    
    'controller_plugins' => array(
        'invokables' => array(
            'ForceHttps' => 'Account\Controller\Plugin\ForceHttps',
        ),
    ),
/* rest of the configuration */

プラグイン:

<?php
/**
 * module/Account/src/Account/Controller/Plugin/ForceHttps.php
 *
 * $Id$
 */
namespace Account\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin,
    Zend\Uri\Http as HttpUri;

class ForceHttps extends AbstractPlugin
{
    public function __invoke()
    {
        $request = $this->getController()->getRequest();

        // if we're over https then everything is ok
        if ('https' == $request->getUri()->getScheme())
        {
            return;
        }
        // Not secure, crete url and redirect only if the method is GET
        if ('GET' == $request->getMethod())
        {
            $uri = $request->getUri();

            $url = new HttpUri($uri);
            $url->setScheme('https');
            $url->setPort('443');

            return $this->getController()->redirect()->toUrl($url);
        }

        // all other methods should throw error
        throw new \Exception("ERROR [116]: Insecure connection. Please use secure connection (HTTPS)");
    }
}

コントローラ:

<?php
/**
 * module/Account/src/Account/Controller/AccountController.php
 *
 * $Id$
 */
namespace Account\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel,
    Zend\EventManager\EventManagerInterface;

class AccountController extends AbstractActionController
{
    /**
     * Inject an EventManager instance
     *
     * @param EventManagerInterface $eventManager
     * @return void
     */
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);

        $controller = $this;

        $events->attach('dispatch', function($e) use ($controller) {
            if (($result = $controller->forceHttps()) instanceof Response) {
                return $result;
            }
        }, 100);

        return $this;
    }

    public function indexAction()
    {       
        return new ViewModel(
        );
    }

    /* rest of the actions */
}

私が使用したコントローラーでは、これがZF1の機能のsetEventManager代わりになる可能性があることを読んだことがあります。init()そして、ユーザーが http 経由でアカウント コントローラーのアクションを入力すると、https 接続にリダイレクトされますが、http 接続経由でアカウント コントローラーに何かを投稿しようとすると、エラーが発生します。そして、それが私が欲しかったものです。

于 2013-08-11T22:13:13.133 に答える