我正在使用Java EE(JPA)为后端创建RESTful应用程序,并使用Eclipse IDE和前端中的angular6。我目前正在尝试创建CORSfilter。我已经编写了以下两个类,但是当我发送POST请求时收到:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
在浏览器(Chrome)的控制台中。
CrossOriginResourceFilter.java:
package filters;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class CrossOriginResourceFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) {
response.getHeaders().putSingle("Access-Control-Allow-Origin", "*");
response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type");
response.getHeaders().putSingle("Access-Control-Allow-Headers", "responseType");
}
}
ApplicationConfig.java
package service;
import java.util.Set;
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("linkedInRestfulProject")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(filters.CrossOriginResourceFilter.class);
resources.add(service.AdvertismentEndpoint.class);
resources.add(service.ArticleEndpoint.class);
resources.add(service.ConnectionRequestEndpoint.class);
resources.add(service.UserEndpoint.class);
resources.add(service.ConversationEndpoint.class);
resources.add(service.MessageEndpoint.class);
}
}
似乎我的后端无法使用或查看过滤器。
我已经搜索过是否必须创建/更改其他任何内容(例如web.xml),但找不到任何东西。
我还尝试在web.xml中添加以下行,并且得到以下错误(编译后端时)。
<filter>
<filter-name>CrossOriginResourceFilter</filter-name>
<filter-class>filters.CrossOriginResourceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CrossOriginResourceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SEVERE: Exception starting filter [CrossOriginResourceFilter]
java.lang.ClassCastException: filters.CrossOriginResourceFilter cannot be cast to javax.servlet.Filter
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:264)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4598)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1421)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1411)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)