【发布时间】:2019-05-15 08:51:28
【问题描述】:
我有一个用 Spring Boot (2.0.5) 编写的服务器。它位于提供 SSL 的代理服务器后面。代理接受 HTTP (80) 和 HTTPS (443) 并将两者都转发到我的服务器,该服务器仅接受端口 2222 上的 HTTP。代理设置以下请求标头。
- x-转发-for
- x-forwarded-proto
- x 转发端口
我在WebSecurityConfig 类中测试了以下代码,但没有成功。
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.requiresChannel().anyRequest().requiresSecure()
.and().
...
}
我还编写了以下代码来将 HTTP 重定向到 HTTPS。但它也会重定向 HTTPS 流量。但我只需要重定向 HTTP。
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpsConfiguration {
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
return tomcat;
}
}
我正在寻找一种检查请求标头的方法,如果是 HTTP,我想将其重定向到 HTTPS。
【问题讨论】:
标签: spring-boot spring-security