springboot2.1.5-https和http訪問

  1. 須要創建父子maven結構java

    1. 父maven pom文件mysql

      <?xml version="1.0" encoding="UTF-8"?>
      	<project xmlns="http://maven.apache.org/POM/4.0.0"
      			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      			 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      		<modelVersion>4.0.0</modelVersion>
      
      		<groupId>tom.heliming.cloud</groupId>
      		<artifactId>cloud-demo</artifactId>
      		<packaging>pom</packaging>
      		<version>1.0-SNAPSHOT</version>
      		<modules>
      			<!--<module>oracle</module>-->
      			<!--<module>mongodb</module>-->
      			<!--<module>okhttp</module>-->
      			<!--<module>netty-demo</module>-->
      			<!--<module>mysqldemo</module>-->
      			<!--<module>rabbitmq</module>-->
      			<!--<module>memcache</module>-->
      			<!--<module>tarsdemo</module>-->
      			<!--<module>agentdemo</module>-->
      			<!--<module>netty</module>-->
      			<!--<module>mybatis</module>-->
      			<!--<module>avro</module>-->
      			<!--<module>mina</module>-->
      			<module>redis</module>
      			<!--<module>quartz</module>-->
      			<!--<module>jvm</module>-->
      			<!--<module>activemq</module>-->
      		</modules>
      		<parent>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-parent</artifactId>
      			<version>2.1.5.RELEASE</version>
      			<relativePath/> <!-- lookup parent from repository -->
      		</parent>
      		<properties>
      			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      			<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      			<maven.compiler.source>1.8</maven.compiler.source>
      			<maven.compiler.target>1.8</maven.compiler.target>
      			<java.version>1.8</java.version>
      		</properties>
      		<dependencies>
      
      			<!-- Spring Boot相關依賴 -->
      			<dependency>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-starter</artifactId>
      			</dependency>
      			<dependency>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-starter-web</artifactId>
      				<exclusions>
      					<exclusion>
      						<groupId>org.springframework.boot</groupId>
      						<artifactId>spring-boot-starter-tomcat</artifactId>
      					</exclusion>
      				</exclusions>
      			</dependency>
      			<!--war包方式-->
      			<dependency>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-starter-tomcat</artifactId>
      				<!--打包的時候能夠不用包進去,別的設施會提供。事實上該依賴理論上能夠參與編譯,測試,運行等週期。
      					至關於compile,可是打包階段作了exclude操做-->
      				<scope>provided</scope>
      			</dependency>
      		   <!-- 不加這個是由於加了這個只能加上tomcat之類的容器啓動不能用java -jar *.war啓動 -->
      			<!--<dependency>-->
      				<!--<groupId>javax.servlet</groupId>-->
      				<!--<artifactId>servlet-api</artifactId>-->
      				<!--<version>2.5</version>-->
      			<!--</dependency>-->
      			<!--<dependency>-->
      				<!--<groupId>org.springframework.boot</groupId>-->
      				<!--<artifactId>spring-boot-starter-jetty</artifactId>-->
      			<!--</dependency>-->
      
      			<dependency>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-starter-test</artifactId>
      				<!-- <scope>test</scope>-->
      			</dependency>
      
      
      		</dependencies>
      
      	</project>
  2. 子maven項目結構web

    1. 子maven pom文件redis

      <?xml version="1.0" encoding="UTF-8"?>
      	<project xmlns="http://maven.apache.org/POM/4.0.0"
      			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      			 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      		<parent>
      			<artifactId>cloud-demo</artifactId>
      			<groupId>tom.heliming.cloud</groupId>
      			<version>1.0-SNAPSHOT</version>
      		</parent>
      		<modelVersion>4.0.0</modelVersion>
      
      		<groupId>tom.heliming.redis</groupId>
      		<artifactId>redis</artifactId>
      		<dependencies>
      
      			<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
      			<dependency>
      				<groupId>redis.clients</groupId>
      				<artifactId>jedis</artifactId>
      				<version>2.9.0</version>
      			</dependency>
      		</dependencies>
      		<!-- 打包設置啓動springboot的main函數 -->
      		<build>
      			<finalName>redis</finalName>
      			<plugins>
      				<plugin>
      					<groupId>org.springframework.boot</groupId>
      					<artifactId>spring-boot-maven-plugin</artifactId>
      					<executions>
      						<execution>
      							<goals>
      								<goal>repackage</goal>
      							</goals>
      						</execution>
      					</executions>
      					<configuration>
      						<includeSystemScope>true</includeSystemScope>
      					</configuration>
      				</plugin>
      			</plugins>
      		</build>
      
      	</project>
    2. 生成證書(server.jks)放到項目的resources下config下spring

      keytool -genkeypair -alias springboot -keyalg RSA -dname "CN=SERVER1,OU=Unit,O=Elim,L=City,S=Province,C=CN" -keypass 123456 -keystore server.jks -storepass 123456 -storetype jks
    3. application.ymlsql

      server:
      	  port: 8443
      	  ssl:
      		enabled: true
      		key-alias: springboot
      		key-password: 123456
      		key-store-password: 123456
      		key-store: classpath:config/server.jks
      		key-store-type: JKS
    4. RedisApp.javamongodb

      package top;
      
      	import org.springframework.boot.SpringApplication;
      	import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      	/**
      	 * description:
      	 *
      	 * @author: dawn.he QQ:       905845006
      	 * @email: dawn.he@cloudwise.com
      	 * @email: 905845006@qq.com
      	 * @date: 2019/9/6    10:07 AM
      	 */
      	@SpringBootApplication
      	public class RedisApp {
      
      		public static void main(String[] args) {
      			SpringApplication.run(RedisApp.class,args);
      		}
      	}
    5. TomcatConfiguration.javaapache

      package top.conf;
      
      	import org.apache.catalina.connector.Connector;
      	import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
      	import org.springframework.context.annotation.Bean;
      	import org.springframework.context.annotation.Configuration;
      
      	/**
      	 * description:
      	 *
      	 * @author: dawn.he QQ:       905845006
      	 * @email: dawn.he@cloudwise.com
      	 * @email: 905845006@qq.com
      	 * @date: 2019/9/27    8:41 AM
      	 */
      	@Configuration
      	public class TomcatConfiguration {
      
      		@Bean
      		public TomcatServletWebServerFactory servletContainer(){
      			TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
      			tomcat.addAdditionalTomcatConnectors(this.httpConnector());
      			return tomcat;
      		}
      
      		private Connector httpConnector(){
      			Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
      			connector.setScheme("http");
      			connector.setPort(8081);
      			connector.setSecure(false);
      			return connector;
      		}
      
      	}
    6. HelloController.javaapi

      package top.web;
      
      	import org.springframework.http.HttpStatus;
      	import org.springframework.http.ResponseEntity;
      	import org.springframework.web.bind.annotation.GetMapping;
      	import org.springframework.web.bind.annotation.RequestMapping;
      	import org.springframework.web.bind.annotation.RestController;
      
      	import static top.activemq.util.RedisUtil.redisdemo;
      
      	/**
      	 * description:
      	 *
      	 * @author: dawn.he QQ:       905845006
      	 * @email: dawn.he@cloudwise.com
      	 * @email: 905845006@qq.com
      	 * @date: 2019/9/6    10:56 AM
      	 */
      	@RestController
      	@RequestMapping("/redis")
      	public class HelloController {
      
      		@GetMapping("/all")
      		public ResponseEntity<String> hello() throws InterruptedException {
      
      			try {
      				int i = 1/0;
      				redisdemo();
      
      			}catch (Exception e ){
      				System.out.println("-----");
      
      			}
      	//        return "redis:success";
      
      			return new ResponseEntity<String>("sss",HttpStatus.INTERNAL_SERVER_ERROR);
      		}
      	}

    訪問:tomcat

    http://localhost:8081/redis/all

    https://localhost:8443/redis/all

  3. 1

    改成tomcat 8.5 外置配置, 只需在tomcat 的conf/server.xml中加入

    <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> 
    	<SSLHostConfig> 
    	<!-- <Certificate certificateKeystoreFile="/Users/heliming/Downloads/apache-tomcat-8.5.42/cert/2877024_heliming.top.pfx" certificateKeyAlias="tomcat" certificateKeystorePassword="pGN2V910" type="RSA" />  -->
    	<Certificate certificateKeystoreFile="/Users/heliming/IdeaProjects/democloud/redis/src/main/resources/config/server.jks" certificateKeyAlias="springboot" certificateKeystorePassword="123456" type="RSA" /> 
    	</SSLHostConfig>
    	</Connector>

    去除項目的中的配置

    訪問 :https://localhost/redis/all 默認爲443端口

    springboot不一樣版本參考:https://blog.csdn.net/elim168/article/details/92689604 https://memorynotfound.com/spring-boot-configure-tomcat-ssl-https/

    後期改成阿里雲生成的證書,暫時用jdk生成的。

相關文章
相關標籤/搜索