在resteasy中添加corsfilter后无法访问服务 [英] Cannot access services after adding corsfilter in resteasy

查看:150
本文介绍了在resteasy中添加corsfilter后无法访问服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将corsfilter添加到ApplicationPath后,我无法访问任何服务

I cannot access any of my services after adding corsfilter to my ApplicationPath

这是例子

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import org.jboss.resteasy.plugins.interceptors.CorsFilter;


@ApplicationPath("/")
public class JaxRsActivator extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public JaxRsActivator() {
        CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().add("*");
        corsFilter.setAllowedHeaders("Content-Type");
        singletons.add(corsFilter);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }

}

如果我卷发,我会得到的,

If I do a curl I get this,

curl -i http://localhost:8080/unika/usuarios -H "Origin:otherdomain.com"
HTTP/1.1 404 Not Found
Connection: keep-alive
Access-Control-Allow-Origin: otherdomain.com
X-Powered-By: Undertow 1
Server: Wildfly 8
Access-Control-Allow-Credentials: true
Content-Length: 0
Date: Tue, 16 Jun 2015 16:18:15 GMT

在服务器上我得到了

11:18:15,533警告[org.jboss.resteasy.core.ExceptionHandler](默认任务10)执行失败:javax.ws.rs.NotFoundException:找不到完整路径的资源: http://localhost:8080/unika/usuarios 在org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)上[resteasy-jaxrs-3.0.10.Final.jar:]

11:18:15,533 WARN [org.jboss.resteasy.core.ExceptionHandler] (default task-10) failed to execute: javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/unika/usuarios at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73) [resteasy-jaxrs-3.0.10.Final.jar:]

如果我将以下代码从代码中删除:

If I take this lines off the code:

CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().add("*");
        corsFilter.setAllowedHeaders("Content-Type");
        singletons.add(corsFilter);

在评论了这些行之后,我得到了curl的以下内容

After commenting those lines I get the following with curl

$ curl -i http://localhost:8080/unika/usuarios -H "Origin:otherdomain.com"
HTTP/1.1 200 OK
Connection: keep-alive
Cache-Control: no-cache
X-Powered-By: Undertow 1
Server: Wildfly 8
Transfer-Encoding: chunked
Content-Type: application/json
Date: Tue, 16 Jun 2015 16:21:09 GMT

["Bill Burke","Stian Thorgersen","Stan Silvert","Gabriel Cardoso","Viliam Rockai","Marek Posolda","Bol"]

但是我需要Access-Control-Allow-Origin

But I need Access-Control-Allow-Origin

您必须手动更新其他服务的类,例如

You have to manually update the classes of your rest services like

classes.add(MyRestService.class);

和这一行

corsFilter.setAllowedHeaders("Content-Type");

给我带来了access-Control-Allow-Headers的麻烦,所以我采用了这一行,一切正常.

gave me trouble access-Control-Allow-Headers, so I took that line an everything worked.

推荐答案

可以在没有web.xml的情况下配置JAX-RS应用程序,而在@ApplicationPath的情况下使用空白的Application子类进行配置.使用此类,足以引导您的JAX-RS应用程序,并且JAX-RS运行时将扫描类路径以查找所有带@Path@Provider注释的类,并自动在应用程序中注册这些类. /p>

A JAX-RS application can be configured with no web.xml and an empty Application subclass with @ApplicationPath. With this class, this is enough for your JAX-RS application to be bootstrapped, and the JAX-RS runtime will scan the classpath for all classes annotated with @Path and @Provider, and automatically register those classes with the application.

@ApplicationPath("/api")
public class JaxRsApplication extends Application {}

一旦覆盖了getSingeletons()getClasses()并在其中一个中返回非空集,就可以通过类路径扫描禁用类的自动注册.既然这样做,您的资源将不再自动注册.因此,您现在可以做几件事:

Once you override the getSingeletons() or getClasses() and return a non-empty set in either of them, you disable the automatic registration of classes through classpath scanning. Since you have done so, your resources are no longer automatically registered. So now you can do a couple things:

  1. 只需注册自动注册之前的资源类

  1. Just register the resources class(es) that was before automatically registered

classes.add(MyResource.class);

  • 您可以查看此答案,该答案使用了

  • You can have a look at this answer, which uses a Feature annotated with @Provider. The Feature will get registered because of the classpath scanning. In this feature is where you can register the CorsFilter with the FeatureContext.

    这篇关于在resteasy中添加corsfilter后无法访问服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆