春季启动和反应:X上XMLHTTPREQUEST的访问已被CORS阻止
每当我将React应用程序的请求发送到Spring Boot后端时,我都会遇到此错误。
在'http:// localhost:8082/customanagement%E2%80%8B/API%E2%8B/Services%8B/Services%E2%8B/8B/PROPSSER %e2%80%8b/delete'从原点'http:// localhost:3000'已被CORS策略阻止:响应前飞行请求不会传递访问控制检查:no'access-controll-allow-allow-hall-origin'header在请求的资源上存在。
我要在下面留下代码。
Spring Boot后端
SecurityConfig.java
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers(AUTH_WHITELIST)
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
prospectController.java
@RestController
@RequestMapping(MICROSERVICE_SERVICES_PROSPECT_PREFIX)
@CrossOrigin(origins = "*")
public class ProspectController {
@Autowired
private ProspectService prospectService;
@DeleteMapping("/{prospectId}/delete")
public ResponseEntity<ProspectResponse> delete(@PathVariable("prospectId") Integer prospectId) {
ProspectResponse response = prospectService.delete(prospectId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
React Code
let handleDelete = async (id) => {
console.log(id)
await axios.delete("http://localhost:8082/customermanagement/api/services/prospect/" + {id} + "/delete")
.then(() => console.log('Delete successful'));
}
您能帮我解决这个问题吗?
我认为我的春季安全配置可能会有问题。
谢谢
I'm getting this error whenever I send a request from my React App to my Spring Boot backend.
Access to XMLHttpRequest at 'http://localhost:8082/customermanagement%E2%80%8B/api%E2%80%8B/services%E2%80%8B/prospect%E2%80%8B/[object%20Object]%E2%80%8B/delete' 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.
I'm leaving the code below.
Spring Boot Backend
SecurityConfig.java
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers(AUTH_WHITELIST)
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
ProspectController.java
@RestController
@RequestMapping(MICROSERVICE_SERVICES_PROSPECT_PREFIX)
@CrossOrigin(origins = "*")
public class ProspectController {
@Autowired
private ProspectService prospectService;
@DeleteMapping("/{prospectId}/delete")
public ResponseEntity<ProspectResponse> delete(@PathVariable("prospectId") Integer prospectId) {
ProspectResponse response = prospectService.delete(prospectId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
React code
let handleDelete = async (id) => {
console.log(id)
await axios.delete("http://localhost:8082/customermanagement/api/services/prospect/" + {id} + "/delete")
.then(() => console.log('Delete successful'));
}
Could you help me with this problem?
I think that there could be a problem with my Spring Security Config.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改订单,类似的内容:
“授权”书面的顺序很重要!
try changing the order, something like this:
the order in which the "authorize" is written matters!