Spring/Spring 블로그만들기

[Springboot] 나만의 블로그 만들기 - 5. MySQL 스프링 연결

개발자하소서 2022. 3. 7. 23:30
728x90
반응형
SMALL

📌 MySQL 스프링 연동

 

1. application.properties ----> .yml 로 바꾼다.

 

- properties를 안쓰고 yml을 쓰는 이유는 중복성을 제거해주고, 들여쓰기를 통해서 가독성을 높여준다.

 

 

2. MySQL 프로젝트 연결

 

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Seoul
    username: cos
    password: cos1234

 

3. 의존성 주석처리 - build gradle

 

  - 이전에 설정한 implementation "com.h2database:h2" 를 주석처리한다.

 

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
     compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.security:spring-security-test'
  
   // implementation "com.h2database:h2"

    implementation 'javax.servlet:jstl'											// JSTL
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'			// JSP 탬플릿 엔진
    implementation 'org.springframework.security:spring-security-taglibs'		// Security 태그 라이브러리implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

}

 

4. Port 8080 is already in use 에러

 - 이미 나는 oracle이 있기때문에 8080 포트를 사용하니 이미 존재하는 port라고 에러가 떴다.

 - 포트번호가 충돌하는 것이다.

 - 해결방안은 ?

   포트번호를 변경해주는 것이다.

   application.yml에서 다음과 같이 포트번호를  8081로 변경하여 준다

# application.yml
+ server.port: 8081

 

 

5 . localhost 로그인 창이 뜰 때

 

 

- port번호도 바꿨고, sts에서 run도 잘되었음에도 

  http://localhost:8081/test/hello 에 접속해보니 계속 로그인 창이 떴다.  

 

- 이는 우리가 dependencies에서 설정한 security의 영향으로 이 주소에 대한 모든 접근을 스프링 시큐리티가 일단 막고     보는 것이다. 

 

-  해결방안은?

  security부분을 주석처리하는 것이다.

  즉, 아예 이런 로그인 창이 뜨지 않게 하려면 build.gradle 파일에서 아래 두 줄을 주석처리 하면 된다.

  // implementation 'org.springframework.boot:spring-boot-starter-security'
  // testImplementation 'org.springframework.security:spring-security-test'

 

- 전체적인 build.gradle 에서의 dependecies 형태

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    // implementation 'org.springframework.boot:spring-boot-starter-security'
     // testImplementation 'org.springframework.security:spring-security-test'
     implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
     compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
  
   // implementation "com.h2database:h2"

    implementation 'javax.servlet:jstl'											// JSTL
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'			// JSP 탬플릿 엔진
    implementation 'org.springframework.security:spring-security-taglibs'		// Security 태그 라이브러리implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

}

 

 

6 . http://localhost:8081/test/hello 접속

 

실행 결과

 

 

 

 

728x90
반응형
LIST