Skip to content

Commit ee2ebcd

Browse files
committed
springboot-web-https
1 parent 55153ed commit ee2ebcd

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

springboot-web-https/.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-web-https/pom.xml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.4.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>net.codingme</groupId>
12+
<artifactId>springboot-web-https</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-https</name>
15+
<description>springboot-web-https</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<optional>true</optional>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.codingme.https;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootHttpsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootHttpsApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.codingme.https.config;
2+
3+
import org.apache.catalina.Context;
4+
import org.apache.catalina.connector.Connector;
5+
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
6+
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* <p>
14+
* HTTP 强制跳转 HTTPS
15+
*
16+
* @Author niujinpeng
17+
* @Date 2019/4/21 17:47
18+
*/
19+
@Configuration
20+
public class Http2Https {
21+
22+
@Value("${server.port}")
23+
private int sslPort;
24+
@Value("${server.http-port}")
25+
private int httpPort;
26+
27+
@Bean
28+
public TomcatServletWebServerFactory servletContainerFactory() {
29+
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
30+
@Override
31+
protected void postProcessContext(Context context) {
32+
SecurityConstraint securityConstraint = new SecurityConstraint();
33+
securityConstraint.setUserConstraint("CONFIDENTIAL");
34+
SecurityCollection collection = new SecurityCollection();
35+
collection.addPattern("/*");
36+
securityConstraint.addCollection(collection);
37+
context.addConstraint(securityConstraint);
38+
}
39+
};
40+
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
41+
connector.setScheme("http");
42+
connector.setPort(httpPort);
43+
connector.setRedirectPort(sslPort);
44+
tomcat.addAdditionalTomcatConnectors(connector);
45+
return tomcat;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.codingme.https.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* <p>
11+
* Https 接口控制类
12+
*
13+
* @Author niujinpeng
14+
* @Date 2019/4/20 22:59
15+
*/
16+
@RestController
17+
public class HttpsController {
18+
19+
@GetMapping(value = "/hello")
20+
public String hello() {
21+
return "Hello HTTPS";
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# \u914D\u7F6E HTTPS \u76F8\u5173\u4FE1\u606F
2+
server:
3+
port: 443
4+
http-port: 80
5+
ssl:
6+
enabled: true
7+
key-store: classpath:tomcat_https.keystore
8+
key-password: 123456
9+
key-store-type: JKS
10+
key-alias: tomcat_https
Binary file not shown.

0 commit comments

Comments
 (0)