首页 > 解决方案 > 'Access-Control-Allow-Origin' 标头包含多个值 ', *',但在 spring boot stomp 配置中只允许一个值

问题描述

我在 spring boot 中配置了 stomp,我设置了 setAllowedOrigins 但是当通过 chrome 连接到 Sockjs 时出现错误,我该如何解决这个问题?

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    
    registry
    .addEndpoint("/agent")
    .setAllowedOrigins(Arrays.stream(domainAllowed.split(",")).toArray(String[]::new))
    //.setAllowedOrigins("*")
    .withSockJS();
    
    logger.info("out register");
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    
    logger.info("thong tin domain allowed: "+ domainAllowed);
    
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.stream(domainAllowed.split(",")).map(String::trim).collect(Collectors.toList()));
    configuration.setAllowCredentials(true);
    configuration.setMaxAge(-1L);
    configuration.setAllowedMethods(Arrays.asList("*"));
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

CORS 策略已阻止从源“https://cli.ovp.vn/agent/info?t=1632386394912”访问 XMLHttpRequest:“Access-Control-Allow-Origin”标头包含多个值“https” ://domain.work, *',但只允许一个。

stomp.min.js:8 哎呀!失去连接到

标签: javaspringspring-bootstomp

解决方案


推荐阅读