1. 程式人生 > 實用技巧 >API安全(七)-https

API安全(七)-https

1、為什麼要使用https

  以使用者註冊為例,通過資料校驗,可以保證使用者傳給我們的密碼是完整有效的。資料進來之後,通過我們的處理,存放到資料庫中的密碼是經過加密的,也是安全的。但是還有一點,就是使用者的請求在到達我們應用之前的一個安全,怎麼來保證?使用者註冊的請求,在到達伺服器之前,就被別人給截獲了。使用者名稱和密碼被別人截獲了,實際上我們應用程式後面做什麼都沒用了,因為別人已經知道了使用者名稱和密碼了。要保證者之間的安全要使用https。

2、什麼是https

  HTTPS (全稱:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全為目標的 HTTP 通道,在HTTP的基礎上通過傳輸加密和身份認證保證了傳輸過程的安全性。HTTPS 在HTTP 的基礎下加入SSL層,HTTPS 的安全基礎是 SSL,因此加密的詳細內容就需要 SSL。 HTTPS 存在不同於HTTP 的預設埠及一個加密/身份驗證層(在 HTTP與 TCP 之間)。這個系統提供了身份驗證與加密通訊方法。它被廣泛用於全球資訊網上安全敏感的通訊,例如交易支付等方面 。

3、https主要乾了什麼

  3.1、客戶端和服務端在傳輸資料之前,會對雙方進行身份認證 ,認證成功建立連線。

  3.2、資料傳輸的機密性,一旦安全連線建立以後,在傳輸的過程中,會對資料進行加密,那麼就算在中間拿到了https傳輸的資料,也都是經過加密的。

4、SpringBoot使用https

  4.1、使用java的keytool生成自簽證書

  使用以下命令生成自簽證書

  4.2、將證書放到resources目錄下

  4.3、修改application.yml配置檔案

server:
  port: 8443
  ssl:
    key-store: classpath:cfq.key
    key-store-password: 123456
    key-password: 123456

  4.4、啟動專案,看控制檯列印,已經是https啟動了

  訪問https://127.0.0.1:8443/users/40,因為是我們自籤的證書,所以要自己點選高階,點選接收風險並繼續,會彈出認證框,輸入使用者名稱密碼,可以正常通過https訪問

5、Springboot同時支援https和http

  SpringBoot設定了https,就不用http訪問了,會直接拒絕掉,如果想支援http和https的話,要進行一下配置

  5.1、配置檔案中,自定義http埠

http:
  port: 9090

  5.2、啟動類配置如下

@SpringBootApplication
public class UserApiApplication {

    @Value("${http.port}")
    private Integer port;

    public static void main(String[] args) {
        SpringApplication.run(UserApiApplication.class, args);
    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    private Connector createStandardConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(port);
        return connector;
    }
}

  5.3、啟動專案,看控制檯列印,已經是https與http啟動了

  5.4、測試通過https://127.0.0.1:8443/users/40 與http://127.0.0.1:9090/users/40都可進行訪問

6、Springboot同時支援http強制跳轉https

  修改啟動類配置如下

@SpringBootApplication
public class UserApiApplication {

    @Value("${http.port}")
    private Integer port;

    @Resource
    private ServerProperties serverProperties;

    public static void main(String[] args) {
        SpringApplication.run(UserApiApplication.class, args);
    }


    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                // 強制使用https
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        //新增http
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    /**
     *  配置http
     */
    private Connector createStandardConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(port);
        //http重定向到https時的https埠號
        connector.setRedirectPort(serverProperties.getPort());
        return connector;
    }


}

  這時訪問http://127.0.0.1:9090/users/40會強制跳轉為https://127.0.0.1:8443/users/40