Spring Security正在重定向到生产服务器上的localhost

时间:2010-10-07 18:50:27

标签: grails spring-security

我有一个安装了spring-security-core插件的grails应用程序。一切都在当地很好。我部署到临时服务器,一切正常。我部署到我们的生产服务器,它是我们的登台服务器的镜像。我可以很好地进入未受保护的页面。但是当Spring Security启动并尝试执行重定向时,它会重定向到localhost而不是grails.serverURL。

我打算尽可能高地记录日志并重新部署以确定我是否可以做任何事情的正面或反面。我会在这里发布我的发现。如果有人以前经历过这个并且知道可能发生了什么,请告诉我。此外,如果有任何需要查看的配置文件,我也可以提供。感谢。

更新 我将以下内容添加到底部Config.groovy

grails.plugins.springsecurity.useSecurityEventListener = true

grails.plugins.springsecurity.onAuthorizationEvent = { e, appCtx ->
   println "here"
   println e
}

在本地,当我尝试访问受保护的页面时,该闭包会被击中2次。一次为初始网址。第二次使用auth url。将它部署到我们的生产服务器,我什么也得不到。

4 个答案:

答案 0 :(得分:5)

重定向在org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint.commence() method中完成,因此如果您能够借用其中一个prod服务器进行调试,则可以在那里设置断点。

它根据登录表单uri(例如/ login / auth)构建重定向URL,但它使用request.getServerName(),因此它应与原始请求相同。请注意,grails.serverURL在此处没有任何影响,因为它使用请求的服务器名称,端口,上下文等构建URL。

将Apache或负载均衡器放在servlet容器前可能会受到影响,尽管我已经完成了两者并且工作正常。

您是否在resources.groovy中完成了可能影响此问题的任何bean自定义?

答案 1 :(得分:5)

如果您的应用程序服务器位于Web服务器后面,则此问题可能是由Web服务器配置引起的。我有同样的问题并通过在我的httpd.conf或apache2.conf中使用以下条目来纠正它。这是......

...这里的样板配置......

################################
# Begin yourdomain.com...      #
################################

ProxyRequests Off
ProxyPreserveHost On

<Proxy>
    Order deny,allow
    Allow from all
</Proxy>

ProxyPass / http://localhost:28080/
ProxyPassReverse / http://localhost:28080/

<Location>
    Order allow,deny
    Allow from all
</Location>

################################
# ... end yourdomain.com       #
################################

答案 2 :(得分:2)

假设您有一个Web服务器(apache,nginx等)作为Tomcat前面的代理(并且您使用的是Tomcat)......

在允许http和https的设置中,将单独的Connector元素添加到tomcat的conf / server.xml文件中:

<Connector port="8081" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443"  URIEncoding="UTF-8"
           scheme="https" secure="true" proxyName="somehostname.domain" proxyPort="443" />

如果只允许https,您可以将scheme,secure,proxyName和proxyPort属性添加到现有的Connector元素。

在apache配置中,使用额外属性将*:443虚拟主机代理设置为连接器。普通的http *:80可以连接到原始连接器。

了解更多信息: http://tomcat.apache.org/tomcat-7.0-doc/config/http.html#Proxy_Support http://tomcat.apache.org/tomcat-7.0-doc/proxy-howto.html

答案 3 :(得分:1)

我知道这是一个老问题,但我想添加我的发现以帮助可能遇到此问题的其他用户。

除了Burt的回答(我假设您使用的是tomcat),我发现request.getServerName()的返回值也可以通过server.xml设置

即在tomcat 8中 https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

在server.xml中有这一行

<Connector protocol="HTTP/1.1"
           port="8080" ...
proxyName="localhost"/>

将返回&#34; localhost&#34;当调用getServername时。