我有一个运行在Tomcat服务器(v8.5)上的Spring MVC(v4.3.1)Web应用程序,该服务器的前端是用作反向代理的Apache(v2.4)应用程序服务器。
我在Apache上安装了SSL证书,以便它处理所有安全(:443)和非安全(:80)传入请求,并将它们重定向到Tomcat实例。
我想处理与外界的所有安全通信(SSL)。但是在Apache和Tomcat之间,不需要通过安全端口进行通信。这就是Apache将所有 HTTP 请求重定向到 HTTPS
的原因我还使用Spring Security(v4)来处理所有用户授权/身份验证工作,因此我什至可以注册/登录/注销,等等。
我当前的服务器架构如下所示,
给出摘要后,问题是,
每当我尝试访问登录页面时,我都会从Chrome收到 ERR_TOO_MANY_REDIRECTS 错误。
此外,当我访问Tomcat管理器(http://myapp.net:8080/manager/html)并单击我的应用程序URL(/MyApp-1.0.0)时,**我可以成功查看**我的登录页面没有https。 (在单击部署在tomcat上的应用程序链接后,它将打开页面:http://myapp.net:8080/MyApp-1.0.0/login。)我相信它表明,当我的Web应用程序在tomcat实例上运行时,没有问题。他们在一起很好。它怀疑我的问题源于SSL的Apache。
虚拟主机的Apache配置:
Listen 80
Listen 443
<VirtualHost *:80>
ServerAdmin emrecaglar@gmail.com
ServerName myapp.net
ServerAlias www.myapp.net
Redirect / https://www.myapp.net/
</VirtualHost>
<VirtualHost *:443>
ServerName myapp.net
ServerAlias www.myapp.net
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/MyApp-1.0.0/
ProxyPassReverse / http://127.0.0.1:8080/MyApp-1.0.0/
SSLEngine on
SSLCertificateFile /root/WWW.myapp.NET.crt
SSLCertificateKeyFile /root/www.myapp.net.key
SSLCertificateChainFile /root/dv_chain.txt
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
除非关键,否则我不想使用重写规则。我想处理重定向。
Tomcat server.xml配置:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" />
没有为:8443 定义的连接器,因为我不希望Tomcat在安全端口上运行。 Tomcat仅与Apache服务器通信。
Spring Security配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/static/**").permitAll()
.antMatchers("/register*").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.defaultSuccessUrl("/view/home")
.permitAll()
.and()
.logout().logoutUrl("/logout").logoutSuccessUrl("/login?
logout").invalidateHttpSession(true).deleteCookies("auth_code",
"JSESSIONID").permitAll();
}
}
我还阅读了一些有关allowAll()的帖子,并且onymous()是不同的,因此allowAll()甚至可能触发重定向循环,因此对于登录页面,它应该是onymous(),但我无法对其进行验证。从春季安全性的角度来看,我认为它也需要扮演其他角色。
我的Spring控制器:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String homePage(Principal principal) {
/**
* Initialize session user if not initialized
*/
return "redirect:/view/home";
}
另一个:
@Controller
@RequestMapping(value = "/view")
public class ViewController
{
@RequestMapping(value = "/home")
public String getHomePage(Model model, Principal principal)
{
//some logic
return "home";
}
}
用于登录的Web MVC配置:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
我有相关的视图解析器和视图(home.jsp,login.jsp)
用于重定向的Chrome开发者控制台调试输出
General
Request URL: http://www.myapp.net/MyApp-1.0.0/login
Request Method: GET
Status Code: 302 Found
Remote Address: 207.154.208.158:80
Referrer Policy: no-referrer-when-downgrade
Response Header
HTTP/1.1 302 Found
Date: Sat, 17 Nov 2018 08:40:04 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://www.myapp.net/MyApp-1.0.0/login
Content-Length: 314
Keep-Alive: timeout=5, max=92
Connection: Keep-Alive
Request Header
Content-Type: text/html; charset=iso-8859-1
GET /MyApp-1.0.0/login HTTP/1.1
Host: www.myapp.net
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: JSESSIONID=8A4E001A841DBC4D55509605FF3E7E23
General
Request URL: https://www.myapp.net/MyApp-1.0.0/login
Request Method: GET
Status Code: 302
Remote Address: 207.154.208.158:443
Referrer Policy: no-referrer-when-downgrade
Response Header
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: Keep-Alive
Content-Length: 0
Date: Sat, 17 Nov 2018 08:40:04 GMT
Expires: 0
Keep-Alive: timeout=5, max=92
Location: http://www.myapp.net/MyApp-1.0.0/login
Pragma: no-cache
Server: Apache/2.4.18 (Ubuntu)
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Request Header
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Cookie: JSESSIONID=8A4E001A841DBC4D55509605FF3E7E23
Host: www.myapp.net
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
请求再次在这两个请求之间循环,从HTTPS到HTTP和HTPPS
Apache access.log
67.171.8.29 - - [17/Nov/2018:08:41:59 +0000] "GET /MyApp-1.0.0/login HTTP/1.1" 302 429 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
Apache error.log
[Sat Nov 17 08:37:59.376633 2018] [mpm_event:notice] [pid 20673:tid 140534533293952] AH00489: Apache/2.4.18 (Ubuntu) mod_jk/1.2.41 OpenSSL/1.0.2g configured -- resuming normal operations
[Sat Nov 17 08:37:59.376707 2018] [core:notice] [pid 20673:tid 140534533293952] AH00094: Command line: '/usr/sbin/apache2'
如您所见,我的spring mvs应用程序和tomcat没有任何与SSL相关的代码/配置。他们不需要意识到这一点,因为我希望apache只负责SSL和处理https请求并定向到Tomcat。
我在这里想念的东西是什么导致了重定向循环?
谢谢