1 簡介
Spring WebFlux
是一個新興的技術,Spring
團隊把寶都壓在響應式Reactive
上了,於是推出了全新的Web
實現。本文不討論響應式編程,而是通過實例講解Springboot WebFlux
如何把http
重定向到https
。
作為餐前小吃,建議大家先吃以下https
小菜,以幫助理解:
(2)HTTPS之密鑰知識與密鑰工具Keytool和Keystore-Explorer
(3)Springboot以Tomcat為容器實現http重定向到https的兩種方式
(4)Springboot以Jetty為容器實現http重定向到https
(5)nginx開啟ssl並把http重定向到https的兩種方式
2 搭建WebFlux項目
引入WebFlux
的依賴如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
實現Controller
,與之前略有不同,返回值為Mono<T>
:
@RestController public class HelloController { @GetMapping("/hello") public Mono<String> hello() { return Mono.just("Welcome to www.pkslow.com"); } }
配置文件與普通的Springboot
項目沒什么差別,配置了兩個端口,並配置SSL
相關參數:
server.port=443
http.port=80
server.ssl.enabled=true
server.ssl.key-store-type=jks
server.ssl.key-store=classpath:localhost.jks
server.ssl.key-store-password=changeit
server.ssl.key-alias=localhost
3 重定向實現
主要做了兩件事:
(1)在有https
的情況下,啟動另一個http
服務,主要通過NettyReactiveWebServerFactory
來生成一個Web
服務。
(2)把http
重定向到https
,這里做了路徑判斷,加了一個簡單的過濾函數。通過提供一個新的HttpHandler
來實現重定向。
package com.pkslow.ssl.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.HttpHandler; import reactor.core.publisher.Mono; import javax.annotation.PostConstruct; import java.net.URI; import java.net.URISyntaxException; @Configuration public class HttpToHttpsConfig { @Value("${server.port}") private int httpsPort; @Value("${http.port}") private int httpPort; @Autowired private HttpHandler httpHandler; @PostConstruct public void startRedirectServer() { NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(httpPort); factory.getWebServer( (request, response) -> { URI uri = request.getURI(); URI httpsUri; try { if (isNeedRedirect(uri.getPath())) { httpsUri = new URI("https", uri.getUserInfo(), uri.getHost(), httpsPort, uri.getPath(), uri.getQuery(), uri.getFragment()); response.setStatusCode(HttpStatus.MOVED_PERMANENTLY); response.getHeaders().setLocation(httpsUri); return response.setComplete(); } else { return httpHandler.handle(request, response); } } catch (URISyntaxException e) { return Mono.error(e); } } ).start(); } private boolean isNeedRedirect(String path) { return !path.startsWith("/actuator"); } }
4 總結
本文詳細代碼可在南瓜慢說公眾號回復<SpringbootSSLRedirectWebFlux>獲取。