【问题标题】:Dart CORS doesn't work飞镖 CORS 不起作用
【发布时间】:2017-01-31 00:34:55
【问题描述】:

您好,我想向我的 Spring 服务器发出请求。现在,由于 CORS 选项受限,我收到错误消息。 所以我添加了一个过滤器,因为注释不起作用:

@Component
public class CORSFilter implements Filter {

    public CORSFilter() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(request, response);
    }
    @Override
    public void init(FilterConfig filterConfig) {
    }
    @Override
    public void destroy() {

    }}

现在我的问题是,cors 过滤器不适用于 dart 请求。 在正常的浏览器请求中,标头已设置,但在 dart http 请求中未设置。

有什么办法可以解决这个问题吗?

2016 年 9 月 23 日更新: 这是http://pastebin.com/9KNfx7Jd 问题是过滤器不受此 http 调用的影响。 只有当我通过浏览器中的 URL 访问文件时它才有效。

这里有 ajax:

Remote Address:127.0.0.1:8090 Request URL:http://localhost:8090/time/time/login Request Method:OPTIONS Status Code:401 Unauthorized Response Headers view source Cache-Control:no-cache, no-store, max-age=0, must-revalidate Connection:keep-alive Content-Length:114 Content-Type:text/html;charset=UTF-8 Date:Fri, 23 Sep 2016 12:57:55 GMT Expires:0 Pragma:no-cache Server:WildFly/10 Set-Cookie:JSESSIONID=ZIkzLq-iALC6CDx7r6LhPz_8PiD05Q9ufod6GluZ.ccn6dc2; path=/time WWW-Authenticate:Basic realm="Realm" X-Content-Type-Options:nosniff X-Frame-Options:DENY X-Powered-By:Undertow/1 X-XSS-Protection:1; mode=block Request Headers view source Accept:*/* Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:content-type Access-Control-Request-Method:GET Connection:keep-alive Host:localhost:8090 Origin:http://localhost:8080 Referer:http://localhost:8080/ User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.104 (Dart) Safari/537.36

这里没有:

Remote Address:127.0.0.1:8090 Request URL:http://localhost:8090/time/time/login Request Method:GET Status Code:200 OK Response Headers view source Access-Control-Allow-Origin:* Cache-Control:no-cache, no-store, max-age=0, must-revalidate Connection:keep-alive Content-Length:5 Content-Type:text/html;charset=ISO-8859-1 Date:Fri, 23 Sep 2016 13:10:36 GMT Expires:0 Pragma:no-cache Server:WildFly/10 Set-Cookie:JSESSIONID=nQFjGB2m7ovHVT9VUnhtCJSXZvEZV4WWH0YCrgFk.ccn6dc2; path=/time X-Content-Type-Options:nosniff X-Frame-Options:DENY X-Powered-By:Undertow/1 X-XSS-Protection:1; mode=block Request Headers view source Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Authorization:Basic c2tvYmxlcjpTMW1vbjUyNzli Cache-Control:max-age=0 Connection:keep-alive Cookie:JSESSIONID=oHJ4GvQ8pFNv8HSujI49NRXQxoVSVMM580sSrvJW.ccn6dc2 Host:localhost:8090 Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.104 (Dart) Safari/537.36

编辑 26.09.2016:

好的,我现在将我的 SecurityConfig 更改为:

@Override protected void configure(final HttpSecurity http) throws Exception { super.configure(http); http.addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class); http.authorizeRequests().antMatchers(HttpMethod.OPTIONS).permitAll(); http.authorizeRequests().antMatchers("/**").authenticated(); } 现在过滤器正在调用,但我现在收到一个新错误:Response for preflight has invalid HTTP status code 401

标题: Access-Control-Allow-Origin:* Cache-Control:no-cache, no-store, max-age=0, must-revalidate Connection:keep-alive Content-Length:114 Content-Type:text/html;charset=UTF-8 Date:Mon, 26 Sep 2016 12:30:39 GMT

【问题讨论】:

  • “飞镖请求”是什么意思?您的意思是在从 dart 代码触发的请求中,标头不存在于对请求的响应中?
  • 是的,我就是这个意思
  • 如果这里没有提供任何信息,实际上不可能看到可能出现的问题。您的 dart 应用程序的一些代码和失败的示例 HTTP 请求的转储将是一个开始。
  • link这里是http调用函数的链接

标签: spring dart


【解决方案1】:

好的,我解决了禁用铬的网络安全问题。 感谢大家帮助我:)

【讨论】:

    【解决方案2】:

    您的过滤器似乎不适用于 OPTIONS 请求。
    对此博文的评论表明需要明确启用OPTIONS 请求:

    https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

    我在使用带有 Spring MVC 的 CORS(使用 Filter 或 HandlerInterceptor 时)和 Spring Security 时发现的一个“问题”是,您需要明确允许所有 OPTIONS 请求以正确处理预飞行。 CORS 的 W3C 规范说飞行前请求不应该发送凭据,但是我发现有些浏览器确实发送了凭据,而其他浏览器则没有。因此,如果您不允许All OPTIONS,如果浏览器未发送凭据,则会收到 403。

    预检请求是在使用 Spring Security 时需要专门配置的,还是会在过滤器链之前处理预检?

    另见

    【讨论】:

    • 好吧,我在 SecurityConfig 类中做了以下操作:http.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll(); 但它没有修复错误XMLHttpRequest cannot load http://localhost:8090/time/time/jsontime. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
    • 这解决了你的问题?
    • 抱歉,新行有问题^^。不,它没有:(。我编辑了上面的评论。
    • 我对 Spring 了解不多,但我确信这需要在服务器上而不是在 Dart 端修复(因为 Dart 标签,我偶然发现了你的问题)。跨度>
    • 所以当我禁用安全性时它工作正常。但是由于用户凭据,我必须使用安全性。也许还有另一种解决方法可以解决此问题。
    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 2021-02-26
    • 2022-06-20
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多