即使在 Spring Boot Server 中使用 CORS 配置,CORS 也存在跨域问题 [已关闭]

CORS cross-origin problem even with CORS configuration in Spring Boot Server [closed]

提问人:Wolf Ganger 提问时间:11/17/2023 最后编辑:Wolf Ganger 更新时间:11/18/2023 访问量:32

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

3天前关闭。

这篇文章昨天经过编辑并提交审核。

我的Spring Boot服务器中已经有CORS配置类:

@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization")
                .maxAge(3600); // Max age of the preflight request in seconds
    }
}

问题是,即使使用该类,AXIOS 仍然会失败,并显示以下错误:

错误信息

我还缺少什么?在我的后端有其他 POST 请求时,我没有任何问题

我还尝试在控制器本身中使用@CrossOrigin(origins=“*”),但结果是一样的

@RestController
@RequestMapping("/classification/")
@CrossOrigin(origins = "*")
public class ClassificationController {

这是进行查询的类:

export default function NewClassification() {
  
  const [classification, setClassification] = useState([]);

  async function addClassification(URL, classification) {
    const createdClassification = await postRequest(URL,classification);
    setClassification(createdClassification)
  }

  console.log(useSearchParams().get('name'))
  const videogameName= useSearchParams().get('name')

  return (
    <Layout>
      <div> 
        <form>
          <input type='text' id='entity' placeholder='Write entity name'/>
          <input type='text' id='age' placeholder='Write age of the label'/>
          <input type='text' id="url" placeholder='Write URL of the homepage of the entity'/>
          <input type='text' id='country' placeholder='Write country where the entity belongs'/>
          <input type='button' value="Add new classification" onClick={e => addClassification("/classification/"+videogameName,{system:document.getElementById("entity").value,
          country:document.getElementById("country").value,tag:document.getElementById("age").value,url:document.getElementById("url").value})}/>
        </form>
      </div>
    </Layout>
  )
}

这是处理请求并将其发送到后端的类:

import axios from "axios";

const axiosClient = axios.create();

axiosClient.defaults.baseURL = "http://<ip>:8080"
axios.defaults.headers.post['Content-Type'] = 'application/json';

export async function postRequest(URL, payload){
    const response =  await axiosClient.post(URL, payload, {
        headers: {crossDomain: true},
        validateStatus: function (status) {
        return true;
        }})
    //.then(response => response)
    //.catch(err => console.log(err))
    console.log(response)

    return response.data
}

javascript java spring-boot next.js axios

评论

0赞 Pointy 11/17/2023
好吧,错误消息似乎很有解释性。您的传出请求标头到底是什么样子的?
0赞 James 11/17/2023
该错误指出不允许标头“crossorigin”。事实上,它不会出现在您显示的允许标头列表中。该怎么做很大程度上取决于“crossorigin”是什么,以及为什么将其添加到您的 cors 预检请求中。要么不发送,要么允许它。
1赞 Jaromanda X 11/17/2023
@James - 实际上,头球抱怨的回合是crossdomain
0赞 Jaromanda X 11/17/2023
AXIOS still fails- 并不少见 - 检查浏览器开发人员工具 - 您的请求是否发送标头?如果是这样,请找出原因 - 问题出在客户端,但您只显示了服务器代码crossdomain
0赞 Wolf Ganger 11/18/2023
解决。我不得不删除前端发布请求的标题字段headers: {crossDomain: true}

答: 暂无答案