spring boot starts with https

1. Generate a key file, keytool is a command of java

 

 

keytool -genkey -alias hpgary -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

   2. Put it under the resource source package of the spring boot project

    3. Modify the configuration file application.perproties file

    

server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=hpgary

  4. Define tomcat https bean

  

@Bean
	public EmbeddedServletContainerFactory servletContainer(){
		TomcatEmbeddedServletContainerFactory
		containerFactory = new TomcatEmbeddedServletContainerFactory(){
			@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) ;
	        }
		};
		containerFactory.addAdditionalTomcatConnectors( initiateHttpConnector() );
		return containerFactory ;
	}
	@Value("${server.port}")
	
	Integer serverPort ;
	private Connector initiateHttpConnector() {
		//http port 80 jumps to https port 8443
	    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	    connector.setScheme( "http" );
	    connector.setPort( 80 );
	    connector.setSecure( false );
	    connector.setRedirectPort (serverPort);
	    return connector;
	}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486203&siteId=291194637