Skip to content

Commit

Permalink
feat: Security,Cors 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
prislewarz committed Jan 8, 2024
1 parent 018c8e9 commit 7b55c68
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.0.4.RELEASE'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly 'com.h2database:h2'

//JWT
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.solutionchallenge.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

@Bean
public CorsFilter corsFilter() {

CorsConfiguration configuration = new CorsConfiguration();
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

configuration.setAllowCredentials(true); // 서버 응답 시 json 자바스크립트에서 처리 허용
configuration.addAllowedOrigin("http://localhost:3000");
configuration.addAllowedHeader("*"); // 모든 header 응답 허용
configuration.addAllowedMethod("*"); // 모든 post, get, put, delete, patch 요청 허용

source.registerCorsConfiguration("/**", configuration);
return new CorsFilter(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.solutionchallenge.common.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@RequiredArgsConstructor
@Configuration
@EnableWebSecurity
public class HttpSecurityConfig {

private final CorsConfig corsConfig;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable() //token을 쓰는 방식이라 필요 없음
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilter(corsConfig.corsFilter())
.formLogin().disable() //직접 만든 로그인 폼 쓸거라 필요없음
.httpBasic().disable()
.authorizeRequests()
.requestMatchers("/oauth2/**").permitAll()
.anyRequest().authenticated()
.and()
.cors().configurationSource(corsConfigurationSource());
return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
// 여기에 CORS 설정을 추가하세요. 예:
configuration.addAllowedOrigin("*");
configuration.addAllowedMethod("*");
configuration.addAllowedHeader("*");
configuration.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}

0 comments on commit 7b55c68

Please sign in to comment.