Https 在spring-boot应用例子

[b]Https 在spring-boot应用例子[/b]


[b]Https工原理(以浏览器-服务器为例子)[/b]
1.浏览器发送Https请求
2.服务器接收到Https请求,返回CA数字证书(里面包含有公钥、服务器基本信息、CA签名、认证机构基本信息(颁发证书机构)等)
3.浏览器接收到数字证书,得到数字证书颁发证书机构信息,浏览器检测这个颁发证书机构的证书是否可以信任(CA一般在浏览器中都信任),就是是否在信任认证机构列表中,不在列表中就弹出不信任提示框。
4.如果信任就用颁发证书机构的公钥解密证书中的签名,并用数字证书中的签名算法重新计算数字证书的信息的签名,看这个签名是否一致,一致表明信息没有改变过,这个证书确实是可以信任的。
5.浏览器再对比现在访问的域名(URL前部)是否要数字证书中的域名对应上,如果不对上也会弹出不信任提示框。
6.浏览器信任后会随机生成一个临时密码串(每次建立随机生成),用数字证书中的公钥加密这个临时密码串,发送到服务器。
7.服务器接收到加密的临时密码串后,用服务器的私钥解密,得到临时密码串。响应浏览器。
8.浏览器接收到响应后,https连接建立,浏览器发送请求到服务器开始。
9.https连接建立后,浏览器发送的所有信息(TCP包中的数据)都是以临时密码串进行了加密的。服务器也是用这个临时密码串进行解密还原请求信息。


[b]数字证书[/b]
1.数字证书是一个包含有公钥、服务器基本信息、认证机构基本信息(颁发证书机构),前面所有内容的信息摘要用CA私钥加密后的签名等组成的一个文件或一串字符串信息。
数字证书到底包含
(1).持有者姓名(Common Name)
(2).发证机关(Issuer)
(3).有效日期(Validity)
(4).证书持有人的公钥(Subject’s Public Key Info)
(5).扩展信息 (Extension)
(6).用发证机关对该证书的数字签名(Certificate Signature)


[b]数字签名[/b]
1.就是对一串字符串进行摘要算法,这个过程是不可逆的(信息部分选取的),用于验证数据是否发生过更改用的。
2.在对所有参数后加一个自己的参数(相当于密码了),再进行计算那别人就不可能算出你一样的摘要信息了(自己的参数(相当于密码了)在别人不知的情况下)

//数字签名算法例子
package com.proserver.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashAlgorithms {
public static String Encrypt(String strSrc, String encName) {
MessageDigest md = null;
String strDes = null;

byte[] bt = strSrc.getBytes();
try {
if (encName == null || encName.equals("")) {
encName = "SHA-256";
}
md = MessageDigest.getInstance(encName);
md.update(bt);
strDes = bytes2Hex(md.digest()); // to HexString
} catch (NoSuchAlgorithmException e) {
return null;
}
return strDes;
}

public static String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}
}



[b]SSL[/b]全称是 Secure Sockets Layer,它是一种间于传输层(比如TCP/IP)和应用层(比如HTTP)的协议。
1.对TCP数据包里的数据信息进行加密的。


[b]数字证书生成过程[/b]
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
1.生成的keystore.p12里面的包含了数字证书、私钥的,程序从这个文件里面取数字证书和私钥。
2.p12是存储这些信息的一类文件类型。

在工程目录终端输入
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
按提示输入相关的信息,到最后就会生成一个keystore.p12的文件了。
[img]http://dl2.iteye.com/upload/attachment/0122/5722/e336d936-536f-3ab2-98f2-d772de9b9669.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0122/5724/2e2f0d7f-7516-3787-8a65-950f2b00f47b.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0122/5726/626d71f9-a65c-3e2b-904a-72c6a72b980c.png[/img]


[b]例子代码:[/b]
application.properties

#Server
#server.port=8090
#LOGGING
logging.pattern.level=INFO
server.port:8443
server.ssl.key-store: classpath:keystore.p12
server.ssl.key-store-password:123456
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat



package com.cesmart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan(basePackages = "com.cesmart") // 扫描那些包得到bean.@ComponentScan({"com.teradata.notification","com.teradata.dal"})
//@EnableSwagger2 //启动swagger注解
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
}
}



package com.cesmart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicates;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration//定义为spring boot 的配置文件
@EnableSwagger2//启动swagger注解
public class Swagger2 {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.cesmart.controller";

@Bean(value="createRestApi")
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test1")
.pathMapping("/")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(Predicates.or(PathSelectors.regex("/webTest2/.*")))
.build();

//groupName,分组名字
//pathMapping,映射路径(会加到URL前面组成新的路径,如:"/xing/WebTest/webTest",(pathMapping("/xing")))
//apiInfo,API信息描述
//select, 选择那些路径和api会生成document
//apis,扫描那些包,RequestHandlerSelectors.any()表示对所有api进行监控
//paths,匹配那些路径,PathSelectors.any()表示所有路径,
}

@Bean(value="createRestApi2")
public Docket createRestApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test2")
.pathMapping("/")
.apiInfo(apiInfo2())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(Predicates.or(PathSelectors.regex("/webTest/.*")))
.build();
}


private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://blog.didispace2.com/")
.contact("程序猿DD")
.version("1.0")
.license("license")
.licenseUrl("licenseUrl")
.build();

//title,标题,在页面顶部显示
//description,描述,在页面顶部显示
//termsOfServiceUrl,
//contact,显示“Created by + contact”,在页面顶部显示
//version,API版本,,在页面顶部显示
//license,版权
}

private ApiInfo apiInfo2() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://blog.didispace2.com/")
.contact("程序猿DD")
.version("1.0")
.license("license")
.licenseUrl("licenseUrl")
.build();
}

}


@RestController
@Api(value = "WebTest", description = "有关于Swagger2操作")
@RequestMapping(value = "/webTest")
// 用在类上,说明该类的作用
// value,显示在类中的说明
// description,类中的说明
// 显示形式:“value:description”,如上面显示为"WebTest:有关于Swagger2操作"
public class WebTest {
@ApiOperation(value = "接口说明", notes = "接口发布说明", response = String.class)
// 用在方法上,说明方法的作用
// 显示在方法说明中,显示notes
// response,接口返回参数类型
// value = "接口说明",
// notes = "接口发布说明"
@ApiImplicitParams({
@ApiImplicitParam(paramType = "path", required = true, name = "test", dataType = "String", value = "456"),
@ApiImplicitParam(paramType = "path", required = true, name = "test2", dataType = "String", value = "789") })
// @ApiImplicitParam,表示一个参数的描述,与请求参数有关系
// paramType,参数放在哪个地方
// required,参数是否必须传
// name,参数名
// dataType,参数类型(描述)
// value,参数的意思(描述)
@ApiParam
@RequestMapping(value = "/webTest/{test}/{test2}", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
public String webTest(@PathVariable("test") String test, @PathVariable("test2") String test2) {
System.out.println("webTest");
System.out.println("test == " + test);
System.out.println("test2 == " + test2);
return "webTest";
}

@ApiOperation(value = "接口说明", notes = "接口发布说明", response = String.class)
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", required = true, name = "test", dataType = "String", value = "456"),
@ApiImplicitParam(paramType = "query", required = true, name = "test2", dataType = "String", value = "789") })
@RequestMapping(value = "/webTest2", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST)
public String webTest2(String test, String test2) {
System.out.println("webTest");
System.out.println("test == " + test);
System.out.println("test2 == " + test2);
return "webTest";
}

@ApiOperation(value = "接口说明", notes = "接口发布说明", response = String.class)
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", required = true, name = "name", dataType = "String", value = "456"),
@ApiImplicitParam(paramType = "query", required = true, name = "value", dataType = "String", value = "789") })
@RequestMapping(value = "/webTest3", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST)
public String webTest3(@ModelAttribute TestModel testModel) { // 这里要用@ModelAttribute,才不会出现testModel输入框
System.out.println("testModel == " + testModel.toString());
return "webTest";
}
}



[b]抓包软件:[/b]fiddler


参考(Spring Boot中启动HTTPS):[url]http://blog.csdn.net/ro_wsy/article/details/51319963[/url]
参考(SpringBoot启用HTTPS协议):[url]http://blog.csdn.net/enterys/article/details/52034250[/url]
参考(在Spring Boot中使用Https(应用参考这个)):[url]http://www.cnblogs.com/xinzhao/p/4952856.html[/url]
参考(基于SpringBoot项目的https):[url]http://www.cnblogs.com/badboyf/p/6145642.html[/url]
参考(5分钟内搞定 Tomcat 的 SSL 配置):[url]https://www.oschina.net/question/12_23148[/url]
参考(Spring Boot基于Tomcat的HTTP和HTTPS协议配置,这个好):[url]http://kchu.me/2015/08/19/Spring-Boot%E5%9F%BA%E4%BA%8ETomcat%E7%9A%84HTTP%E5%92%8CHTTPS%E5%8D%8F%E8%AE%AE%E9%85%8D%E7%BD%AE/[/url]

参考(ca证书的工作原理):[url]http://wenda.tianya.cn/question/1d6e2990867fb734[/url]
参考(数字证书及CA的扫盲介绍):[url]http://kb.cnblogs.com/page/194742/[/url]
参考(SSL 与 数字证书 的基本概念和工作原理):[url]http://blog.csdn.net/jhonguy/article/details/7577729[/url]
参考(数字证书及其认证过程):[url]http://blog.csdn.net/cyy089074316/article/details/9071951[/url]
参考(https原理:证书传递、验证和数据加密、解密过程解析):[url]http://blog.csdn.net/clh604/article/details/22179907[/url]
参考(浏览器和服务器在基于https进行请求链接到数据传输过程中,用到了如下哪些技术):[url]http://www.cnblogs.com/cloudml/p/4798876.html[/url]
参考(数字证书原理):[url]http://www.2cto.com/article/201203/122095.html[/url]
参考(生成PKI公私密钥对及数字证书):[url]http://openwares.net/misc/pki_key_pair_certificate.html[/url]
参考(keytool生成私钥、公钥、证书详解):[url]http://blog.csdn.net/kexiuyi/article/details/52413133[/url]
参考(数字证书文件格式(cer和pfx)的区别):[url]http://blog.csdn.net/xueyepiaoling/article/details/6524248[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie310600

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值