千家信息网

Springboot中怎么配置ssl

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,Springboot中怎么配置ssl,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。SSL(Secure Sockets Layer
千家信息网最后更新 2025年02月03日Springboot中怎么配置ssl

Springboot中怎么配置ssl,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

SSL(Secure Sockets Layer 安全套接层)是为网络通信提供安全及数据完整性的一种安全协议,SSL在网络传输层对网络连接进行加密,SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通信提供安全支持。SSL协议分为两层,SSL记录协议建立在TCP之上,为高层协议提供数据封装、压缩、加密等基本功能支持。SSL握手协议建立在SSL记录协议之上,用户实际数据传输开始前进行身份验证、协商加密算法、交换加密秘钥。

1.生成证书,可以使自签名或者从SSL证书授权中心获得的。

JDK中keytool是一个证书管理工具,可以生成自签名证书。
本人这里用的系统是deepin,然后生成命令如下(找不到keytoo命令的先去配置java环境)
我指定的名字叫tomcat.keystore 别名叫tomcat,密码自己设置,我这里用的tomcat,最后那个直接按得回车

keytool -genkey -alias tomcat -keyalg RSA -keystore /home/gzr/tomcat.keystore

我的命令执行记录如下,(前提:在本地要创建目录,否则会失败)

结果是生成了对应的文件,如下:

#端口号server.port: 8443#你生成的证书名字server.ssl.key-store: E:\work\rave\tomcat.keystore#密钥库密码server.ssl.key-store-password: duan123server.ssl.keyStoreType: JKSserver.ssl.keyAlias: tomcat

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.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class SpringbootmyApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootmyApplication.class, args);    }    /**     * it's for set http url auto change to https     */    @Bean    public EmbeddedServletContainerFactory servletContainer(){        TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){            @Override            protected void postProcessContext(Context context) {                SecurityConstraint securityConstraint=new SecurityConstraint();                securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential                SecurityCollection collection=new SecurityCollection();                collection.addPattern("/*");                securityConstraint.addCollection(collection);                context.addConstraint(securityConstraint);            }        };        tomcat.addAdditionalTomcatConnectors(httpConnector());        return tomcat;    }    @Bean    public Connector httpConnector(){        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");        connector.setScheme("http");        connector.setPort(8080);        connector.setSecure(false);        connector.setRedirectPort(8443);        return connector;    }}

关于Springboot中怎么配置ssl问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

0