Springbootプロジェクト構成アリクラウドssl証明書

7418 ワード

  • 私はアリクラウドの無料ssl証明書を使って、申請して審査を通過すればいいSpringboot项目配置阿里云ssl证书_第1张图片
  • 署名してから先にローカルにダウンロードして、私のここはダウンロードしたtomcatのSpringboot项目配置阿里云ssl证书_第2张图片
  • です
  • 解凍してこの2つのものを得た:在这里插入图片描述
  • 将.pfxファイルはspringbootプロジェクトのresourcesディレクトリの下に置かれます:applicatio.ymlに構成を追加:
  • http:
      port: 80
    server:
      port: 443
      ssl:
        key-store: classpath:1655744_nichang.site.pfx
        key-store-password: xxxxxx(pfx-password.txt    )
        keyStoreType: PKCS12
    
  • httpからhttpへの自動リダイレクトを設定する(80ポート->443ポート):起動クラスに次のコードを追加します:(ネット上の一部のコードのE m b e d d d e d e d S v e r t ContainerFactoryが見つからないのはspringboot 2.Xの後に書き方が変更されたためです)
  •     @Bean
        public ServletWebServerFactory servletContainer() {
            TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
                @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(redirectConnector());
            return tomcat;
        }
    
        private Connector redirectConnector() {
            Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
            connector.setScheme("http");
            connector.setPort(80);
            connector.setSecure(false);
            connector.setRedirectPort(443);
            return connector;
        }
    
  • これにより、80ポートへのアクセス時にhttps
  • に自動的にリダイレクトすることができる.