Wro*_*ong 1 java patch jax-rs cors spring-boot
我正在使用带有Jersey 2.1 的Spring Boot
和 Ionic 来开发一个应用程序,我已经尝试了我找到的每一个帖子,但没有一个帖子为我解决了这个问题,我得到的错误,包括那些关于@PATCH自己创建界面注释的内容。
因此,问题是在执行PATCH请求时在浏览器上进行测试时,我在客户端收到此错误:
CORS 策略已阻止从源 ' http://localhost:8100 '访问 XMLHttpRequest :预检响应中的 Access-Control-Allow-Methods 不允许方法 PATCH。
我用于响应的过滤器如下,如果我有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");
}
}
Run Code Online (Sandbox Code Playgroud)
使用的方法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();
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,我还尝试创建一个注释,PATCH正如我在一些答案中所读到的,但是这个 http 方法应该包含在 Jersey 2.1 中,并且确实就像在上一段代码中看到的那样,我创建的接口曾是:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
public @interface PATCH {
}
Run Code Online (Sandbox Code Playgroud)
如果我执行 POSTMAN 请求,这些是我得到的标头:
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
Run Code Online (Sandbox Code Playgroud)
如果它以某种方式相关,这是我的 SpringSecurity 配置:
@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, "/**" );
}
}
Run Code Online (Sandbox Code Playgroud)
更新 1
我已经打印了响应的代码,我得到了一个200. 但是,我不断收到此错误。
更新 2
根据OPTIONSsideshowbarker的要求,我向POSTMAN提出了一个请求,这是标题:
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
Run Code Online (Sandbox Code Playgroud)
更新 3
我按照建议检查了开发工具中的标题,但没有 PATCH 方法。为什么是这样?如果我使用 POSTMAN 而不是我的 Ionic 应用程序,为什么会出现它?
请帮忙,我已经为此苦苦挣扎了好几天......
谢谢