gpt4 book ai didi

angularjs - CORS:AngularJS + Resteasy 3 + Wildfly

转载 作者:行者123 更新时间:2023-12-04 13:31:46 27 4
gpt4 key购买 nike

我正在使用 AngularJS 和 Resteasy 开发一个应用程序。正如所料,我面临着众所周知的问题
XMLHttpRequest cannot load http://localhost:8080/..... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.
正如在其他堆栈溢出帖子中看到的 [1] ,我尝试从 Feature 对象中使用 Resteasy 的 CorsFilter,但我得到:

[33m02:06:57,883 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-1) failed to execute: javax.ws.rs.ForbiddenException: Origin not allowed: http://localhost:3000
at org.jboss.resteasy.plugins.interceptors.CorsFilter.checkOrigin(CorsFilter.java:194)
at org.jboss.resteasy.plugins.interceptors.CorsFilter.filter(CorsFilter.java:134)

我的 Cors 功能 目的:
@Provider
public class CorsFeature implements Feature {

@Override
public boolean configure(FeatureContext context) {
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
context.register(corsFilter);
return true;
}
}

web.xml 我补充说:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>org.jboss.resteasy.plugins.interceptors.CorsFilter</param-value>
</context-param>

我看到当我评论这个上下文参数时,我没有得到前面提到的异常并且响应状态是 200,而不是 403。

Angular 模块配置我补充说:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

我知道可以选择创建 node.js 代理,但我想以艰难的方式解决这个问题。
你能帮我克服生活中的这个大障碍吗?

谢谢 :)

稍后编辑:
我设法通过注释要素类 (CorsFeature) si @Component 来实现这一点。这样应用程序上下文就知道它了。

最佳答案

Wildfly 解决方案

  • 编辑 standalone.xml :

  • 配置
    <subsystem xmlns="urn:jboss:domain:undertow:3.0">
    <buffer-cache name="default"/>
    <server name="default-server">
    <http-listener name="default" socket-binding="http" redirect-socket="https"/>
    <host name="default-host" alias="localhost">
    <location name="/" handler="welcome-content"/>
    <filter-ref name="server-header"/>
    <filter-ref name="x-powered-by-header"/>
    <filter-ref name="Access-Control-Allow-Origin"/>
    <filter-ref name="Access-Control-Allow-Methods"/>
    <filter-ref name="Access-Control-Allow-Headers"/>
    <filter-ref name="Access-Control-Allow-Credentials"/>
    <filter-ref name="Access-Control-Max-Age"/>
    </host>
    </server>
    <servlet-container name="default">
    <jsp-config/>
    <websockets/>
    </servlet-container>
    <handlers>
    <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
    </handlers>
    <filters>
    <response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
    <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
    <response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="*"/>
    <response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/>
    <response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="accept, authorization, content-type, x-requested-with"/>
    <response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/>
    <response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/>
    </filters>
    </subsystem>
  • 重启 Wildfly
  • 关于angularjs - CORS:AngularJS + Resteasy 3 + Wildfly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337780/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com