springboot的HTTP与HTTPS

本文旨在教学https等相关信息。

1. keytool 生成证书

windows下的生成:

  keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

linux下的命令:

keytool -genkey -alias tomcat -keyalg RSA -validity 20000 -keystore keystore.p12 
springboot的HTTP与HTTPS_第1张图片
windows下生成.png

执行完后会生成一个文件:keystore.p12

关于keytool的说明:
keytool -genkey -alias 你的证书别名 -keyalg 密钥算法 -keystore 证书库文件保存的位置和文件名 -keysize 密钥长度 -validity 证书有效期天数

springboot编码

配置application.yml

server:
  port: 8081
  ssl:
    key-store: keystore.p12
    key-store-password: test08
    key-store-type: PKCS12
    key-alias: tomcat

将上面生成的keystore.p12文件移动到classpath下。

此时你的项目就可以使用https协议访问。

与http同时访问

如果你希望你的项目既可以http访问,也可以https访问,而且两个互不干扰:
在启动类下,public class KindoApplication extends SpringBootServletInitializer(继承SpringBootServletInitializer类):

  • springboot版本1.x:
@Bean
    public EmbeddedServletContainerFactory servletContainer() {


        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {

            @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);
            }
        };
        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());

        return tomcat;
    }


    private Connector initiateHttpConnector() {
        Connector connector = new Connector(
                "org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8082);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
  • springboot版本2.x:
@Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
        return tomcat;
    }

    private Connector createHTTPConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(8082);
        connector.setRedirectPort(8443);
        return connector;
    }

ps:注意:http的端口不要https相同,即上文的port设置,否则会出现端口占用错误。

http强制转换https访问

在用户用http访问的时候,强制转换为https。
基于上述模块与http同时访问,添加过滤器,强制重定向。


import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lingbao on 2018/1/23.
 *
 * @author lingbao
 * @Description
 * @Modify
 */
@Configuration
@WebFilter
public class KindoFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String requestURL = request.getRequestURL().toString();
        String protocol = requestURL.split("://")[0];
        if ("http".equals(protocol)) {
            requestURL = requestURL.replace("http", "https").replace("8082", "8081");
            response.sendRedirect(requestURL);
        }
        filterChain.doFilter(request, response);
    }
}

方法有点蠢,如果有好的方法,欢迎留言
有BUG也可以交流交流!谢谢!

参考文献:
https://www.jianshu.com/p/68d723431596。
https://www.jianshu.com/p/05c8be17c80a。

你可能感兴趣的:(springboot的HTTP与HTTPS)