Skip to content

Commit 72ffe33

Browse files
committed
添加 https 集成
1 parent 0378ad0 commit 72ffe33

File tree

13 files changed

+376
-0
lines changed

13 files changed

+376
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<module>spring-boot-demo-ratelimit-guava</module>
6868
<module>spring-boot-demo-ratelimit-redis</module>
6969
<module>spring-boot-demo-elasticsearch-rest-high-level-client</module>
70+
<module>spring-boot-demo-https</module>
7071
</modules>
7172
<packaging>pom</packaging>
7273

spring-boot-demo-https/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.net.*;
18+
import java.io.*;
19+
import java.nio.channels.*;
20+
import java.util.Properties;
21+
22+
public class MavenWrapperDownloader {
23+
24+
private static final String WRAPPER_VERSION = "0.5.6";
25+
/**
26+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
27+
*/
28+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
30+
31+
/**
32+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
33+
* use instead of the default one.
34+
*/
35+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
36+
".mvn/wrapper/maven-wrapper.properties";
37+
38+
/**
39+
* Path where the maven-wrapper.jar will be saved to.
40+
*/
41+
private static final String MAVEN_WRAPPER_JAR_PATH =
42+
".mvn/wrapper/maven-wrapper.jar";
43+
44+
/**
45+
* Name of the property which should be used to override the default download url for the wrapper.
46+
*/
47+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
48+
49+
public static void main(String args[]) {
50+
System.out.println("- Downloader started");
51+
File baseDirectory = new File(args[0]);
52+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
53+
54+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
55+
// wrapperUrl parameter.
56+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57+
String url = DEFAULT_DOWNLOAD_URL;
58+
if (mavenWrapperPropertyFile.exists()) {
59+
FileInputStream mavenWrapperPropertyFileInputStream = null;
60+
try {
61+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62+
Properties mavenWrapperProperties = new Properties();
63+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65+
} catch (IOException e) {
66+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67+
} finally {
68+
try {
69+
if (mavenWrapperPropertyFileInputStream != null) {
70+
mavenWrapperPropertyFileInputStream.close();
71+
}
72+
} catch (IOException e) {
73+
// Ignore ...
74+
}
75+
}
76+
}
77+
System.out.println("- Downloading from: " + url);
78+
79+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80+
if (!outputFile.getParentFile().exists()) {
81+
if (!outputFile.getParentFile().mkdirs()) {
82+
System.out.println(
83+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84+
}
85+
}
86+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87+
try {
88+
downloadFileFromURL(url, outputFile);
89+
System.out.println("Done");
90+
System.exit(0);
91+
} catch (Throwable e) {
92+
System.out.println("- Error downloading");
93+
e.printStackTrace();
94+
System.exit(1);
95+
}
96+
}
97+
98+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100+
String username = System.getenv("MVNW_USERNAME");
101+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102+
Authenticator.setDefault(new Authenticator() {
103+
@Override
104+
protected PasswordAuthentication getPasswordAuthentication() {
105+
return new PasswordAuthentication(username, password);
106+
}
107+
});
108+
}
109+
URL website = new URL(urlString);
110+
ReadableByteChannel rbc;
111+
rbc = Channels.newChannel(website.openStream());
112+
FileOutputStream fos = new FileOutputStream(destination);
113+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114+
fos.close();
115+
rbc.close();
116+
}
117+
118+
}
49.5 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

spring-boot-demo-https/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Getting Started
2+
3+
### Reference Documentation
4+
For further reference, please consider the following sections:
5+
6+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/maven-plugin/)
8+
9+
10+
11+
1. 首先使用jdk 自带的keytool 命令生成证书(一般在用户目录下C:\Users\Administrator\server.keystore) 复制到项目中
12+
> 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会
13+
14+
![ssl 命令截图](ssl.png)
15+
16+
17+
2. 然后添加配置
18+
```yml
19+
server:
20+
ssl:
21+
# 证书路径
22+
key-store: spring-boot-demo-https\src\main\resources\server.keystore
23+
key-alias: tomcat
24+
enabled: true
25+
key-store-type: JKS
26+
#与申请时输入一致
27+
key-store-password: 123456
28+
# 浏览器默认端口 和 80 类似
29+
port: 443
30+
#debug: true
31+
32+
33+
```
34+
35+
3. 需要与http 自动跳转再添加bean
36+
37+
```java
38+
39+
@Bean
40+
public Connector connector(){
41+
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
42+
connector.setScheme("http");
43+
connector.setPort(80);
44+
connector.setSecure(false);
45+
connector.setRedirectPort(443);
46+
return connector;
47+
}
48+
49+
@Bean
50+
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
51+
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
52+
@Override
53+
protected void postProcessContext(Context context) {
54+
SecurityConstraint securityConstraint=new SecurityConstraint();
55+
securityConstraint.setUserConstraint("CONFIDENTIAL");
56+
SecurityCollection collection=new SecurityCollection();
57+
collection.addPattern("/*");
58+
securityConstraint.addCollection(collection);
59+
context.addConstraint(securityConstraint);
60+
}
61+
};
62+
tomcat.addAdditionalTomcatConnectors(connector);
63+
return tomcat;
64+
}
65+
66+
```
67+
68+

spring-boot-demo-https/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.xkcoding</groupId>
7+
<artifactId>spring-boot-demo-https</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>spring-boot-demo-https</name>
10+
<description>Demo project for Spring Boot</description>
11+
12+
13+
<parent>
14+
<groupId>com.xkcoding</groupId>
15+
<artifactId>spring-boot-demo</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
<exclusions>
39+
<exclusion>
40+
<groupId>org.junit.vintage</groupId>
41+
<artifactId>junit-vintage-engine</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.xkcoding.springbootdemohttps;
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.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
10+
import org.springframework.context.annotation.Bean;
11+
12+
13+
/**
14+
* <p>
15+
* SpringBoot启动类
16+
* </p>
17+
*
18+
* @package: com.xkcoding.https
19+
* @description: SpringBoot启动类
20+
* @author: Chen.Chao
21+
* @date 2020.01.12 10:31 am
22+
* @copyright: Copyright (c)
23+
* @version: V1.0
24+
* @modified: Chen.Chao
25+
*/
26+
@SpringBootApplication
27+
public class SpringBootDemoHttpsApplication {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(SpringBootDemoHttpsApplication.class, args);
31+
}
32+
33+
34+
@Bean
35+
public Connector connector(){
36+
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
37+
connector.setScheme("http");
38+
connector.setPort(80);
39+
connector.setSecure(false);
40+
connector.setRedirectPort(443);
41+
return connector;
42+
}
43+
44+
@Bean
45+
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
46+
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
47+
@Override
48+
protected void postProcessContext(Context context) {
49+
SecurityConstraint securityConstraint=new SecurityConstraint();
50+
securityConstraint.setUserConstraint("CONFIDENTIAL");
51+
SecurityCollection collection=new SecurityCollection();
52+
collection.addPattern("/*");
53+
securityConstraint.addCollection(collection);
54+
context.addConstraint(securityConstraint);
55+
}
56+
};
57+
tomcat.addAdditionalTomcatConnectors(connector);
58+
return tomcat;
59+
}
60+
61+
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server:
2+
ssl:
3+
# 证书路径
4+
key-store: spring-boot-demo-https\src\main\resources\server.keystore
5+
key-alias: tomcat
6+
enabled: true
7+
key-store-type: JKS
8+
#与申请时输入一致
9+
key-store-password: 123456
10+
# 浏览器默认端口 和 80 类似
11+
port: 443
12+
#debug: true
2.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)