亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CORS 阻塞了我的后端服務器,如何修復?使用 Springboot java 作為后端

CORS 阻塞了我的后端服務器,如何修復?使用 Springboot java 作為后端

臨摹微笑 2023-10-12 14:51:55
import React, { Component } from "react";import axios from 'axios';class App extends Component {  handleSubmit(event) {    axios.post('http://localhost:3050/login', {      "username": "username",      "password": "password"    })      .then(function (response) {        console.log(response);      })      .catch(function (error) {        console.log(error);      });    event.preventDefault();  }  render() {      return(        <form onSubmit={this.handleSubmit}>          <input type="submit" value="Submit" />        </form>      );  }}export default App;只需檢查后端,將用戶名 json 設置為“username”并將密碼設置為“password”我的后端是 spring boot,使用帶有“用戶名”和“密碼”的結束鏈接 /login 應該會給出一些響應。因此,這段代碼可以工作,除非 CORS 會阻止連接,因此它會永遠停留在處理狀態。我發現的一個解決方案是禁用 Chrome 中的所有安全性,并且它有效。但我正在尋找一個永久的解決方案,而不必禁用 chrome 設置的安全性。不確定我是通過 springboot 還是 React 來完成
查看完整描述

4 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

嘗試在 Spring REST 端點上使用注釋“@CrossOrigin”,位于“@RequestMapping”注釋上方。

例如:-

    @CrossOrigin
    @RequestMapping(value="/login",method=RequestMethod.POST)


查看完整回答
反對 回復 2023-10-12
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

當請求 POST 時,您可能需要額外的配置,如下所示;


axio({

  method: 'put',

  headers: {

    'Content-Type': 'application/json',

  },

  data: JSON.stringify({

    "username": "username",

    "password": "password"

  }),

});


查看完整回答
反對 回復 2023-10-12
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

請將以下文件添加到您的項目中存在 Main spring boot 類的包中。這對我來說適用于 Spring boot、React 生態系統中的所有 CORS 問題。


import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.CorsRegistry;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration

@EnableWebMvc

public class CorsWebConfig implements WebMvcConfigurer {


    @Override

    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**");

        registry.addMapping("/*.html");

    }


    @Override

    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addRedirectViewController("/api/v2/api-docs", "/v2/api-docs");

        registry.addRedirectViewController("/api/swagger-resources/configuration/ui",

                "/swagger-resources/configuration/ui");

        registry.addRedirectViewController("/api/swagger-resources/configuration/security",

                "/swagger-resources/configuration/security");

        registry.addRedirectViewController("/api/swagger-resources", "/swagger-resources");

    }


    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/swagger-ui.html**")

                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");

        registry.addResourceHandler("/api/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }}


查看完整回答
反對 回復 2023-10-12
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

在u配置中創建這個bean


 @Bean

public CorsConfigurationSource corsConfigurationSource() {


    final CorsConfiguration configuration = new CorsConfiguration();

    configuration.setAllowedOrigins(ImmutableList.of(

            "http://example.net",

            "http://example.com",

           ));

    configuration.setAllowedMethods(ImmutableList.of("HEAD", "OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH"));

    configuration.setAllowCredentials(true);

    configuration.setAllowedHeaders(ImmutableList.of("*"));

    configuration.setExposedHeaders(ImmutableList.of("Content-Disposition"));

    configuration.setMaxAge(3600L);


    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    source.registerCorsConfiguration("/**", configuration);


    return source;

}


查看完整回答
反對 回復 2023-10-12
  • 4 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號