先下载ssl证书,我的下载的是jks,阿里云腾讯云都可以,看你服务器绑定的哪的,下载过之后放到和配置文件统计的文件中
cloud项目直接再getway网关配置,下面是配置文件,
server: port: 443 http: port: 80 tomcat: # tomcat的URI编码 uri-encoding: UTF-8 # tomcat最大线程数,默认为200 max-threads: 800 # Tomcat启动初始化的线程数,默认值25 min-spare-threads: 30 #开启https认证配置 ssl: key-store: classpath:****.jks key-password: ***** key-store-type: JKS client-auth: none
然后http自动转向https的配置文件
@Configuration public class HttpToHttpsRedirectConfig { @Value("${server.http.port}") private int httpPort; @Value("${server.port}") private int serverPort; @PostConstruct public void startRedirectServer() { NettyReactiveWebServerFactory httpNettyReactiveWebServerFactory = new NettyReactiveWebServerFactory(httpPort); httpNettyReactiveWebServerFactory.getWebServer((request, response) -> { URI uri = request.getURI(); URI httpsUri; try { httpsUri = new URI("https", uri.getUserInfo(), uri.getHost(), serverPort, uri.getPath(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { return Mono.error(e); } response.setStatusCode(HttpStatus.MOVED_PERMANENTLY); response.getHeaders().setLocation(httpsUri); return response.setComplete(); }).start(); } }
配置完成之后,访问http路径直接会自动转为https ,直接访问https也可以