[Project] 프로젝트 만드는법 리액트 + 스프링부트+ 스프링시큐리티~2.백엔드 프로젝트 생성하기~()_react springboot_--신촌 더조은 학원 2차 프로젝트 +How to create a project with Spring Boot and React+
[프로젝트] 리액트 + 스프링부트로 프로젝트 만드는법~2.백엔드 프로젝트 생성하기
1. 스프링부트 프로젝트 생성 방법 (프론트엔드와 별도의 폴더에 백엔드 프로젝트생성)
- 아래 링크 참고
https://rockbottomdevbus.blogspot.com/2024/04/springboot-1-325-intellij-idea-20241.html
2. 스프링 시큐리티 설정 CORS 설정(frontEnd 와 통신을위해)
CustomSecurityConfig.java
package com.project.back.config;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
@Configuration
@Log4j2
@EnableMethodSecurity
@RequiredArgsConstructor
public class CustomSecurityConfig {
@Bean
public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception {
log.info("------------------------ configure ----------------------------------");
//CORS 설정 적용
http.cors(httpSecurityCorsConfigurer -> {
httpSecurityCorsConfigurer.configurationSource(corsConfigurationSource());
});
return http.build();
}
// CORS 필터 Bean 설정
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 접근할 URL을 지정하여 처리... "*" 는 모든 주소로의 접근 허용. 대상 지정하면 지정 대상만 접근 가능...
corsConfiguration.setAllowedOriginPatterns(Arrays.asList("*"));
corsConfiguration.setAllowedMethods(Arrays.asList("HEAD","GET","POST","PUT","DELETE"));
corsConfiguration.setAllowedHeaders(Arrays.asList("Authorization","Cache-Control","Content-Type"));
corsConfiguration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration); // "/**" - 하위 모든 폴더.....
return source;
}
}
3. build.gradle (학원 2차프로젝트에서 사용한 gradle)
buildscript {
ext{
queryDslVersion = "5.0.0"
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.7'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'com.project'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
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'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testAnnotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
// validation 필수값 입력
implementation 'org.springframework.boot:spring-boot-starter-validation'
// swagger UI 설정
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
//modelmapper
implementation 'org.modelmapper:modelmapper:3.2.0'
//토큰값 설정
implementation 'io.jsonwebtoken:jjwt-api:0.12.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.5', 'io.jsonwebtoken:jjwt-jackson:0.12.5'
// 인증 정보 JSON 문자열 처리를 위한 gson 라이브러리
implementation 'com.google.code.gson:gson:2.10.1'
// Querydsl 관련 라이브러리
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta"
annotationProcessor(
"jakarta.persistence:jakarta.persistence-api",
"jakarta.annotation:jakarta.annotation-api",
"com.querydsl:querydsl-apt:${queryDslVersion}:jakarta"
)
// thymleaf 관련 라이브러리
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
//Thymeleaf의 레이아웃 기능을 위한 추가 라이브러리
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.3.0'
// //ThymeLeaf에서 스프링시큐리티 사용하기 위한 라이브러리
// implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.2.RELEASE'
//소셜로그인을 위한 oauth2
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
}
tasks.named('test') {
useJUnitPlatform()
}
sourceSets {
main{
java{
srcDirs = ["$projectDir/src/main/java", "$projectDir/build/generated"]
}
}
}
//compile정리
compileJava.dependsOn('clean')
4. application.properties (학원 2차프로젝트에서 사용한 gradle)
spring.application.name=REACT_TFT
server.port=8090
# datasource setting
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/webdb?serverTimezone=Asia/Seoul
spring.datasource.username=spring
spring.datasource.password=spring
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
logging.level.org.springframework=info
logging.level.com.project.react_tft=debug
logging.level.org.springframework.security=trace
spring.security.oauth2.client.provider.kakao.authorization-uri=https://kauth.kakao.com/oauth/authorize
spring.security.oauth2.client.provider.kakao.user-name-attribute=id
spring.security.oauth2.client.provider.kakao.token-uri=https://kauth.kakao.com/oauth/token
spring.security.oauth2.client.provider.kakao.user-info-uri=https://kapi.kakao.com/v2/user/me
spring.security.oauth2.client.registration.kakao.redirect-uri=http://localhost:8090/login/oauth2/code/kakao
spring.security.oauth2.client.registration.kakao.client-name=kakao
spring.security.oauth2.client.registration.kakao.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.kakao.client-id=307632e76fbc1389bc9e576b16717d8d
spring.security.oauth2.client.registration.kakao.client-secret=8Ahu44sn0Kh5TvUGHM3Q7TuEJPyWC7mr
spring.security.oauth2.client.registration.kakao.client-authentication-method=client_secret_post
spring.security.oauth2.client.registration.kakao.scope=profile_nickname,account_email
spring.jackson.serialization.write-dates-as-timestamps=false
jwt.secret=32qkdlxmfmfcodnfurhsorkdlrjftmrhdltekwlqdprkrhtlvrhdkwlreh51qkdlxmek256qkdlxmfmfcodnfksmsepakfehdksehlsekdkwlreh104qkdlxmrhdlrjdkfvkqptgkskekd1qkdlxmfksmsrpakfdldksehlsekdlrjfdhogjsgjsjsjekcodnrhdksgdkdltskgkwlqdprkrhtlvekdlwpekcodnjrksekekcodnjtekdkssudqq
5. 깃허브 (backEnd 포맷 소스)
댓글
댓글 쓰기