htaccess-在Laravel上的身份验证,不包括公共目录

时间:2018-10-03 04:44:08

标签: php laravel apache .htaccess cas

我当前正在Linux服务器上托管站点,而无法访问根系统。我只能在分配的目录下更改.htaccess文件。

背景

基本上,我需要在应用程序中使用AuthType CAS。对受此文件保护的文件的请求将被重定向到外部域身份验证服务器,然后如果登录成功,则将返回到站点。 我遇到问题的背景是在Chrome中请求资源时需要处理CORS。

  • 我在app.x.com上托管。
  • 身份验证服务器位于cas.x.com
  • app.x.com/resource的请求被重定向到cas.x.com/cas/login_service?http%3a%2f%2fapp.x.com%2fresource,然后又重定向回。

例如,我无法在Chrome上加载Font Awesome的字体,因为所请求的URL在我无法控制的cas.x.com上,并且没有允许我的域作为来源的标头。 (TBH,我不明白为什么用这种方式实现浏览器,因为CSS从我的域加载得很好,但是从CSS加载的字体变成了对CAS服务器的请求,但是我离题了。)


所以我只想对Laravel的index.php文件进行身份验证。通过具有以下.htaccess文件:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<FilesMatch "index.php">
    AuthType CAS
    CASAuthNHeader On
    Require valid-user
</FilesMatch>

但是,重定向显示错误,并且此方法由于重定向错误而失败:

  • 302 http://app.x.com/internal/marketplace redirects to http://cas.x.com/cas/login_service?http%3a%2f%2fapp.x.com%2fpublic%2findex.php
  • 302 http://cas.x.com/cas/login_service?http%3a%2f%2fapp.x.com%2fpublic%2findex.php redirects to http://app.x.com/public/index.php?ticket=ST-1180135-xxxxxxxxxxxxxxxxx-cas2
  • ...
  • 404 http://app.x.com/public/index.php

它显示404,因为我没有在Laravel中路由该路径。

没有FilesMatch部分,重定向将类似于:http://cas.x.com/cas/login_service?http%3a%2f%2fapp.x.com%2finternal%2fmarketplace

如何将Auth仅应用于index.php或应用于整个网站,但不包括public/内部的目录和文件?

0 个答案:

没有答案