Skip to content

Commit

Permalink
hotfix : Security cors 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgolf committed Nov 11, 2022
1 parent d5c9439 commit f127c7b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/me/golf/blog/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.golf.blog.global.security.CustomAuthenticationProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
Expand All @@ -16,6 +17,9 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -43,7 +47,7 @@ public void configure(WebSecurity web) {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.cors()
.cors().configurationSource(corsConfigurationSource())
.and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
Expand All @@ -52,6 +56,7 @@ protected void configure(HttpSecurity http) throws Exception {
.authenticationProvider(customAuthenticationProvider())
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers(PUBLIC).permitAll()
.anyRequest().authenticated();
http
Expand All @@ -68,4 +73,18 @@ protected void configure(HttpSecurity http) throws Exception {
public CustomAuthenticationProvider customAuthenticationProvider() {
return new CustomAuthenticationProvider();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.setAllowCredentials(true);

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

0 comments on commit f127c7b

Please sign in to comment.