스프링 부트 HTTPS 설정

mcyoo.log·2022년 6월 21일
0

spring-boot

목록 보기
1/3
post-thumbnail

요약
1. "스프링 부트와 AWS로 혼자 구현하는 웹 서비스" 책에 없는 HTTPS 설정
2. 소셜 로그인 이슈가 남아 있음구성

구성도
podoclub.net 접속 요청nginx : 80 번 포트nginx : 443 포트 및 https 연결자바스프링 내장 톰캣 : 8080 포트 접속


인증서 생성

certbot 설치

yum install certbot
certbot certonly --standalone 으로 도메인 입력 후 인증서 생성

인증서 확인

/etc/letsencrypt/live/podoclub.net/fullchain.pem;
/etc/letsencrypt/live/podoclub.net/privkey.pem;

nginx 설치

yum install nginx

nginx 설정

  1. 기존 AWS ec2 에 실행되고 있는 웹 서버 stop 시키기
    sudo service httpd stop

  2. nginx 설치 및 /etc/nginx/nginx.conf 설정 값 수정
    nginx.conf

upstream origin {
        # 로드밸런싱 알고리즘 선택(default는 라운드 로빈)
        server 127.0.0.1:8080;
}

server {

	  listen 		80;
	  server_name 	podoclub.net;
	  return 301 	https://$host$request_uri;

}
server {

		listen       	443 ssl;
        server_name 	podoclub.net;

        ssl_certificate /etc/letsencrypt/live/podoclub.net/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/podoclub.net/privkey.pem;
  
		location / {
		    proxy_pass http://origin;
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header Host $http_host;
	    }
}
  1. nginx 서비스 시작
    sudo service nginx start

스프링 부트 내장 톰캣 실행 (책 내용 그대로)
1. spring boot 빌드 및 실행 ./deploy.sh

nohup java -jar \
    -Dspring.config.location=classpath:/application.properties,classpath:/application-real.properties,/home/ec2-user/app/application-oauth.properties,/home/ec2-user/app/a$
    -Dspring.profiles.active=real \
    $JAR_NAME > $REPOSITORY/nohup.out 2>&1 &

https 접속 확인

소셜로그인 이슈

구글 로그인, 네이버 로그인 등 소셜 로그인에서 문제가 된다.
문제는 redirect url

application.properties 설정을 보면,

구글은 따로 설정 안해도 되고,(default 로 설정 되있음)
네이버는
spring.security.oauth2.client.registration.naver.redirect-uri={baseUrl}/{action}/oauth2/code/{registrationId}
이런 설정이 되있을 것이다.

{baseUrl}/{action}/oauth2/code/{registrationId} 이 부분이 http:// 로 시작한다는 것이 문제가 된다.

네이버 callback URL 에 https:// 로 설정 하면 http:// ↔ https:// 가 mismatch 되어 에러가 뜬다. 로그인이 안된다.

구글도 마찬가지 이다. https 랑 http 랑 mismatch 된다.

위에 redirect_uri 값을 보면 http:// 인 것을 확인할 수 있다.
(물론 네이버, 구글에 callback url 설정을 http:// 로 설정하면 아무 문제 없다.)

application.properties 설정

구글

spring.security.oauth2.client.registration.google.redirect-uri=https://podoclub.net/{action}/oauth2/code/{registrationId}

네이버

spring.security.oauth2.client.registration.naver.redirect-uri=https://podoclub.net/{action}/oauth2/code/{registrationId}

로 redirect url 을 수동 설정 하면 에러는 더 이상 나지 않는다.

또 문제.

https://podoclub.net/{action}/oauth2/code/{registrationId} 이 주소에 담긴 Data 가 같이 안 넘어 온다. nginx 설정 문제로 추정되어

upstream origin {
        # 로드밸런싱 알고리즘 선택(default는 라운드 로빈)
        server 127.0.0.1:8080;
}

server {

	  listen 		80;
	  server_name 	podoclub.net;
	  return 301 	https://$host$request_uri;

}
server {

		listen       	443 ssl;
        server_name 	podoclub.net;

        ssl_certificate /etc/letsencrypt/live/podoclub.net/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/podoclub.net/privkey.pem;
  
		location / {
		    proxy_pass http://origin;
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header Host $http_host;
	    }
}

이 부분에서 어떻게 할지 모르겠다…

해결방법은 다음과 같겠다

1. nginx 로 https 설정 하지 말기, 내장 톰캣에다가 설정하기
2. nginx 설정 더 조지기

#시도 해본 것
location /login {

        proxy_pass 	        http://origin;
        proxy_http_version  1.1;
        proxy_cache_bypass  $http_upgrade;

        proxy_set_header Upgrade           $http_upgrade;
        proxy_set_header Connection        "upgrade";
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-Port  $server_port;
	    
}

정리
1. 스프링 부트 내장 톰캣에 nginx 를 활용하여 https 적용을 해보았다.
2. 소셜 로그인 적용 시, http 로 설정하면 아무 문제 없지만, https 로 redirect uri 를 설정할시, body 에 담겨서 오는 data를 어떻게 처리할건지 에 대한 이슈가 있다.

2022-06-21 로그인 이슈 찾는 중...

참고 사이트
https://kerobero.tistory.com/40

0개의 댓글