SpringBoot+ajax跨域请求

本文介绍了在不集成和集成SpringSecurity两种情况下,SpringBoot处理Ajax跨域请求的问题。在不集成时,由于跨域引发错误,解决方案是在Controller上添加@CrossOrigin注解。而集成SpringSecurity后,虽然GET请求正常,POST请求仍会遇到跨域问题,需要通过配置WebSecurityConfiguration关闭csrf。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不集成SpringSecurity的情况下进行跨域访问

错误信息

在使用ajax请求后端的时候在浏览器控制台会输出如下信息:

Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

从源’本地路径’访问 '目标路径(请求链接)'文本传输请求已被CORS策略阻塞:对预置请求的响应未通过访问控制检查:请求的资源上不存在’Access- control - allow - origin '报头。

错误原因

本地路径和目标路径不是同一个域名下引起的跨域问题

解决方案

在对应的Controller类前上@CrossOrigin注解

例如:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: demo
 * @description:
 * @author: liu
 * @create: 2019-07-11 18:14
 **/
@RestController
@CrossOrigin
public class TestController {
  
    @PostMapping("/testPost")
    public String testPost() {
        System.out.println("testPost成功");
        return "testPost跨域请求成功";
    }
  
    @GetMapping("/testGet")
    public String testGet() {
        System.out.println("testGet成功");
        return "testGet跨域请求成功";
    }
}

集成SpringSecurity的情况下进行跨域访问

错误信息

集成SpringSecurity后get请求正常,但是对于post请求仍然会显示错误信息

jquery.min.js:4 POST http://localhost:8080/testPost 403
list_student.html:1 Access to XMLHttpRequest at 'http://localhost:8080/testPost' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

解决方案

添加WebSecurityConfiguration配置文件可关闭csrf

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-1)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.requestMatchers().antMatchers(HttpMethod.OPTIONS, "/oauth/**","/login/**","/logout/**")
//            .and()
//            .authorizeRequests()
//            .antMatchers().permitAll()
//            .and()
//            .formLogin().permitAll(); //新增login form 支持用户登录及授权

        http.requestMatchers().antMatchers(HttpMethod.OPTIONS, "/oauth/**")
                .and()
                .cors()
                .and()
                .csrf().disable();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值