溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS

發(fā)布時(shí)間:2021-04-20 16:46:26 來源:億速云 閱讀:157 作者:Leah 欄目:編程語言

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

springboot是什么

springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。

在配置TLS/SSL之前我們需要拿到相應(yīng)簽名的證書,測試實(shí)例可以使用Java 下面的 Keytool 來生成證書:

打開控制臺輸入:

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

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS

這里的別名是 keystore.p12,密碼什么的直接設(shè)置就好,然后回車

然后根據(jù)路徑找到生成好的證書,把證書復(fù)制到項(xiàng)目里,我是放到了這里

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS

放好證書后,建立一個(gè)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監(jiān)聽的http的端口號
    connector.setPort(8080);
    connector.setSecure(false);
    //監(jiān)聽到http的端口號后轉(zhuǎn)向到的https的端口號
    connector.setRedirectPort(8888);
    return connector;
  }
 
}
@Controller
@RequestMapping
public class ViewControlller {
 
  @GetMapping("index")
  public String index(){
    return "index";
  }
}

值得注意的是加入的springboot jar的版本不同代碼有一定的改變,我這里用的是2.0的版本,還有就是要想跳轉(zhuǎn)到html頁面的時(shí)候一定注意的就是千萬不要在Controller中用@RestController而是要用Controller,如果用RestController的話就會直接把你的index解析顯示在頁面當(dāng)中,就不會跳轉(zhuǎn)了,還有就是想要跳轉(zhuǎn)的話一定要加入下面的兩個(gè)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>

準(zhǔn)備完畢后啟動項(xiàng)目,打印臺顯示

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS

再輸入:

127.0.0.1:8080/demo/index就會自動跳轉(zhuǎn)

怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS

看完上述內(nèi)容,你們掌握怎么在springboot中通過配置ssl實(shí)現(xiàn)HTTPS的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI