问题描述
根据上面非对称加密实战(二):解决web项目不支持https问题 【附源码】来讲,配好https后,http是无法访问的,那么要进行处理的话,需要加入拦截器,来进行https跳转
解决方案
在服务端加入拦截器,拦截所有的http请求重定向到https请求
修改application.yml
添加http端口
# http端口
http-port: 8082
# https端口
server:
port: 8081
ssl:
key-store: classpath:fd-alias.keystore
key-store-type: JKS
key-alias: fd-alias
key-store-password: 123456
key-password: 123456
加入拦截器,拦截所有请求
package com.springweb.intercept;
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.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* http强制跳转https
*/
@Configuration
public class HttpToHttps {
// https的端口
@Value("${server.port}")
private int sslPort;
// http的端口
@Value("${http-port}")
private int httpPort;
@Bean
public TomcatServletWebServerFactory servletContainerFactory() {
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);
}
};
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//遇到http进行转发
connector.setScheme("http");
//true: http使用http, https使用https;
//false: http重定向到https;
connector.setSecure(false);
// 设置http端口
connector.setPort(httpPort);
//重定向端口号(非SSL到SSL)
connector.setRedirectPort(sslPort);
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
我们直接请求8082端口,会直接跳转到8081端口,附截图