Skip to content

Commit

Permalink
Merge pull request #197 from cathaaaaand/dev
Browse files Browse the repository at this point in the history
fix: cors ์ˆ˜์ •
  • Loading branch information
daeundada authored May 13, 2024
2 parents 6fb4dad + ceeae5b commit f657ac1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public ResponseEntity<CommonResponse<LoginResponseDTO>> login(
LoginResponseDTO loginResponseDTO = authService.login(requestDTO);
String jwtToken = jwtUtil.createToken(loginResponseDTO.getUserId().toString());

/*
// ํ† ํฐ์„ ์ฟ ํ‚ค์— ์ €์žฅ
Cookie authCookie = new Cookie("Authorization", jwtToken);
authCookie.setHttpOnly(true); // ์ฟ ํ‚ค๋ฅผ HTTP ํ†ต์‹ ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •
authCookie.setSecure(true); // HTTPS๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์ฟ ํ‚ค ์ „์†ก
authCookie.setPath("/"); // ์ฟ ํ‚ค๊ฐ€ ์ „์†ก๋˜๋Š” ๊ฒฝ๋กœ
authCookie.setMaxAge(7 * 24 * 60 * 60); // ์ฟ ํ‚ค์˜ ๋งŒ๋ฃŒ ์‹œ๊ฐ„ ์„ค์ • (์˜ˆ: 7์ผ)
response.addCookie(authCookie); // ์‘๋‹ต์— ์ฟ ํ‚ค ์ถ”๊ฐ€
*/

response.setHeader(HttpHeaders.AUTHORIZATION, jwtToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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;
Expand All @@ -16,10 +15,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;

import java.util.Arrays;
import java.util.Collections;

@Configuration
@EnableWebSecurity
Expand All @@ -40,42 +35,11 @@ public JwtAuthorizationFilter jwtAuthorizationFilter() {
return new JwtAuthorizationFilter(jwtUtil, userDetailsService);
}

/* @Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.setAllowedOrigins(Collections.singletonList("*")); // ๋ชจ๋“  ์ถœ์ฒ˜ ํ—ˆ์šฉ
configuration.setAllowedMethods(Collections.singletonList("*")); // ๋ชจ๋“  ๋ฉ”์„œ๋“œ ํ—ˆ์šฉ
configuration.setAllowedHeaders(Collections.singletonList("*")); // ๋ชจ๋“  ํ—ค๋” ํ—ˆ์šฉ
configuration.addExposedHeader("Authorization");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}*/

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.addFilterBefore(new CustomCorsFilter(), UsernamePasswordAuthenticationFilter.class)
.csrf(AbstractHttpConfigurer::disable)
.cors(corsCustomize -> corsCustomize.configurationSource(request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOriginPatterns(Arrays.asList(
"https://www.wooriforei.info",
"https://cat.wooriforei.info",
"http://localhost:3000"
));
config.addAllowedMethod(HttpMethod.OPTIONS);
config.addAllowedMethod(HttpMethod.GET);
config.addAllowedMethod(HttpMethod.POST);
config.addAllowedMethod(HttpMethod.PUT);
config.addAllowedMethod(HttpMethod.DELETE);
config.addAllowedHeader("Authorization, Content-Type, Accept");
config.setMaxAge(3600L);
return config;
}))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/schedulers/**").authenticated()
Expand Down

0 comments on commit f657ac1

Please sign in to comment.