About Project/Issues

401 Unauthorized : 포트 중복 이슈 해결방법 (mac, windows, netstat, taskkill)

마이트너 2023. 6. 13. 10:37

 


✅ 이슈 발생

신나게 Postman으로 서버 동작을 미리 확인해보러 갔다가

401 Error가 발생했다.


✅ 이슈 확인

전에 공부했던 에러 상태 코드의 기억을 더듬어

이게 무슨 문제인지부터 확인했다.

1XX : 요청 수신 후 현재 처리가 진행 중인 상태로, 주로 클라이언트와 서버의 연결 상태를 확인하는 용도
2XX : 요청 성공적으로 처리되었음을 나타내는 상태
3XX : 클라이언트의 추가적인 조치가 필요한 상태로, redirection 등에서 볼 수 있다
4XX : 클라이언트 요청 에러
5XX : 서버에 관한 오류

그 중에서도 401 Error는 요청된 리소스에 대한 유효한 인증 자격 증명이 없기 때문에 클라이언트 요청이 완료되지 않았는 경우라고 한다. 음.. 그렇다면! 당황하지 않고~

 

이게 왜 이럴까 살펴보다가 아래의 오류를 확인했다.

....!!! 실행만 시켜놓고 확인하지 않았다니.. !!! 

이러니까 안 되지..!

C:\Users\feshe\.jdks\azul-17.0.7\bin\java.exe -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1\lib\idea_rt.jar=50866:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -classpath D:\spring\spring-prepare\out\production\classes;D:\spring\spring-prepare\out\production\resources;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.projectlombok\lombok\1.18.26\8f8cf0372abf564913e9796623aac4c8ea44025a\lombok-1.18.26.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\3.1.0\36a8666047ea49114e0974bece35e2ef68cf975f\spring-boot-starter-web-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-json\3.1.0\546bd372bc070339c4255ed37f4f0ff5aafa2648\spring-boot-starter-json-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\3.1.0\2960a1f899f4ee3eb815dc85986b0428c1a5289f\spring-boot-starter-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\3.1.0\e7ea53de5a58aaf63f0e3a80d438f7dfc0b9806b\spring-boot-starter-tomcat-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\6.0.9\e127c07a23403832d0c6292f4a0bf8c7a2b7329f\spring-webmvc-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\6.0.9\2837dec8a75ecfdad367d6c30ce9cbdfc89caa7a\spring-web-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310\2.15.0\3c3a0cfba09271fab4603224f2c2e21c6ddf6dc4\jackson-datatype-jsr310-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-parameter-names\2.15.0\b900bd44597ba9a8b348b49428f457d5f16fc302\jackson-module-parameter-names-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8\2.15.0\ddfd375d4df3a4c149bf7d5d71c45b91909cf31f\jackson-datatype-jdk8-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.15.0\d41caa3a4e9f85382702a059a65c512f85ac230\jackson-databind-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\3.1.0\b06d1f0b08f6f8a2636e364c8941b2dabc4f0b77\spring-boot-autoconfigure-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\3.1.0\efa941e9a2162a3dd8c5e4679f46a24af9e5769f\spring-boot-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\3.1.0\4784b6e2adfe32720a4e2c009a62650835bba391\spring-boot-starter-logging-3.1.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\jakarta.annotation\jakarta.annotation-api\2.1.1\48b9bda22b091b1f48b13af03fe36db3be6e1ae3\jakarta.annotation-api-2.1.1.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\6.0.9\284ed111fa0b49b29f6fea6ac0afa402b809e427\spring-core-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.33\2cd0a87ff7df953f810c344bdf2fe3340b954c69\snakeyaml-1.33.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\10.1.8\7abb8ab966dd135924c8e69cb2f1db735dee5f23\tomcat-embed-websocket-10.1.8.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\10.1.8\ec4b884806c65c80c86bb3db134f6f6f99e79ed8\tomcat-embed-core-10.1.8.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\10.1.8\6f3a4ae2ae37270eeb6e9bec4e7207facdc9e8fa\tomcat-embed-el-10.1.8.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\6.0.9\be88c57829b9ec038774b47c241ac45673352a55\spring-context-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\6.0.9\8c1025bf9c1dc66f5268639866b5a45ed9bc62ef\spring-aop-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\6.0.9\745619eee32c8ead88a21c97748d2416f1db8dd9\spring-beans-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\6.0.9\f50a1df7ed038ee7ca85528aff652cef4ff4883b\spring-expression-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\io.micrometer\micrometer-observation\1.11.0\b3d1b34d16e7e8fa9087c5d51ec39bc3005e2733\micrometer-observation-1.11.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.15.0\89b0fd554928425a776a6e97ed010034312af21d\jackson-annotations-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.15.0\12f334a1dc9c6d2854c43ae314024dde8b3ad572\jackson-core-2.15.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.4.7\307944865579a6d490e6a4cbb5082dc8f36536ca\logback-classic-1.4.7.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-to-slf4j\2.20.0\d37f81f8978e2672bc32c82712ab4b3f66624adc\log4j-to-slf4j-2.20.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\2.0.7\a48f44aeaa8a5ddc347007298a28173ac1fbbd8b\jul-to-slf4j-2.0.7.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jcl\6.0.9\88d9ddfc6bbbf4047c2a8de8de94a425b06f636a\spring-jcl-6.0.9.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\io.micrometer\micrometer-commons\1.11.0\5de5da6be4f01128ab3995acdf86f2844137d4e4\micrometer-commons-1.11.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\ch.randelshofer\fastdoubleparser\0.8.0\85c25540369921659556ead85e02c99ef0d24280\fastdoubleparser-0.8.0.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.4.7\a2948dae4013d0e9486141b4d638d8951becb767\logback-core-1.4.7.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\2.0.7\41eb7184ea9d556f23e18b5cb99cad1f8581fc00\slf4j-api-2.0.7.jar;C:\Users\feshe\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.20.0\1fe6082e660daf07c689a89c94dc0f49c26b44bb\log4j-api-2.20.0.jar com.sparta.springprepare.SpringPrepareApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-13T10:43:58.748+09:00  INFO 20240 --- [           main] c.s.s.SpringPrepareApplication           : Starting SpringPrepareApplication using Java 17.0.7 with PID 20240 (D:\spring\spring-prepare\out\production\classes started by feshe in D:\spring)
2023-06-13T10:43:58.749+09:00  INFO 20240 --- [           main] c.s.s.SpringPrepareApplication           : No active profile set, falling back to 1 default profile: "default"
2023-06-13T10:43:59.125+09:00  INFO 20240 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-06-13T10:43:59.129+09:00  INFO 20240 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-13T10:43:59.129+09:00  INFO 20240 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-13T10:43:59.176+09:00  INFO 20240 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-13T10:43:59.176+09:00  INFO 20240 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 405 ms
2023-06-13T10:43:59.320+09:00  WARN 20240 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'
2023-06-13T10:43:59.322+09:00  INFO 20240 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-06-13T10:43:59.328+09:00  INFO 20240 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-06-13T10:43:59.337+09:00 ERROR 20240 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.


Process finished with exit code 1

Error, Failed 글자가 눈에 걸리지만 일단은 당장의 이슈를 해결하는 것이 목표이기 때문에 Description 확인!

가끔 겪었던 포트사용 중복 문제인듯 하다.


✅ 이슈 해결방법 01. 사용할 포트번호 변경

이렇게 application.properties (경로:src-main-resources)에서 변경해줄 수도 있고, 이 외에도 방법은 몇가지 있는 걸로 알고있다. 어쨌든 이러면 중복을 피하게 되므로 바로 해결!


✅ 이슈 해결방법 02. 프로세스 강제종료

일단 어느쪽에서 중복되어 사용되고 있는지를 확인하고자 한다면 이렇게 하면 된다.

netstat -a -o -n

보니까 저기 드래그 한 부분이 문제다.

 

확인했으니 cmd 창에서 톰캣의 default 포트인 8080를 사용하고 있는 task 즉, 프로세스를 강제로 종료할 수 있는 아래의 command를 날려주면 바로 삭제된다.

 

참고로 /f는 강제종료, /pid는 종료하고자 하는 프로세스 pid 값을 가리킨다.

//windows

taskkill /f /pid 포트번호

만약에 엑세스 거부가 뜬다면 cmd를 관리자 권한으로 실행하면 된다.

 

 

맥 운영체제를 사용하는 경우에는 아래의 명령어를 입력하면 암호 입력이 나온다. 이때 암호는 맥북을 켤 때 사용하는 그 암호라고 한다. 암호 입력 후 그 아래 명령어를 입력해주자. 

//mac

sudo lsof -i :포트번호
sudo kill 피드번호

✅ 이슈 해결 후

우리가 실행한 Spring 프로젝트가 서버에서 잘 실행되는 것을 확인!


✅ 참고한 자료모음

[블로그] HTTP 상태 코드 정리 | 와탭 블로그 (whatap.io)

[블로그] 서버 기동시 port 중복 해결방법! (tistory.com)


 

728x90