CXF 3.2.2 CORS 失败 "No 'Access-Control-Allow-Origin' header is present"
CXF 3.2.2 CORS failed with "No 'Access-Control-Allow-Origin' header is present"
我正在使用 CXF + spring 引导开发 Restful 服务。 CORS 设置有问题
我在服务定义接口 (GateRs) 上设置了 CORS
...
@Service
@CrossOriginResourceSharing(
allowAllOrigins = true,
allowHeaders = {
"Accept", "Accept-Charset", "Accept-Encoding", "Accept-Datetime",
"Accept-Language", "Authorization", "Content-Language", "Content-Length",
"Content-Type", "Origin", "User-Agent"},
exposeHeaders = {
"Accept", "Accept-Charset", "Accept-Encoding", "Accept-Datetime",
"Accept-Language", "Authorization", "Content-Language", "Content-Length",
"Content-Type", "Origin", "User-Agent"},
allowCredentials = true,
maxAge = 1209600 )
public interface GateRs {
@POST
@Path("/require")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@VoluntarySecure
public Res require(Req req);
...
使用 class
实现接口
@Component
public class Gate implements GateRs {
@Override
public Res require(Req req) {
...
将 'CrossOriginResourceSharingFilter' 附加到服务 bean
@Bean
public CrossOriginResourceSharingFilter cors() {
return new CrossOriginResourceSharingFilter();
}
@Bean(destroyMethod = "destroy") @DependsOn(Bus.DEFAULT_BUS_ID)
public Server jaxRsServer() {
final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setServiceBean(new Gate());
factory.setProviders(Arrays.asList(jackson(), cors()));
factory.setBus(springBus());
factory.setAddress("/Gate");
return factory.create();
}
...
客户端是基于 Ionic 的移动应用程序,由 Chrome 浏览器模拟。
“/Gate/require”返回“404”,因为 CORS 失败,没有“
Access-Control-Allow-Origin" header.
我查看了细节;并发现预检('OPTIONS')实际上成功了; "POST" 失败,触发了上述消息
预检(选项)
POST失败;服务器端似乎没有在响应消息中显示 'Access-Control-Allow-Origin' header
有没有人有什么想法或建议?
谢谢
我终于通过如下更改“@CrossOriginResourceSharing”解决了问题:
...
@CrossOriginResourceSharing(
allowAllOrigins = true,
allowCredentials = true,
maxAge = 1209600 )
public interface GateRs {
...
我正在使用 CXF + spring 引导开发 Restful 服务。 CORS 设置有问题
我在服务定义接口 (GateRs) 上设置了 CORS
...
@Service
@CrossOriginResourceSharing(
allowAllOrigins = true,
allowHeaders = {
"Accept", "Accept-Charset", "Accept-Encoding", "Accept-Datetime",
"Accept-Language", "Authorization", "Content-Language", "Content-Length",
"Content-Type", "Origin", "User-Agent"},
exposeHeaders = {
"Accept", "Accept-Charset", "Accept-Encoding", "Accept-Datetime",
"Accept-Language", "Authorization", "Content-Language", "Content-Length",
"Content-Type", "Origin", "User-Agent"},
allowCredentials = true,
maxAge = 1209600 )
public interface GateRs {
@POST
@Path("/require")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@VoluntarySecure
public Res require(Req req);
...
使用 class
实现接口@Component
public class Gate implements GateRs {
@Override
public Res require(Req req) {
...
将 'CrossOriginResourceSharingFilter' 附加到服务 bean
@Bean
public CrossOriginResourceSharingFilter cors() {
return new CrossOriginResourceSharingFilter();
}
@Bean(destroyMethod = "destroy") @DependsOn(Bus.DEFAULT_BUS_ID)
public Server jaxRsServer() {
final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setServiceBean(new Gate());
factory.setProviders(Arrays.asList(jackson(), cors()));
factory.setBus(springBus());
factory.setAddress("/Gate");
return factory.create();
}
...
客户端是基于 Ionic 的移动应用程序,由 Chrome 浏览器模拟。
“/Gate/require”返回“404”,因为 CORS 失败,没有“ Access-Control-Allow-Origin" header.
我查看了细节;并发现预检('OPTIONS')实际上成功了; "POST" 失败,触发了上述消息
预检(选项)
POST失败;服务器端似乎没有在响应消息中显示 'Access-Control-Allow-Origin' header
有没有人有什么想法或建议?
谢谢
我终于通过如下更改“@CrossOriginResourceSharing”解决了问题:
...
@CrossOriginResourceSharing(
allowAllOrigins = true,
allowCredentials = true,
maxAge = 1209600 )
public interface GateRs {
...