
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.
스프링부트에서 웹소켓 서버를 만들어서
간단한 플레시게임을 만드려고 했는데
엥?? 이전에는 이렇게 작성하면 별 문제 없었는데??
모든 Access-Control-Allow-Origin * 설정했는데??
뭐가 변경된거지?????? 😡 😡 😡 😡
현재 내 웹 컨픽, 소켓 설정은 아래와 같다.
package com.car.ndj.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","OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
}
package com.car.ndj.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic"); // 구독 prefix
config.setApplicationDestinationPrefixes("/app"); // 송신 prefix
}
}
setAllowedOrigins -> setAllowedOriginPatterns
사실 오류 내용을 직역하면
allowCredentials가 true인 경우, allowedOrigins는 "Access-Control-Allow-Origin" 응답 헤더에 설정할 수 없는 특수 값 "*"를 포함할 수 없습니다. 특정 출처 집합에 대한 자격 증명을 허용하려면 해당 출처 집합을 명시적으로 나열하거나 "allowedOriginPatterns"를 사용하는 것이 좋습니다
해결 방법이 다 있다.
allowedOrigin("*") 과 allowCredentials(true) 는 동시에 설정할 수 없는데
이 부분은 추후에 시간이 나면 내부 소스를 따라가보도록 하자 😩
웹소켓 설정과 웹 컨픽 설정을 수정하여 해결 할 수 있다. 🤔
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.