Skip to content

Commit 1855bab

Browse files
committed
#31 BE : [FIX] CORS에러 해결을 위한 Spring Security 수정
1 parent 5dc5e7f commit 1855bab

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

server/src/main/java/org/cecd/server/external/SecurityConfig.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import jakarta.servlet.http.HttpServletRequest;
2222
import jakarta.servlet.http.HttpServletResponse;
2323
import org.springframework.security.core.AuthenticationException;
24+
import org.springframework.web.cors.CorsConfiguration;
2425

2526
import java.io.IOException;
2627

@@ -38,11 +39,19 @@ public class SecurityConfig {
3839
@Bean
3940
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
4041
httpSecurity
42+
.cors(cors -> cors.configurationSource(request -> {
43+
CorsConfiguration configuration = new CorsConfiguration();
44+
configuration.addAllowedOrigin("https://dgutestbed.netlify.app");
45+
configuration.addAllowedMethod("*");
46+
configuration.addAllowedHeader("*");
47+
configuration.setAllowCredentials(true);
48+
return configuration;
49+
}))
4150
.csrf(csrf -> csrf.disable()) // CSRF 비활성화
4251
.sessionManagement(session ->
4352
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // 상태 없는 세션 관리
4453
.addFilterBefore(new JwtTokenFilter(memberService, jwtTokenUtil), UsernamePasswordAuthenticationFilter.class)
45-
.authorizeHttpRequests(authorize -> authorize // 권한 설정
54+
.authorizeHttpRequests(authorize -> authorize
4655
.requestMatchers("/jwt-login/info").authenticated()
4756
.requestMatchers("/jwt-login/admin/**").hasAuthority(MemberRole.ADMIN.name())
4857
.requestMatchers("/ws/**").permitAll() // WebSocket 엔드포인트 허용

0 commit comments

Comments
 (0)