春季启动和反应:X上XMLHTTPREQUEST的访问已被CORS阻止

发布于 2025-02-09 03:59:23 字数 1801 浏览 1 评论 0原文

每当我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-02-16 03:59:23

尝试更改订单,类似的内容:

httpSecurity.authorizeRequests()
            .antMatchers(AUTH_WHITELIST).authenticated()
            .and()
            .authorizeRequests().anyRequest().permitAll();

“授权”书面的顺序很重要!

try changing the order, something like this:

httpSecurity.authorizeRequests()
            .antMatchers(AUTH_WHITELIST).authenticated()
            .and()
            .authorizeRequests().anyRequest().permitAll();

the order in which the "authorize" is written matters!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文