quack337 / bbs1

0 stars 0 forks source link

로그인 #5

Open quack337 opened 4 months ago

quack337 commented 4 months ago

URL context path

URL은 다음과 같은 요소로 구성된다.

HTTP://SERVER:PORT/CONTEXT_PATH/PATH?QUERY_STRING

spring boot 프로젝트를 서버에 설치하고 실행하는 방법은 둘이다.

  1. tomcat 서버에 설치해서 실행하기
  2. 독립적으로 실행하기

위의 두 경우에 URL의 context path 부분이 서로 다르다.

tomcat 서버에 설치해서 실행하기

bbs1 프로젝트를 빌드한 bbs1.war 파일을 tomcat 서버 디렉토리 아래의 webapps 디렉토리에 복사하면 된다.

이렇게 실행하는 경우 URL의 context path는 프로젝트 이름인 /bbs1 이다. 따라서 login URL은 http://server:port/bbs1/login 이다. 여기서 포트 번호는 tomcat 서버가 차지하고 있는 포트번호이다.

독릭접으로 실행하기

쉘에서 java -jar bbs1.war 명령을 실행하면 bbs1 프로젝트가 별도의 프로세스로 실행된다.

이렇게 실행하는 경우 URL의 context path는 그냥 / 이다. 따라서 login URL은 http://server:port/login 이다. 여기서 포트 번호는 application.properties 파일의 server.port 항목에 설정한 값이다.

thymeleaf에서 URL context path

백엔드 강의에서 절대 경로 URL을 다음과 같이 구현하였다.

<a href="/login">로그인</a>

spring boot 프로젝트를 Run - Run As - Spring Boot App 메뉴로 실행할 때도 프로젝트가 별도의 프로세스로 실행되기 때문에, context path가 그냥 / 이다. 따라서 위 a 태그의 href="/login" URL이 잘 작동한다.

그런데 bbs1 프로젝트를 tomcat 서버에 설치해서 실행하면, context path가 /이 아니고 /bbs1 이기 때문에, 위 a 태그를 다음과 같이 수정해야 한다.

<a href="/bbs1/loing">로그인</a>

th:href

thymeleaf의 th:href 기능을 활용하면 URL의 context 부분이 자동으로 수정된다.

<a th:href="@{/login}">로그인</a>

위와 같이 구현하면, 위 thymeleaf 소스코드가 실행되어 HTML 태그가 출력될 때 context path 부분이 자동으로 수정된다.

quack337 commented 4 months ago

pom.xml 파일 수정

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
quack337 commented 4 months ago

commit: 로그인 #5

quack337 commented 4 months ago

실행 URL: https://app.garam.click/bbs1/login