JAX-RX - 被 CORS 策略阻止:Access-Control-Allow-Methods 在预检响应中不允许方法 PATCH
JAX-RX - Blocked by CORS policy: Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response
我正在使用 Spring 引导 Jersey 2.1
和 Ionic 开发应用程序,我已经尝试了我发现的每一个 post 但是 none 为我解决了这个问题,我得到的错误,包括那些关于创建的错误@PATCH
接口注释自己。
所以,问题是我在执行 PATCH
请求时在浏览器上进行测试时在客户端收到此错误:
Access to XMLHttpRequest at '' from origin 'http://localhost:8100' has
been blocked by CORS policy: Method PATCH is not allowed by
Access-Control-Allow-Methods in preflight response.
我用于响应的过滤器如下,如果我将 PATCH
作为允许的方法并且它在 Postman 上完美运行,我不明白为什么我会得到这个:
过滤器:
@Provider
public final class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Accept-Patch", "*");
headers.putSingle("Access-Control-Allow-Origin", "*");
headers.putSingle("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH");
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Max-Age", "3600");
headers.putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
}
}
使用PATCH
的方法:
import javax.ws.rs.PATCH;
@PATCH
@Path("/{username}")
@Produces(MediaType.APPLICATION_JSON)
public Response actualizarPassword(@Valid @NotNull(message = "user must not be null") final UserDTO userDTO,
@PathParam("username") final String username,
@QueryParam("codigo") @NotNull(message = "codigo musnt be null") final String codigo) {
userDTO.setUsername(username);
this.usersService.actualizarPassword(this.userDTOMapper.map(userDTO), codigo);
return Response.noContent().build();
}
正如我所说,我也尝试用 PATCH
创建一个注释,正如我在一些答案中读到的那样,但是这个 http 方法应该包含在 Jersey 2.1 中,它确实是在上一段代码,我创建的界面是:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
public @interface PATCH {
}
如果我发出 POSTMAN 请求,这些是 headers 我得到:
Accept-Patch →*
Access-Control-Allow-Origin →*
Access-Control-Allow-Methods →POST, PUT, GET, OPTIONS, DELETE, PATCH
Access-Control-Allow-Credentials →true
Access-Control-Max-Age →3600
Access-Control-Allow-Headers →Content-Type, Accept, Authorization
X-Content-Type-Options →nosniff
X-XSS-Protection →1; mode=block
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Pragma →no-cache
Expires →0
X-Frame-Options →DENY
Content-Type →application/json
Content-Length →54
Date →Wed, 07 Nov 2018 07:46:45 GMT
如果它有某种关联,这是我的Spring安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
}
@Override
public void configure(WebSecurity web ) throws Exception
{
web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
}
}
更新 1
我打印了响应中的代码,我得到了 200
。但是,我一直收到此错误。
更新 2
应 sideshowbarker 的要求,我向 POSTMAN 发出了 OPTIONS
请求,这是 headers:
Allow →HEAD,GET,OPTIONS,PUT,PATCH
Last-modified →Wed, 07 Nov 2018 10:07:57 GMT+01:00
Accept-Patch →*
Access-Control-Allow-Origin →*
Access-Control-Allow-Methods →POST, PUT, GET, OPTIONS, DELETE, PATCH
Access-Control-Allow-Credentials →true
Access-Control-Max-Age →3600
Access-Control-Allow-Headers →Content-Type, Accept, Authorization
Content-Type →application/vnd.sun.wadl+xml
Content-Length →1165
Date →Wed, 07 Nov 2018 09:07:57 GMT
更新 3
我按照建议在开发工具中检查了 headers,但 PATCH 方法不存在。为什么是这样?如果我使用 POSTMAN 而不是我的 Ionic 应用程序,为什么它在那里?
请帮忙,我已经为此苦苦挣扎了好几天...
谢谢
好吧我傻到我自己都不敢相信。我正在使用 [这个插件 Chrome] 1
正在修改响应中的 headers。感谢@sideshowbarker 注意到了它。这是一件多么愚蠢的事情,但我敢打赌它也可能发生在其他人身上。
非常感谢@sideshowbarker。
我也有同样的问题。我为 CORS 启用了一个 chrome CORS 插件,它似乎在发出请求之前修改了 header。禁用它只是工作,这样的错误 :)
我正在使用 Spring 引导 Jersey 2.1
和 Ionic 开发应用程序,我已经尝试了我发现的每一个 post 但是 none 为我解决了这个问题,我得到的错误,包括那些关于创建的错误@PATCH
接口注释自己。
所以,问题是我在执行 PATCH
请求时在浏览器上进行测试时在客户端收到此错误:
Access to XMLHttpRequest at '' from origin 'http://localhost:8100' has been blocked by CORS policy: Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.
我用于响应的过滤器如下,如果我将 PATCH
作为允许的方法并且它在 Postman 上完美运行,我不明白为什么我会得到这个:
过滤器:
@Provider
public final class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Accept-Patch", "*");
headers.putSingle("Access-Control-Allow-Origin", "*");
headers.putSingle("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH");
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Max-Age", "3600");
headers.putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
}
}
使用PATCH
的方法:
import javax.ws.rs.PATCH;
@PATCH
@Path("/{username}")
@Produces(MediaType.APPLICATION_JSON)
public Response actualizarPassword(@Valid @NotNull(message = "user must not be null") final UserDTO userDTO,
@PathParam("username") final String username,
@QueryParam("codigo") @NotNull(message = "codigo musnt be null") final String codigo) {
userDTO.setUsername(username);
this.usersService.actualizarPassword(this.userDTOMapper.map(userDTO), codigo);
return Response.noContent().build();
}
正如我所说,我也尝试用 PATCH
创建一个注释,正如我在一些答案中读到的那样,但是这个 http 方法应该包含在 Jersey 2.1 中,它确实是在上一段代码,我创建的界面是:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
public @interface PATCH {
}
如果我发出 POSTMAN 请求,这些是 headers 我得到:
Accept-Patch →*
Access-Control-Allow-Origin →*
Access-Control-Allow-Methods →POST, PUT, GET, OPTIONS, DELETE, PATCH
Access-Control-Allow-Credentials →true
Access-Control-Max-Age →3600
Access-Control-Allow-Headers →Content-Type, Accept, Authorization
X-Content-Type-Options →nosniff
X-XSS-Protection →1; mode=block
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Pragma →no-cache
Expires →0
X-Frame-Options →DENY
Content-Type →application/json
Content-Length →54
Date →Wed, 07 Nov 2018 07:46:45 GMT
如果它有某种关联,这是我的Spring安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
}
@Override
public void configure(WebSecurity web ) throws Exception
{
web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
}
}
更新 1
我打印了响应中的代码,我得到了 200
。但是,我一直收到此错误。
更新 2
应 sideshowbarker 的要求,我向 POSTMAN 发出了 OPTIONS
请求,这是 headers:
Allow →HEAD,GET,OPTIONS,PUT,PATCH
Last-modified →Wed, 07 Nov 2018 10:07:57 GMT+01:00
Accept-Patch →*
Access-Control-Allow-Origin →*
Access-Control-Allow-Methods →POST, PUT, GET, OPTIONS, DELETE, PATCH
Access-Control-Allow-Credentials →true
Access-Control-Max-Age →3600
Access-Control-Allow-Headers →Content-Type, Accept, Authorization
Content-Type →application/vnd.sun.wadl+xml
Content-Length →1165
Date →Wed, 07 Nov 2018 09:07:57 GMT
更新 3
我按照建议在开发工具中检查了 headers,但 PATCH 方法不存在。为什么是这样?如果我使用 POSTMAN 而不是我的 Ionic 应用程序,为什么它在那里?
请帮忙,我已经为此苦苦挣扎了好几天...
谢谢
好吧我傻到我自己都不敢相信。我正在使用 [这个插件 Chrome] 1
正在修改响应中的 headers。感谢@sideshowbarker 注意到了它。这是一件多么愚蠢的事情,但我敢打赌它也可能发生在其他人身上。
非常感谢@sideshowbarker。
我也有同样的问题。我为 CORS 启用了一个 chrome CORS 插件,它似乎在发出请求之前修改了 header。禁用它只是工作,这样的错误 :)