内容简介:作为餐前小吃,建议大家先吃以下
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 >获取。
欢迎访问 南瓜慢说 www.pkslow.com 获取更多精彩文章!
欢迎关注微信公众号< 南瓜慢说 >,将持续为你更新...
多读书,多分享;多写作,多整理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- php如何实现session,自己实现session,laravel如何实现session
- AOP如何实现及实现原理
- webpack 实现 HMR 及其实现原理
- Docker实现原理之 - OverlayFS实现原理
- 为什么实现 .NET 的 ICollection 集合时需要实现 SyncRoot 属性?如何正确实现这个属性?
- 自己实现集合框架(十):顺序栈的实现
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法竞赛入门经典(第2版)
刘汝佳 / 清华大学出版社 / 2014-6-1 / CNY 49.80
《算法竞赛入门经典(第2版)》是一本算法竞赛的入门与提高教材,把C/C++语言、算法和解题有机地结合在一起,淡化理论,注重学习方法和实践技巧。全书内容分为12 章,包括程序设计入门、循环结构程序设计、数组和字符串、函数和递归、C++与STL入门、数据结构基础、暴力求解法、高效算法设计、动态规划初步、数学概念与方法、图论模型与算法、高级专题等内容,覆盖了算法竞赛入门和提高所需的主要知识点,并含有大量......一起来看看 《算法竞赛入门经典(第2版)》 这本书的介绍吧!