1.6SpringBoot之Tomcat证书配置

1.6SpringBoot之Tomcat证书配置,第1张

 控制台 cmd 后输入以下内容

 keytool -genkey -alias mthttps -keyalg RSA -keystore H:\TOMCAT.KEYSTORE -validity 365

然后将文件复制到resources根目录呢下,并在application.properties输入

#证书别名
server.ssl.key-alias=httpss
#证书名称
server.ssl.key-store=classpath:tomcat_keystre.p12
#密码
server.ssl.key-store-password=111111

加载之后输入URL直接访问是访问不到的

 需要在开头加入加https://

 选择接受风险就可以正常访问了

自动跳转

package com.example.demo02.config;

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;

/**
 * @ClassName: TomcatConfig
 * @Author: XIAO_骁
 * @Date: 2022/4/25 10:03
 * @Description: TODO
 */
@Configuration
public class TomcatConfig {
    @Bean
    TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        TomcatServletWebServerFactory factory= new TomcatServletWebServerFactory(){
            @Override
            protected void postProcessContext(org.apache.catalina.Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        factory.addAdditionalTomcatConnectors(createTomcatConnector());
        return factory;
    }

    private Connector createTomcatConnector() {

        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8081);
        connector.setSecure(false);
        connector.setRedirectPort(8080);
        return connector;
    }
}

这个会写下来就行,额。。。我也不理解,然后访问 localhost:8081/hello 就会自动跳转到https://localhost:8080/hello

有时候idea可能加载不到 reasource目录下的文件,解决方法


		
			
				src/main/java
				
					**/*.*
				
			
			
				src/main/resources
				
					**/*.*
				
			
		
	

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/788323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存