怎么在springboot中通过配置ssl实现HTTPS

发布时间:2021-04-20 16:46:26 作者:Leah
来源:亿速云 阅读:135

怎么在springboot中通过配置ssl实现HTTPS?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

在配置TLS/SSL之前我们需要拿到相应签名的证书,测试实例可以使用Java 下面的 Keytool 来生成证书:

打开控制台输入:

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

怎么在springboot中通过配置ssl实现HTTPS

这里的别名是 keystore.p12,密码什么的直接设置就好,然后回车

然后根据路径找到生成好的证书,把证书复制到项目里,我是放到了这里

怎么在springboot中通过配置ssl实现HTTPS

放好证书后,建立一个index.html放到resources/templates文件夹下,一会用于测试。

再配置properties

server.port=8888
server.tomcat.uri-encoding=utf-8
server.servlet.context-path=/demo 
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=tomcat 
spring.thymeleaf.prefix=classpath:/templates/

配置好properties再加入下面的代码

@Configuration
public class HttpsConfig {
 
  /**
   * spring boot 1.0
   */
  /* @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;
  }*/
 
  /**
   * spring boot 2.0
   * @return
   */
  @Bean
  public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
      @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(8888);
    return connector;
  }
 
}
@Controller
@RequestMapping
public class ViewControlller {
 
  @GetMapping("index")
  public String index(){
    return "index";
  }
}

值得注意的是加入的springboot jar的版本不同代码有一定的改变,我这里用的是2.0的版本,还有就是要想跳转到html页面的时候一定注意的就是千万不要在Controller中用@RestController而是要用Controller,如果用RestController的话就会直接把你的index解析显示在页面当中,就不会跳转了,还有就是想要跳转的话一定要加入下面的两个jar包

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.0.1.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

准备完毕后启动项目,打印台显示

怎么在springboot中通过配置ssl实现HTTPS

再输入:

127.0.0.1:8080/demo/index就会自动跳转

怎么在springboot中通过配置ssl实现HTTPS

看完上述内容,你们掌握怎么在springboot中通过配置ssl实现HTTPS的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Apache配置SSL 实现https访问
  2. 怎么在nginx中通过配置SSL证书实现https服务

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot https ssl

上一篇:怎么在springboot中使用redis分布式锁模拟抢单

下一篇:怎么在java中使用Base64加密解密文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》