Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring Security的HTTPS登录重定向到HTTP_Spring_Ssl_Https_Spring Security_Tomcat6 - Fatal编程技术网

使用Spring Security的HTTPS登录重定向到HTTP

使用Spring Security的HTTPS登录重定向到HTTP,spring,ssl,https,spring-security,tomcat6,Spring,Ssl,Https,Spring Security,Tomcat6,我有一个SpringWeb应用程序,由SpringSecurity保护,运行在EC2上。EC2实例前面是一个具有SSL证书的弹性负载平衡器(https终止于负载平衡器,即端口443->端口80),因此从Tomcat的角度来看,入站请求是HTTP 我的登录表单提交到https,但随后重定向到http(成功或失败)。身份验证成功,我可以返回https并登录 我的登录配置如下所示: <security:form-login default-target-url="/home" l

我有一个SpringWeb应用程序,由SpringSecurity保护,运行在EC2上。EC2实例前面是一个具有SSL证书的弹性负载平衡器(https终止于负载平衡器,即端口443->端口80),因此从Tomcat的角度来看,入站请求是HTTP

我的登录表单提交到https,但随后重定向到http(成功或失败)。身份验证成功,我可以返回https并登录

我的登录配置如下所示:

<security:form-login
    default-target-url="/home"
    login-page="/"
    login-processing-url="/processlogin"
    authentication-failure-url="/?login_error=1"/>

要使默认目标url和身份验证失败url变为https,我需要更改什么

  • 雄猫6
  • SpringSecurity 3.0.x

我实现此功能的一种方法是添加以下配置

<http auto-config="true" use-expressions="true" entry-point-ref="authenticationEntryPoint" >
    <form-login login-page="/login.jsf" authentication-failure-url="/login.jsf?login_error=t" always-use-default-target="true" default-target-url="xxxxx" />
    <logout logout-url="/logout" logout-success-url="/logoutSuccess.jsf" />
    ...
</http>

...
必须添加
始终使用default target=“true”
默认目标url=”https://....“
。这不是理想的方式,因为您需要在配置中硬编码url。

I set要求在所有截取url上使用channel=“any”。这允许它在我不使用SSL的开发环境中仍然工作

<intercept-url pattern="/createUser" access="permitAll" requires-channel="any"/>
<intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" requires-channel="any"/>
<intercept-url pattern="/**" access="isAuthenticated()" requires-channel="any"/>

然后,创建一个apache虚拟主机,将所有流量重定向到HTTPS版本

<VirtualHost *:80>
  ServerName www.mywebsite.com
  Redirect permanent / https://www.mywebsite.com/
</VirtualHost>

服务器名www.mywebsite.com
重定向永久/https://www.mywebsite.com/

您的spring配置应该与所使用的协议无关。如果您使用“requires channel”之类的东西,您迟早会遇到问题,特别是如果您希望将同一应用程序部署到没有https的开发环境中

请考虑正确配置Tomcat。你可以用它来做这件事。根据loadbalancer发送的标头,server.xml配置需要包含以下内容:

<Valve
   className="org.apache.catalina.valves.RemoteIpValve"
   internalProxies=".*"
   protocolHeader="X-Forwarded-Proto"
   httpsServerPort="443"
   />

Spring将根据ServletRequest确定绝对重定向地址,因此,如果您使用的不是443,请更改httpsServerPort:

httpsServerPort是由返回的端口 当protocolHeader指示https时,ServletRequest.getServerPort() 协议


我也面临着完全相同的问题,在得到正确的解决方案之前,我正在通过AJP而不是HTTP将我的请求从代理服务器重定向到tomcat服务器。 下面是我的apache配置

ProxyPass /myproject ajp://localhost:8009/myproject
ProxyPassReverse /myproject ajp://localhost:8009/myproject

在web.xml中使用以下代码行

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Login and Restricted Space URLs</web-resource-name>
    <url-pattern>/j_security_check</url-pattern>
    <url-pattern>/loginpage.rose</url-pattern>
  </web-resource-collection>
  <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>
</security-constraint>

登录和受限空间URL
/j_安全检查
/罗金帕吉玫瑰酒店
保密的

它会强制使用HTTPS。

如果它是一个Spring Boot应用程序(我目前使用的是2.0.0版本),则
应用程序中的以下配置。属性
文件应该足够了:

server.tomcat.protocol-header=x-forwarded-proto
这对我在AWS上使用负载平衡器很有效


对于Spring Boot<2.0.0,它也应该可以工作(未经测试)

我在Google Kubernetes后面的Spring Boot也有同样的问题。将这两行添加到application.properties为我做到了这一点

server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto

来源:

在我的例子中,我必须删除属性
服务器。使用forwardheaders=true

这是我的设置:

Digital Ocean LB-->Kubernetes群集和Ingress-->Spring boot应用程序解决方案有两个方面

(1) application.yml

server:
  use-forward-headers: true
(2) 在服务器中
/etc/apache2/sites enabled/oow.com le ssl.conf

RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
(2.1)并使用

sudo a2enmod headers

同时,在和的帮助下,我已经设置了一个Apache重写,但仍然很好奇是否有办法在Spring Security config中完成。通常在负载平衡器上完成,配置称为url重写。通过这种方式,负载平衡器确保重定向保持在https。登录后,Spring仍将发送带有http地址的位置头。用户不会注意到,因为您的apache会立即再次将他重定向到https,但这仍然是一个严重的安全问题,因为浏览器会通过不安全的通道发送会话cookies。@marcelj-我刚刚通过firebug运行了它,您是对的。我会尝试找出更好的解决方案,如果我这样做,我会修改我的答案。@marcelj,你能解释为什么这是一个严重的安全风险,或者提供一个描述风险的参考吗?@NathanWard如果攻击者截获HTTP请求,她可以提供HTTP登录表单并检索凭据。这个问题让我发疯。这个建议非常有效。谢谢谢谢我还必须添加服务器。use forward headers=true从servlet请求加载http方案的实际代码可以从以下位置找到:从那里可以发现,Spring实际上从servlet请求收集http方案。所以@marcelj solution是这个问题的完美答案<代码>服务器。使用转发头=trueis。相反,请使用
server.forward headers strategy=native
,不要将其设置为
framework
,除非您信任反向代理;-)。我们在chrome上遇到了错误“您将要提交的信息不安全”。为了解决这个问题,我们添加了server.forward-headers-strategy:native To application.yml,并在docker compose“traefik.frontend.headers.customResponseHeaders=X-Forwarded-Proto:https | | X-Forwarded-Port:443”中为traefik添加了以下行