java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot配置ssl支持https

springboot如何配置ssl支持https

作者:GKFreewind

在SpringBoot应用中配置SSL支持HTTPS需要创建KeyStore并在application.yml中进行相应配置,首先,使用java的keytool工具创建KeyStore,这涉及到设置密钥对、指定密钥算法(RSA)、密钥大小(2048位)、密钥库名称、证书有效期等,创建KeyStore后

springboot 配置ssl支持https

以下信息请根据实际情况修改!!!

创建keystore

KeyStore是一个用于存储和管理加密密钥和证书的存储库。

它通常被用于各种应用和系统中,以保护敏感数据和确保安全通信。

KeyStore可以被看作是一种加密的“保险箱”,用于存放密钥、密钥对和证书等安全相关的数据。

通过java的keytool创建keyStore

keytool -genkeypair -alias mykey -keyalg RSA -keysize 2048 -keystore keystore.jks -validity 365 -storepass mypassword -keypass mypassword -dname "CN=localhost, OU=MyOrg, O=MyOrg, L=MyCity, ST=MyState, C=MyCountry"

解释各个参数:

在springboot的application.yml配置文件中配置相关内容

server:  
  port: 8443 # HTTPS的默认端口是443,但443端口通常需要管理员权限才能绑定  
  ssl:  
    key-store: classpath:keystore.jks # 密钥库的位置,相对于classpath  对应keytool生成时的-keystore
    key-store-type: JKS # 密钥库类型,默认为JKS  
    key-store-password: mypassword # 密钥库密码  对应keytool生成时的-storepass
    key-alias: mykey # 密钥别名  对应keytool生成时的-alias
    key-password: mypassword # 密钥别名  对应keytool生成时的-keypass
    # 如果你使用的是PKCS12密钥库,则需要如下配置,相应信息根据实际情况修改
    # key-store-type: PKCS12  
    # key-store: classpath:keystore.p12  
    # key-store-password: yourpassword  
    # 如果需要,还可以配置信任库  
    # trust-store: classpath:truststore.jks  
    # trust-store-password: trustpassword  
    # trust-store-type: JKS  
    # 启用SSL客户端认证(可选)  
    # client-auth: need

springboot应用支持https实现

jdk自带证书生成

切换到jdk目录下bin目录,执行

列:

keytool -genkey -alias lyc -dname "CN=l,OU=SC,O=yd,L=lc,ST=sc,C=CN" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 365

配置https协议

springboot+tomcat,增加代码configuration配置项如下:

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatHttpsConfig {

    @Bean
    public Connector connector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(80);
        connector.setRedirectPort(443);
        return connector;
    }

    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
        TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection securityCollection = new SecurityCollection();
                securityCollection.addPattern("/*");
                securityConstraint.addCollection(securityCollection);
                context.addConstraint(securityConstraint);
            }
        };
        webServerFactory.addAdditionalTomcatConnectors(connector);
        return webServerFactory;
    }

}

springboot+undertow,增加代码configuration配置项如下:

import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

public class UndertowHttpsConfig {
    private int httpPort = 80;
    private int httpsPort = 443;

    @Bean
    public ServletWebServerFactory undertowFactory() {
        UndertowServletWebServerFactory undertowFactory = new UndertowServletWebServerFactory();
        undertowFactory.addBuilderCustomizers((Undertow.Builder builder) -> {
            builder.addHttpListener(httpPort, "0.0.0.0");
            // 开启HTTP2
            builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
        });
        undertowFactory.addDeploymentInfoCustomizers(deploymentInfo -> {
            // 开启HTTP自动跳转至HTTPS
            deploymentInfo.addSecurityConstraint(new SecurityConstraint()
                    .addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*"))
                    .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
                    .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
                    .setConfidentialPortManager(exchange -> httpsPort);
        });
        return undertowFactory;
    }
}

完成以上配置之后,启动项目

访问:https://localhost:443/test/test实现springboot的https协议访问

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文