spring boot支持https请求

spring boot给我们提供了很多便利之处,包括spring boot内置了tomcat,所以我们一般启动都是spring boot内置的tomcat,用HTTP请求进行访问,但是http请求并不安全,由于我们对项目应用的权限认证变得更加谨慎,需要用到https请求,自己新建了一个spring boot 项目进行测试,现在将怎么新建spring boot工程和使用https请求以及在此过程中遇到的问题记录一下。

一.新建spring boot 的web工程,我用的是intelij idea

1.创建一个项目,创建时选择Spring Initializr,然后Next

image.png

 

2.填写项目信息

image.png

3.选择web工程

image.png

注:我的intelij版本可能比较低,出现的是这样的,但是有的可能会出现下图的,都是一样的

image.png

4.填写工程名字点击finish

image.png

ok,一个新的web工程就建好了,下载依赖的过程比较慢

我们新建的是一个web工程,可以在项目的入口类加上@RestController注解,使之变为一个Controller,然后里边提供一个地址转换方法

 

@RestController
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    String index(){
        return "Hello Spring Boot!";
    }
}

启动main方法,访问http://localhost:8080/(查看启动日志时可以发现默认的端口号是8080)可以看到页面显示

image.png

二.生成ssl证书

我是自己生成的ssl证书,当然这个证书是不被客户端认可的
1.打开cmd,输入生成证书的命令

 

keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

关于这几个参数的解释如下:

1.-storetype 指定密钥仓库类型
2.-keyalg 生证书的算法名称,RSA是一种非对称加密算法
3.-keysize 证书大小
4.-keystore 生成的证书文件的存储路径
5.-validity 证书的有效期

2.输入密钥库口令

image.png

3.依次填写证书相关的信息

image.png

完成之后,系统的当前用户目录下会生成一个keystore.p12文件,当然你在生成证书的时候可以改变证书的名称,那么相应的系统用户目录下就会生成相应的文件,将keystore.p12文件拷贝到我们项目的根路径下,项目的结构如下:

image.png

4.修改application.properties文件,添加HTTPS支持

 

server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias:tomcat

注:这里的key-store-password就是我们生成ssl证书时设置的密钥库的口令

此时,我们的工程就支持了https请求,我们就可以启动项目的main方法,查看启动时的日志会发现默认的端口依旧是8080,因为我们并没有额外设置https的端口号,访问https://localhost:8080/

image.png

注:因为我用的是自己生成的ssl证书,所以https请求是不被客户端识别的,我用的是火狐浏览器,在警告页点击 高级按钮,点击添加例外,点击确认安全例外就可以正常看到页面显示的内容,所以当你用浏览器进行https请求时未正常显示并不是我们的项目有问题,修改浏览器的安全认证配置就行了

三.http请求转换成https请求

我们已经习惯用http请求访问,此时我们的项目支持的是https请求,为了方便我们可以将访问时的http请求转换成https请求
在main方法中添加注解bean,代码如下:

 

@RestController
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    String index(){
        return "Hello Spring Boot!";
    }
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(8081);
        return connector;
    }

}

注:我们知道spring boot内置的tomcat启动时的默认的端口号时8080,我们现在需要将http请求转换成https请求,所以一定要区分端口号,我们在代码中将http的端口号设置为8080,在application.properties添加https的端口号

 

server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias:tomcat
server.port=8081

启动项目,我们访问https://localhost:8081/,(同时还是需要设置浏览器的安全认证的)页面可以正常显示说明项目是支持https请求的

访问http://localhost:8080/,我们可以看到页面的url变成了https://localhost:8081/,页面也可以正常显示,说明我们的需求解决了,可以将http请求转换成https请求。

四.同时支持http和https请求

如果我们想要项目同时支持http和https请求,就是说我们用http请求时不再转换成https请求,同时还支持https请求,很简单,我们只需要将代码中的connector.setSecure(false)中的false改成true就可以了,代码如下:

 

@RestController
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    String index(){
        return "Hello Spring Boot!";
    }
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(8080);
        connector.setSecure(true);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(8081);
        return connector;
    }

}

启动项目,我们访问https://localhost:8081/,页面可以正常显示说明项目是支持https请求的

访问http://localhost:8080/,我们可以看到页面的url并没有转换成https请求,页面也可以正常显示,说明我们的需求解决了,可以同时支持http和https请求。

如有问题,欢迎指正!!!!



作者:嗨_等风来
链接:https://www.jianshu.com/p/71cd01fa8438
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值