1️⃣ 오류1
오류 메세지
java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
원인
Spring Security의 CORS 설정이 잘못 되어 있었다.
Security에서 CORS 설정시
.addAllOrigin("*") 과 .allowCrededentials(true) 를 같이 쓸 수 없다.
allowCredential 이 true 라면 “Access-Control-Allow-Origin” 응답 헤더헤더를 설정 할 수 없기 때문에 특수 값인 "*"를 사용 못하는 것...
해결방법
해결방법은 특정 주소만 허락하거나, .addAllOrigin("*") 대신 .addAllOriginPatterns("**")를 사용
⇒ 하지만 또 오류가 나서 확인해보니 All을 빼고 .allowedOriginPatterns("*") 을 사용하라고 한다.
변경 전
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("Authorization", "Content-Type")
.exposedHeaders("Custom-Header")
.allowCredentials(true);
.maxAge(3600);
}
}
변경 후
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("Authorization", "Content-Type")
.exposedHeaders("Custom-Header")
.allowCredentials(true);
.maxAge(3600);
}
}
2️⃣ cmd에서 포트 확인하고 죽이기
run 했을 때, 이미 열린 포트라는 오류가 떴다면 포트를 종료하고 다시 실행 해야한다.
방법
netstat -ano 를 입력하면 모든 포트 목록이 다 열린다. 죽이고 싶은 포트의 PID번호를 확인한 후, taskkill /f /pid PID번호 를 입력하면 해당 포트를 종료시킨다.
3️⃣ 오류2
오류 메세지
java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not found in class file either.
원인
클래스 파일의 파라미터 이름 정보가 없다는 것
문제가 된 코드
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
public String test(@RequestParam String name) { // 받는 파라미터의 변수 이름과 리액트에서 요청 시 보내는 key의 이름이 동일
System.out.println(name); // 출력 확인
return null;
}
}
Spring Boot의 TestController 클래스의 test 메소드이다.
@RequestParam을 정의하면서 name 또는 value 속성을 명시해주지 않은 것이 원인이다.
바인딩 되는 파라미터의 이름을 명시해주지 않았기 때문에 해당 파라미터를 찾지 못해 발생된다.
해결방법1
@RequestParam에 name 또는 value 속성을 명시해주기
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
public String test(@RequestParam(value="name") String name) { // 받는 파라미터의 변수 이름과 리액트에서 요청 시 보내는 key의 이름이 동일
System.out.println(name); // 출력 확인
return null;
}
}
해결방법2
javac 컴파일 시, ‘-parameters’ 옵션을 추가해준다. ⇒ 컴파일 시, 메소드의 파라미터명을 유지하여 class 파일을 생성하도록 설정하기
Gradle의 build.gradle 파일에 아래 내용을 추가
compileJava {
options.compilerArgs << '-parameters'
}
💻 정상 작동되는 과정
스프링부트에서 포트 지정
스프링부트 정상 실행
리액트에서 포트 지정
리액트 정상 실행
콘솔 확인
'Project 댕린이집' 카테고리의 다른 글
[React] React의 동적 라우팅, useParams (0) | 2024.01.18 |
---|---|
[React] React-Router-Dom (0) | 2024.01.17 |
Axios (0) | 2024.01.11 |
React 연습 (0) | 2024.01.06 |
React의 state를 알아보자 (0) | 2024.01.05 |