Skip to content

Commit

Permalink
๐Ÿ› CORS ์˜ค๋ฅ˜ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
- ๋ฐฐํฌ ์‹œ ๋ฐœ์ƒํ•˜๋Š” CORS ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • Loading branch information
Ganghee-Lee-0522 committed Jan 12, 2024
1 parent 6a5a127 commit ff40441
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;

import java.util.Collections;

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -41,9 +46,24 @@ public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

CorsConfigurationSource corsConfigurationSource() {
return request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedOriginPatterns(Collections.singletonList("http://localhost:3000"));
config.setAllowedOriginPatterns(Collections.singletonList("https://kidari.site"));
config.setAllowedOriginPatterns(Collections.singletonList("https://kidari.site:3000"));
config.setAllowCredentials(true);
return config;
};
}

@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic(HttpBasicConfigurer::disable)
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource()))
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.authorizeHttpRequests((authorizeRequests) -> {
Expand All @@ -56,7 +76,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.addFilterBefore(new JwtExceptionFilter(), JwtFilter.class) // JwtExceptionFilter๋ฅผ JwtFilter ์•ž์— ์ถ”๊ฐ€

.logout(log -> log
.logoutUrl("/api/logout")
.logoutUrl("/logout")
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
)
// OAuth 2.0 ๋กœ๊ทธ์ธ ์„ค์ • ์‹œ์ž‘
Expand All @@ -77,11 +97,11 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.build();
}

public class CorsConfigurer extends AbstractHttpConfigurer<CorsConfigurer, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.addFilter(corsConfig.corsFilter());
}
}
// public class CorsConfigurer extends AbstractHttpConfigurer<CorsConfigurer, HttpSecurity> {
// @Override
// public void configure(HttpSecurity http) throws Exception {
// http
// .addFilter(corsConfig.corsFilter());
// }
// }
}

0 comments on commit ff40441

Please sign in to comment.