码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot开启HTTPS(九)

时间:2018-07-06 11:39:12      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:redirect   打开   protoc   lse   prot   desc   util   control   RoCE   

springboot 默认只开启 http请求,若是要开启https 需要生成证书和配置

 

1.首先打开CMD命令行工具,在JDK/bin目录下 生成证书.keystore

keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore F:/tomcat.keystore -storepass 123456

相关参数:

keytool 
-genkey 
-alias tomcat(别名) 
-keypass 123456(别名密码) 
-keyalg RSA(生证书的算法名称,RSA是一种非对称加密算法) 
-keysize 1024(密钥长度,证书大小) 
-validity 365(证书有效期,天单位) 
-keystore F:/tomcat.keystore(指定生成证书的位置和证书名称) 
-storepass 123456(获取keystore信息的密码)
- storetype (指定密钥仓库类型) 

技术分享图片

 

springboot配置ssl(证书为.keystore文件)

1.将tomcat.keystore拷贝到项目根目录下(跟pom文件同级目录)

2.配置application.properties

#端口号
server.port=8443
#你生成的证书名字
server.ssl.key-store=tomcat.keystore
#密钥库密码
server.ssl.key-store-password=123456
server.ssl.keyStoreType=JKS
server.ssl.keyAlias:tomcat

3.修改springboot运行主类:实现http自动跳转https

package com.mingtong.demo_client;

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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoClientApplication.class, args);
    }
    
     @GetMapping("/login")
    public String login() {
       return "login";
    }
     
     /**
         * 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;
        }
}

测试:访问http://localhost:8080/login,会自动跳转到https://localhost:8443/login

 

SpringBoot开启HTTPS(九)

标签:redirect   打开   protoc   lse   prot   desc   util   control   RoCE   

原文地址:https://www.cnblogs.com/wookong/p/9272215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!