春季靴子CORS块删除请求
Whenever I try to send request to delete endpoint with axios, I get the following error:
Access to XMLHttpRequest at 'http://localhost:8080/api/payment_card/delete/1234123412343433' from 原始'http:// localhost:3000'被CORS策略阻止:响应前飞行请求 不通过访问控制检查:没有“访问控制”标头 请求的资源
Axios请求如下:
.delete(
"http://localhost:8080/api/payment_card/delete/" + selectedCardId ,
{
headers: {
Authorization: `Bearer ${token}`,
"Access-Control-Allow-Origin": "**"
},
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});```
My java WebSecurityConfig stays as follow:
Override protected void configure(HttpSecurity http) throws Exception {
http = http.cors().and().csrf().disable();
http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
// Set session management to stateless
http = http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and();
// Set unauthorized requests exception handler
http = http
.exceptionHandling()
.authenticationEntryPoint(new AuthException())
.and();
http.addFilterBefore(requestFilter, UsernamePasswordAuthenticationFilter.class);
}
在控制器中,映射是:
public ResponseEntity<PaymentCard> deletePaymentCard(@PathVariable Long cardNumber) {
PaymentCard pCard = paymentCardService.deletePaymentCard(cardNumber);
return new ResponseEntity<>(pCard, HttpStatus.OK);
}
我尝试了许多解决方案,例如添加@Crossorigin注释,制作CorsFilter,但似乎没有任何帮助。最终,我更改了删除以在控制器中获取映射,但是 我觉得HTTP政策可以随时吸引我拘留:( 感谢您的时间并提前帮助。
Whenever I try to send request to delete endpoint with axios, I get the following error:
Access to XMLHttpRequest at 'http://localhost:8080/api/payment_card/delete/1234123412343433' from
origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request
doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the
requested resource
Axios request is built as following :
.delete(
"http://localhost:8080/api/payment_card/delete/" + selectedCardId ,
{
headers: {
Authorization: `Bearer ${token}`,
"Access-Control-Allow-Origin": "**"
},
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});```
My java WebSecurityConfig stays as follow:
Override protected void configure(HttpSecurity http) throws Exception {
http = http.cors().and().csrf().disable();
http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
// Set session management to stateless
http = http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and();
// Set unauthorized requests exception handler
http = http
.exceptionHandling()
.authenticationEntryPoint(new AuthException())
.and();
http.addFilterBefore(requestFilter, UsernamePasswordAuthenticationFilter.class);
}
And in the controller, mapping is :
public ResponseEntity<PaymentCard> deletePaymentCard(@PathVariable Long cardNumber) {
PaymentCard pCard = paymentCardService.deletePaymentCard(cardNumber);
return new ResponseEntity<>(pCard, HttpStatus.OK);
}
I tried many solutions like adding @CrossOrigin annotation, making CorsFilter but nothing seems to help at all. Ultimately, I've changed DeleteMapping to GetMapping in my controller but
I feel like http policy can catch me to custody at any time :(
Thanks for your time and help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
corsconfiguration.applypermitdefaultValues()
允许不像人们那样假设的所有方法,而只允许使用:遵循方法:get,head,post。要允许删除方法,您可以使用以下代码:
如果我们将
corsconfiguration
明确地配置,我建议不要使用applypermitdefaultValues()
,但要明确指定所有所需的方法。那么,没有人需要记住applypermitdefaultValues()
的准确启用哪些方法,并且此类代码将更易于理解。CorsConfiguration.applyPermitDefaultValues()
allows not all methods as one may assume, but following methods only: GET, HEAD, POST.To allow DELETE method, you can use following code:
If we configure
CorsConfiguration
explicitly, I recommend not to useapplyPermitDefaultValues()
, but specify all desired methods explicitly. Then nobody will need to remember what methods exactly are enabled byapplyPermitDefaultValues()
, and such code will be easier to understand.我可以使用以下方法实现过滤器的类
I could have a class that implements Filter with the following methods