版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zuozewei/article/details/84727095 </div>
<div id="content_views" class="markdown_views prism-dracula">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
<p></p><div class="toc"><h3><a name="t0"></a>文章目录</h3><ul><ul><li><a href="#HTTPS_1" rel="nofollow" target="_self">HTTPS接口实现</a></li><ul><li><a href="#Spring_Boot_38" rel="nofollow" target="_self">新建Spring Boot项目</a></li><li><a href="#Entity_68" rel="nofollow" target="_self">编写Entity</a></li><li><a href="#_96" rel="nofollow" target="_self">统一异常处理</a></li><li><a href="#RESTful_API_216" rel="nofollow" target="_self">创建RESTful API</a></li><li><a href="#SSLHTTPS_254" rel="nofollow" target="_self">使用SSL-HTTPS</a></li><ul><li><a href="#SSL_260" rel="nofollow" target="_self">获取SSL证书</a></li><li><a href="#HTTPS_322" rel="nofollow" target="_self">启用HTTPS</a></li><li><a href="#HTTPHTTPS_341" rel="nofollow" target="_self">将HTTP请求重定向到HTTPS</a></li></ul><li><a href="#_385" rel="nofollow" target="_self">自定义启动标志</a></li><li><a href="#_391" rel="nofollow" target="_self">配置日志配置文件</a></li><li><a href="#_432" rel="nofollow" target="_self">配置数据库配置</a></li><li><a href="#_449" rel="nofollow" target="_self">启动并测试</a></li><li><a href="#_459" rel="nofollow" target="_self">完整的项目结构</a></li></ul></ul></ul></div><p></p>
HTTPS接口实现
在上文秒懂HTTPS接口(原理篇)中我们详细介绍了HTTPS协议原理,下面我们来实践使用Java实现一个简单HTTPS接口示例
项目结构:
springbootdemo
├─config 配置信息类
├─controller 控制器类
├─entity 实体类
├─enums 枚举类
├─exception 异常类
├─handler 捕获类
├─repository 数据访问类
├─util 工具类
├─SpringbootdemoApplication 项目启动类
├──resources 资源文件目录
│ ├─application.yml 全局配置文件
│ ├─banner.txt 项目启动banner
│ ├─tomcat.keystore SSL证书
│ ├─logback.xml 日志配置文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
主要特点:
- Restful风格
- 统一异常处理
- SQL预处理
技术选型:
- 核心框架:Spring Boot 2.1
- 持久层框架:JPA 2.0
- 日志管理:Logback
- 数据库:MySQL 5.7
- 插件:lombok
开发环境:
- SUN JDK1.8
- Maven 3.5.4
新建Spring Boot项目
这里使用的IDE是IntelliJ IDEA 2018
引包,配置pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!--引入MySQL驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
编写Entity
编写Person实体bean,用于ORM对象关系映射,映射数据库表结构
/**
* Person实体类
*/
@Entity
@Data
@Table(name = "person")
public class Person {
//主键自增长
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
创建一个接口PersonRepository,后续的控制器直接调用该接口继承自JpaRepository的方法,来实现和数据库交互
/**
* 继承JpaRepository,实现与数据库交互(JPA支持自动生成一些基本CURD SQL语句)
*/
public interface PersonRepository extends JpaRepository<Person,Integer> {
}
- 1
- 2
- 3
- 4
- 5
- 6
统一异常处理
自定义异常
/**
* 自定义异常类
*/
//RuntimeException继承Exception,spring只对继承RuntimeException的异常进行回滚
public class PersonException extends RuntimeException {
private Integer code;
<span class="token keyword">public</span> <span class="token function">PersonException</span><span class="token punctuation">(</span>ResultEnum resultEnum<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">super</span><span class="token punctuation">(</span>resultEnum<span class="token punctuation">.</span><span class="token function">getMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>code <span class="token operator">=</span> code<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> Integer <span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> code<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setCode</span><span class="token punctuation">(</span>Integer code<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>code <span class="token operator">=</span> code<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
捕获异常类
@ControllerAdvice
@Slf4j
public class ExceptionHandle {
<span class="token comment">/**
* 捕获异常类
* @param e
* @return
*/</span>
<span class="token annotation punctuation">@ExceptionHandler</span><span class="token punctuation">(</span>value <span class="token operator">=</span> Exception<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@ResponseBody</span>
<span class="token keyword">public</span> Result <span class="token function">handle</span><span class="token punctuation">(</span>Exception e<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">PersonException</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
PersonException personException <span class="token operator">=</span> <span class="token punctuation">(</span>PersonException<span class="token punctuation">)</span> e<span class="token punctuation">;</span>
<span class="token keyword">return</span> ResultUtil<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>personException<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>personException<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"【系统错误】"</span><span class="token punctuation">,</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> ResultUtil<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">"未知错误"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
封装异常消息枚举
/**
* 封装异常消息枚举类
*/
public enum ResultEnum {
UNKONW_ERROR(-1, "未知错误"),
SUCCESS(0, "成功");
<span class="token keyword">private</span> Integer code<span class="token punctuation">;</span>
<span class="token keyword">private</span> String msg<span class="token punctuation">;</span>
<span class="token function">ResultEnum</span><span class="token punctuation">(</span>Integer code<span class="token punctuation">,</span> String msg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>code <span class="token operator">=</span> code<span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>msg <span class="token operator">=</span> msg<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> Integer <span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> code<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> String <span class="token function">getMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> msg<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
封装异常对象实体
/**
* 封装异常对象(Http请求返回的最外层对象)
* @param <T>
*/
@Data
public class Result<T> {
//错误码
private Integer code;
//提示信息
private String msg;
private T date;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
异常工具类
/**
* 异常工具类
*/
public class ResultUtil {
public static Result sucess(Object obj) {
Result result = new Result();
result.setCode(0);
result.setMsg("sucess");
result.setDate(obj);
return result;
}
<span class="token keyword">public</span> <span class="token keyword">static</span> Result <span class="token function">sucess</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
Result result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
result<span class="token punctuation">.</span><span class="token function">setCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
result<span class="token punctuation">.</span><span class="token function">setMsg</span><span class="token punctuation">(</span><span class="token string">"sucess"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> Result <span class="token function">error</span><span class="token punctuation">(</span>Integer code<span class="token punctuation">,</span> String message<span class="token punctuation">)</span> <span class="token punctuation">{</span>
Result result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Result</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
result<span class="token punctuation">.</span><span class="token function">setCode</span><span class="token punctuation">(</span>code<span class="token punctuation">)</span><span class="token punctuation">;</span>
result<span class="token punctuation">.</span><span class="token function">setMsg</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
创建RESTful API
风格设计
请求类型 | 请求路径 | 功能 |
---|---|---|
Get | /person | 获取人员列表 |
Post | /person | 创建一个人员 |
创建Controller
/**
* 控制器,处理Http/https请求(RESTful API)
*/
@RestController
public class PersonController {
@Autowired
PersonRepository personRepository;
<span class="token comment">/**
* 查询所有人员列表(Get方式)
* @return
*/</span>
<span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/person"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> List<span class="token generics function"><span class="token punctuation"><</span>Person<span class="token punctuation">></span></span> <span class="token function">personlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> personRepository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 添加人员 (Post方式)
* @param person
* @return
*/</span>
<span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/person"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Result <span class="token function">personAdd</span><span class="token punctuation">(</span>HttpServletRequest request<span class="token punctuation">,</span><span class="token annotation punctuation">@RequestBody</span> Person person<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> ResultUtil<span class="token punctuation">.</span><span class="token function">sucess</span><span class="token punctuation">(</span>personRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
使用SSL-HTTPS
Spring Boot中使用HTTPS步骤:
- 要有一个SSL证书,证书怎么获取呢?买(通过证书授权机构购买)或者自己生成(通过keytool生成)
- 启用HTTPS
- 将HTTP重定向到HTTPS(可选)
获取SSL证书
有两种方式可以获取到SSL证书:
- 自己通过keytool生成;
- 通过证书授权机构购买;
这里作为演示,采用keytool生成,实际项目中大部分采用的都是购买的方式。
那么怎么使用keytool生成呢?
Keytool是Java提供的证书生成工具,如果配置了JAVA_HOME的,直接就可以在控制台进行生成了,这里演示使用的是Mac的终端窗口
192:~ apple$ keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
[Unknown]: zuozewei
您的组织单位名称是什么?
[Unknown]: 7DGroup
您的组织名称是什么?
[Unknown]: 7D
您所在的城市或区域名称是什么?
[Unknown]: Beijing
您所在的省/市/自治区名称是什么?
[Unknown]: Beijing
该单位的双字母国家/地区代码是什么?
[Unknown]: CN
CN=zuozewei, OU=7DGroup, O=7D, L=Beijing, ST=Beijing, C=CN是否正确?
[否]: y
输入 <tomcat> 的密钥口令
(如果和密钥库口令相同, 按回车):
再次输入新口令:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
查看生成的SSL证书信息
apple$ keytool -list -keystore tomcat.keystore
输入密钥库口令:
密钥库类型: jks
密钥库提供方: SUN
您的密钥库包含 1 个条目
tomcat, 2018-11-29, PrivateKeyEntry,
证书指纹 (SHA1): 2B:C5:FB:77:2C:5E:DC:5B:C5:E9:9F:06:27:7F:2E:A4:E4:9E:DF:8C
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
这里解释下命令的各个参数的含义:
-genkey :生成key;
-alias :key的别名;
-dname:指定证书拥有者信息
-storetype :密钥库的类型为JCEKS。常用的有JKS(默认),JCEKS(推荐),PKCS12,BKS,UBER。每个密钥库只可以是其中一种类型。
-keyalg :DSA或RSA算法(当使用-genkeypair参数),DES或DESede或AES算法(当使用-genseckey参数);
-keysize :密钥的长度为512至1024之间(64的倍数)
-keystore :证书库的名称
-validity : 指定创建的证书有效期多少天
dname的值详解:
CN(Common Name名字与姓氏)
OU(Organization Unit组织单位名称)
O(Organization组织名称)
L(Locality城市或区域名称)
ST(State州或省份名称)
C(Country国家名称)
这时候在当前目录下就会看到一个文件tomcat.keystore
,到这里SSL证书就有了。
启用HTTPS
默认情况下Spring Boot内嵌的Tomcat服务器会在8080端口启动HTTP服务,Spring Boot允许在全局配置文件中配置HTTP或HTTPS,但是不可同时配置,如果两个都启动,至少有一个要以编程的方式配置,Spring Boot官方文档建议在application配置文件中配置HTTPS,因为HTTPS比HTTP更复杂一些
在application.yml
中配置HTTPS,配置信息如下:
server:
port: 443
servlet:
context-path: /springboot
ssl:
key-store: classpath:tomcat.keystore
key-store-type: jks
key-alias: tomcat
key-store-password: zuozewei
key-store-provider: SUN
key-password: zuozewei
enabled: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
注意:请将在上一步生成的证书放到src/main/resources
目录下。
将HTTP请求重定向到HTTPS
由于不能同时在application.l中同时配置两个connector,所以要以编程的方式配置HTTP Connector,然后重定向到HTTPS Connector
编写TomcatHttp配置类
@Configuration
public class TomcatHttpConfig {
<span class="token comment">/**
* 配置内置的Servlet容器工厂为Tomcat
* @return
*/</span>
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> ServletWebServerFactory <span class="token function">servletContainer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
TomcatServletWebServerFactory tomcat <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TomcatServletWebServerFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">postProcessContext</span><span class="token punctuation">(</span>Context context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
SecurityConstraint securityConstraint <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SecurityConstraint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
securityConstraint<span class="token punctuation">.</span><span class="token function">setUserConstraint</span><span class="token punctuation">(</span><span class="token string">"CONFIDENTIAL"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
SecurityCollection collection <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SecurityCollection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
collection<span class="token punctuation">.</span><span class="token function">addPattern</span><span class="token punctuation">(</span><span class="token string">"/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
securityConstraint<span class="token punctuation">.</span><span class="token function">addCollection</span><span class="token punctuation">(</span>collection<span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">addConstraint</span><span class="token punctuation">(</span>securityConstraint<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//添加配置信息,主要是Http的配置信息</span>
tomcat<span class="token punctuation">.</span><span class="token function">addAdditionalTomcatConnectors</span><span class="token punctuation">(</span><span class="token function">redirectConnector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> tomcat<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 配置一个Http连接信息
* @return
*/</span>
<span class="token keyword">private</span> Connector <span class="token function">redirectConnector</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
Connector connector <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Connector</span><span class="token punctuation">(</span><span class="token string">"org.apache.coyote.http11.Http11NioProtocol"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
connector<span class="token punctuation">.</span><span class="token function">setScheme</span><span class="token punctuation">(</span><span class="token string">"http"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
connector<span class="token punctuation">.</span><span class="token function">setPort</span><span class="token punctuation">(</span><span class="token number">8088</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
connector<span class="token punctuation">.</span><span class="token function">setSecure</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
connector<span class="token punctuation">.</span><span class="token function">setRedirectPort</span><span class="token punctuation">(</span><span class="token number">443</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> connector<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
自定义启动标志
只需要在src/main/resources
路径下新建一个banner.txt文件,banner.txt中填写好需要打印的字符串内容即可。
一般情况下,我们会借助第三方工具帮忙转化内容,如:
网站:http://www.network-science.de/ascii/ 将文字转化成字符串,
网站:http://www.degraeve.com/img2txt.php 可以将图片转化成字符串。
配置日志配置文件
只需要在src/main/resources
路径下新建一个logback.xml
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/>
<property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="CRAWLER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/event.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/event.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%msg%n</pattern>
</encoder>
</appender>
<logger name="com.business.intelligence.util.CrawlerLogger" level="INFO" additivity="false">
<appender-ref ref="CRAWLER_LOG"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
配置数据库配置
手动先创建db_person
数据库
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.jdbc.Driver
#手动创建db_person数据库
url: jdbc:mysql://39.105.21.2:3306/db_person?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: zuozewei
password: zuozewei
jpa:
hibernate:
ddl-auto: update
show-sql: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
启动并测试
启动项目
通过浏览器输入:http://127.0.0.1:8088/springboot/person
我们可以看到浏览器自动重定向到 https://127.0.0.1/springboot/person
点击浏览器上方的证书,我们可以看到使用的SSL证书信息
完整的项目结构
秒懂HTTPS接口系列源码:
https://github.com/zuozewei/Java-API-Test-Examples
相关系列:
秒懂HTTPS接口(原理篇)
秒懂HTTPS接口(接口测试篇)
秒懂HTTPS接口(JMeter压测篇)
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet">
</div>
</article>