我已经使用Spring Boot部署了一个项目。我想将所有流量从HTTP重定向到HTTPS。由于某些原因,重定向无法正常工作。如果我尝试使用Chrome访问HTTP网站,则会出现“连接超时”的消息。
现在,如果我尝试通过HTTPS访问该网站,则chrome能够加载该网站。初始加载HTTPS网站后,重定向便开始起作用。即HTTP流量已成功重定向到HTTPS。
您可以在隐身模式下使用chrome尝试上述情况。
场景1
场景2
我想知道为什么在方案1中重定向不起作用。
这是我的代码:
@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);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
};
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
Application.properties
server.port = 443
build.gradle
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.6.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.2.6.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.2.6.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.6.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.2.6.RELEASE'
答案 0 :(得分:1)
端口80(HTTP)无法访问或至少没有响应。我是通过命令行完成的:
$ telnet timelines.co 80
该命令刚刚挂起。当客户端和服务器之间存在某种类型的防火墙时,这很常见。您询问是否知道服务器在AWS上。只需做一个:
$ host timelines.co
timelines.co has address 54.203.56.245
然后
$ host 54.203.56.245
245.56.203.54.in-addr.arpa domain name pointer ec2-54-203-56-245.us-west-2.compute.amazonaws.com.
因此,您在us-west-2
AWS区域中的EC2上。
设置安全组时,尤其是更改内容时,很容易错过端口。
我不完全了解浏览器在做什么。我觉得有时候它们是“有帮助的”,并且在此过程中掩盖了根本原因。例如,Chrome和Firefox首先假设https
。因此,如果您只输入主机名,那么默认情况下您打开的端口443将为您工作。这就是为什么我使用较低级别的工具(远程登录)来帮助对其进行调试的原因。它不会试图猜测我真正想做什么。