我在tomcat8上使用了swagger ui实例。它会加载一个.yaml文件,该文件描述了一个摇头规格。一切正常,当我尝试使用“尝试”功能时,它将向服务器发送一个http请求并给出
TypeError: Failed to fetch
回来。 在我的服务器上,我已通过
启用了CORS import java.io.IOException;
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 CORSFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
responseContext.getHeaders().add(
"Access-Control-Allow-Origin", "http://localhost:8080");
responseContext.getHeaders().add(
"Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add(
"Access-Control-Allow-Headers",
"origin, content-type, accept, authorization");
responseContext.getHeaders().add(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
}
,并且在浏览器中输入了正确的证书。通过浏览器成功发送相同的请求(通过摇晃发送),并给出正确的响应。
编辑: 刚刚发现我收到以下错误
Fetch API cannot load https://localhost:8184/user/info?username=mustermann. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
如何在Swagger / UI中启用CORS?在服务器站点上应启用它。
我想念什么吗?