zoukankan      html  css  js  c++  java
  • Springboot-WebFlux实现http重定向到https

    1 简介

    Spring WebFlux是一个新兴的技术,Spring团队把宝都压在响应式Reactive上了,于是推出了全新的Web实现。本文不讨论响应式编程,而是通过实例讲解Springboot WebFlux如何把http重定向到https

    spring mvc and webflux venn

    作为餐前小吃,建议大家先吃以下https小菜,以帮助理解:

    (1)Springboot整合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获取更多精彩文章!

    欢迎关注微信公众号<南瓜慢说>,将持续为你更新...

    多读书,多分享;多写作,多整理。

  • 相关阅读:
    chmod: changing permissions of ‘/etc/fstab': Read-only file system
    让Mac终端保持(SSH)与远程的连接状态
    修改数据库密码
    SSH密钥登录让Linux VPS/服务器更安全
    redis 使用redis Desktop manger进行远程进行链接
    mysql 性能
    对阿里云服务器(数据盘已分区并格式化)的数据盘进行扩容
    备份数据库的shell
    window上可以执行的shell脚本,复制到linux上执行报错了
    自然语言处理中的N-Gram模型
  • 原文地址:https://www.cnblogs.com/larrydpk/p/12824493.html
Copyright © 2011-2022 走看看