HTTPS 인증서 발급 & 서버 구현하기

hzn·2022년 11월 12일
0

HTTP / 네트워크

목록 보기
4/9
post-thumbnail

HTTPS 사설 인증서 발급받기

  • mkcert라는 프로그램을 이용해서 로컬 환경(내 컴퓨터)에서 신뢰할 수 있는 인증서 만들 수 있다.

설치하기

brew install mkcert

인증서 생성

  • 로컬(내 컴퓨터)을 인증된 발급 기관으로 추가
mkcert -install
  • (localhost로 대표되는) 로컬 환경에 대한 인증서를 만들기
mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1

👉🏽 옵션으로 추가한 localhost, 127.0.0.1(IPv4), ::1(IPv6)에서 사용할 수 있는 인증서가 완성됨.
👉🏽 cert.pem, key.pem 이라는 파일이 생성됨

HTTPS 서버 구현하기

  • 생성한 인증서 파일들을 HTTPS 서버에 적용

Node.js https 모듈 이용

const https = require('https');
const fs = require('fs');

https
  .createServer(
    {
      key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
      cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
    },
    function (req, res) {
      res.write('Congrats! You made https server now :)');
      res.end();
    }
  )
  .listen(3001);
  • 서버 실행 후 https://localhost:3001로 접속하면 HTTPS 프로토콜을 이용한다는 것을 확인할 수 있다. (브라우저의 url 창 왼쪽에 자물쇠가 잠겨있다!)

express.js 이용

const https = require('https');
const fs = require('fs');
const express = require('express');

const app = express();

https
  .createServer(
    {
      key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
      cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
    },
    app.use('/', (req, res) => {
      res.send('Congrats! You made https server now :)');
    })
  )
  .listen(3001);

0개의 댓글