Skip to content

Commit

Permalink
SecurityFilterChain을 거치는 POST 요청을 처리하기 위한 CORS 설정 추가.
Browse files Browse the repository at this point in the history
SecurityFilterChain을 거치는 POST 요청을 처리하기 위한 CORS 설정 추가.
  • Loading branch information
xGreenNarae authored Nov 3, 2023
2 parents 9232cc5 + 7706441 commit 710fb56
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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 org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.http.HttpServletResponse;
Expand All @@ -27,27 +30,31 @@
public class SecurityConfig {
private final TokenProvider tokenProvider;

// Custom SecurityFilterManagerImpl 클래스를 통해 JWT 필터를 추가
public class SecurityFilterManagerImpl extends AbstractHttpConfigurer<SecurityFilterManagerImpl, HttpSecurity> {
@Override
public void configure(final HttpSecurity builder) throws Exception {
final AuthenticationManager authenticationManager = builder.getSharedObject(AuthenticationManager.class);
builder.addFilter(new JwtAuthenticationFilter(authenticationManager, tokenProvider))
.addFilterBefore(new JwtExceptionFilter(), JwtAuthenticationFilter.class);
super.configure(builder);
}
}

@Bean
public BCryptPasswordEncoder registerPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public CorsConfigurationSource configureCors() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.addAllowedOriginPattern("*");
configuration.setAllowCredentials(true);
configuration.addExposedHeader("Authorization");

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

@Bean
public SecurityFilterChain securityFilterChain(final HttpSecurity http,
@Autowired @Qualifier("handlerExceptionResolver")
final HandlerExceptionResolver resolver) throws Exception {
@Autowired @Qualifier("handlerExceptionResolver") final HandlerExceptionResolver resolver) throws Exception {
http.cors().configurationSource(configureCors());
http.csrf().disable();

http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

http.formLogin().disable();
Expand All @@ -67,13 +74,24 @@ public SecurityFilterChain securityFilterChain(final HttpSecurity http,
});

http.authorizeRequests(
authorize -> authorize
.antMatchers(HttpMethod.POST, "/pet/**").hasAuthority("SHELTER")
.antMatchers(HttpMethod.PATCH).hasAuthority("SHELTER")
.antMatchers(HttpMethod.PUT).hasAuthority("SHELTER")
.anyRequest().permitAll()
authorize -> authorize
.antMatchers(HttpMethod.POST, "/pet/**").hasAuthority("SHELTER")
.antMatchers(HttpMethod.PATCH).hasAuthority("SHELTER")
.antMatchers(HttpMethod.PUT).hasAuthority("SHELTER")
.anyRequest().permitAll()
);

return http.build();
}

// Custom SecurityFilterManagerImpl 클래스를 통해 JWT 필터를 추가
public class SecurityFilterManagerImpl extends AbstractHttpConfigurer<SecurityFilterManagerImpl, HttpSecurity> {
@Override
public void configure(final HttpSecurity builder) throws Exception {
final AuthenticationManager authenticationManager = builder.getSharedObject(AuthenticationManager.class);
builder.addFilter(new JwtAuthenticationFilter(authenticationManager, tokenProvider))
.addFilterBefore(new JwtExceptionFilter(), JwtAuthenticationFilter.class);
super.configure(builder);
}
}
}

0 comments on commit 710fb56

Please sign in to comment.