SpringBoot-2.1.1系列一:使用https

1.什么是https?

HTTPS中文名称:超文本传输安全协议,是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。

https=http+ssl

2.为什么需要使用https?

最初大家都使用http协议,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂其中的信息,因此,HTTP协议不适合传输一些敏感信息,比如:信用卡号、密码等支付信息。

为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS,为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。

3.怎样使用https?

3.1.生成证书

keytool -genkey -alias tomcat -keyalg RSA -keystore /home/gzr/tomcat.keystore

953639-20181212224506736-484175936.png

953639-20181212224532633-1879570116.png

将生成好的证书放在项目根目录即可

3.2.配置文件增加配置属性

server:
  #端口号
  port: 443
  ssl:
    #生成证书的名字
    key-store: https.keystore
    #密钥库密码
    key-store-password: 123456
    key-store-type: JKS
    key-alias: https

此时访问https://localhost即可(https默认端口号是443,所以无需加上端口号)

953639-20181212224547819-531261493.png

3.3.让http访问自动跳转到https

上面只有https协议,如果用http访问会报错

953639-20181212224731793-1466740554.png

可是用户日常使用中,并不愿意加上https

以百度为例,当我们输入www.baidu.com时,它会自动加上https。这样用户体验较好。

953639-20181212224627430-422977047.png

953639-20181212224636937-740367683.png

下面增加http访问自动跳转到https:

首先增加一个自定义属性,用于http端口号

http:
  port: 80

编写配置类HttpsConfig

package com.chenyuwen.demo;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@Configuration
public class HttpsConfig {
    @Value("${server.port}")
    private int httpsPort;
    @Value("${http.port}")
    private int httpPort;

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
    }

    private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(httpPort);
        connector.setSecure(false);
        connector.setRedirectPort(httpsPort);
        return connector;
    }
}

此时再访问http://localhost会自动跳转到https://localhost

本次讲解到此结束!

转载于:https://www.cnblogs.com/yvanchen1992/p/10111534.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值