你可以用一点 mod_perl 来做到这一点。下面是一个示例解决方案,需要稍微调整一下,以便它只捕获您想要保护的 URL 而忽略其他所有内容。
下面的代码假定 Grails 服务器上的某个 URL 由 Spring Security 保护,并且该页面上唯一的内容是“允许”的工作。我使用了 URL http://mywebapp.com/some-secured-page.jsp。
mod_perl 代码,在一个名为 MyModPerlFilter.pm 的文件中是这样的:
package MyModPerlFilter;
use strict;
use Apache2::RequestRec;
use Apache2::Connection;
use APR::Table;
use LWP::Simple;
use base qw(Apache2::Filter);
use Apache2::Const -compile => qw(REDIRECT DECLINED M_GET);
use constant BUFF_LEN => 1024;
sub handler : FilterRequestHandler {
my $r = shift;
# check only GET requests for /*.php, ignore everything else
if ($r->uri() =~ m|/.*\.php$| && $r->method_number == Apache2::Const::M_GET) {
# grab Tomcat session ID from request
my $jsessionid = $r->headers_in->{Cookie} =~ /JESSIONID=([^;\s]+)/ && $1;
# fetch secure page with the Tomcat session ID
my $res = get("http://mywebapp.com/some-secured-page.jsp;jsessionid=$jsessionid");
# any response other than the word "ALLOWED" redirect to login form
if ($res ne 'ALLOWED') {
$r->headers_out->set("Location", "http://mywebapp.com/login.jsp");
return Apache2::Const::REDIRECT;
}
}
return Apache2::Const::DECLINED;
}
1;
如果请求是对匹配“/*.php”的 URL 的 GET,它将直接调用 Grails 服务器上的安全页面并验证它是否返回文本“ALLOWED”。如果对 Grails 页面的调用没有,则意味着用户未通过身份验证并将他们重定向到 Spring Security 登录页面。
唯一棘手的部分是从请求中获取会话 ID,并在您调用安全检查 URL 时将其包括在内。它假定会话 ID 存储在 cookie JSESSIONID 中,并且您的 Tomcat 设置为允许在 URL 中传递会话 ID。
Apache 设置需要安装 mod_perl,并且配置与此类似。
# location of the mod_perl handler (or use the default locations)
PerlSwitches -I/usr/local/somewhere
<VirtualHost *:80>
...
SetHandler modperl
PerlMapToStorageHandler MyModPerlFilter
...
# any proxy related stuff here
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>