头中的响应必须不是通配符“*”时,请求的凭证模式是“包括”

山姆:

我用Auth0我的用户身份验证的用户只允许登录访问一个Spring(引导)RestController在这一点上我创建一个实时信息功能,用户可以从发送邮件Angular 2客户端(localhost:4200)到春节服务器:使用(本地主机8081)stompjssockjs

当试图创建一个践踏的客户端和我开始收到下面的控制台错误的连接:

 The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

研究这个问题后,它看起来是不可能的设置选项起源= *和凭证=在同一时间如此。我怎样才能解决这个时候我已经设置允许起源于WebSocketConfig到客户端领域?

角2成分

connect() {
    var socket = new SockJS('http://localhost:8081/chat');
    this.stompClient = Stomp.over(socket);  
    this.stompClient.connect({}, function(result) {
        console.log('Connected: ' + result);
        this.stompClient.subscribe('/topic/messages', function(message) {
            console.log(message);
        });
    });
}    

WebSocketConfig

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/chat").setAllowedOrigins("http://localhost:4200").withSockJS();
    }
}

本地主机:8081 /聊天/信息T = 1490866768565

{"entropy":-1720701276,"origins":["*:*"],"cookie_needed":true,"websocket":true}

MessageController

public class MessageController {
    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public Message send(Message message) throws Exception {
        return new Message(message.getFrom(), message.getText());
    }
}

SecurityConfig(暂时允许所有)

public class SecurityConfig extends Auth0SecurityConfig {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }
}

UPDATE

经过一些测试和研究似乎问题只使用Chrome发生。问题可能涉及:https://github.com/sockjs/sockjs-node/issues/177

UPDATE

我创建CORSFilter等chsdk提到和所使用的addFilterBefore()方法:https://stackoverflow.com/a/40300363/4836952

@Bean
CORSFilter corsFilter() {
    CORSFilter filter = new CORSFilter();
    return filter;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilterBefore(corsFilter(), SessionManagementFilter.class).authorizeRequests().anyRequest().permitAll();
    http.csrf().disable();
}

我可以看到,滤波器是由调试,但错误信息不断出现在即使正确的访问控制允许来源被设置客户方叫:

在这里输入图像描述

cнŝdk:

问题:

您没有配置'Access-Control-Allow-Origin'正确,您的当前配置可以简单地通过服务器忽略。

情况:

错误堆栈跟踪说:

所述的值'Access-Control-Allow-Origin'在响应头必须不是通配符'*'时,请求的凭证模式为“包括”。原产地“ 的http://本地主机:4200 ”,因此是不允许的访问。

这意味着,除了事实,你不能设置'Access-Control-Allow-Origin'为通配符"*",您的域名'http://localhost:4200'是不允许访问过。

要回答你的问题:

我怎样才能解决这个时候我已经设置允许起源于WebSocketConfig到客户端领域?

解:

我猜你不需要设置允许起源于WebSocketConfig,因为它意味着配置的WebSocket式的消息在Web应用程序中规定的WebSocket支持 Spring文档中,则需要对其进行配置,在CORSFilter配置类,因为它的意思配置弹簧过滤器的Web应用程序的访问。

这是你需要在你的什么CORSFilter.java配置类:

public class CORSFilter implements Filter {

    // This is to be replaced with a list of domains allowed to access the server
  //You can include more than one origin here
    private final List<String> allowedOrigins = Arrays.asList("http://localhost:4200"); 

    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // Lets make sure that we are working with HTTP (that is, against HttpServletRequest and HttpServletResponse objects)
        if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;

            // Access-Control-Allow-Origin
            String origin = request.getHeader("Origin");
            response.setHeader("Access-Control-Allow-Origin", allowedOrigins.contains(origin) ? origin : "");
            response.setHeader("Vary", "Origin");

            // Access-Control-Max-Age
            response.setHeader("Access-Control-Max-Age", "3600");

            // Access-Control-Allow-Credentials
            response.setHeader("Access-Control-Allow-Credentials", "true");

            // Access-Control-Allow-Methods
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

            // Access-Control-Allow-Headers
            response.setHeader("Access-Control-Allow-Headers",
                "Origin, X-Requested-With, Content-Type, Accept, " + "X-CSRF-TOKEN");
        }

        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {
    }
}

你可以看到使用:

private final List<String> allowedOrigins = Arrays.asList("http://localhost:4200");

要设置允许访问服务器的域列表。

参考文献:

您可能需要看一看在Spring框架CORS支持启用跨源请求RESTful Web服务有关进一步阅读。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Blazor发出CORS请求时,如何包括凭证?

当请求的凭据模式为“ include”时,响应中的“ Access-Control-Allow-Origin”标头不得为通配符“ *”

当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *”

当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *”

如何在amCharts4请求中包括凭证?

将工作表另存为单独的PDF:“发出API请求时必须包括API令牌。”

Haskell中的请求,响应模式

如何记录 HttpClient 请求,响应包括正文?

无法使内容编码显示在get请求的响应标头中

JwtAuthProvider 在不是 httpOnly 的响应头中发出 bearToken - ServiceStack

类型错误:列表索引必须是整数或切片,而不是解析 json 请求时的 str

对预检请求的响应未通过访问控制检查:在标头中使用身份验证时,它没有HTTP正常状态

当请求不是目录中的文件时,FallbackResource将响应500错误

仅在Chrome(Service Worker)中:'...对于重定向模式不是“跟随”的请求,使用了重定向响应'

如何使用WebSockets执行请求-响应模式

使用Spring amqp库的请求-响应模式

TPL数据流的请求/响应模式

RESTful 服务。不同的响应和请求模式

在同步请求-响应模式中使用会话

AWS SQS异步排队模式(请求/响应)

在Rails的生产模式下对HTTP请求的响应

骆驼RabbitMQ请求响应模式示例

相关模式匹配要求使用通配符而不是正确的类型

Spring WS生成通知而不是请求-响应

得到休息请求的结果而不是响应?

http请求响应不是json格式

尝试请求OAuth access_token时,我得到:TypeError:encode()参数1必须为字符串,而不是None

当我必须向HTTP请求发送有效载荷时,应该使用PUT而不是DELETE吗?

HMAC apisign在请求标头中,但标头必须是str错误?