springboot跨域配置

  关于什么是跨域的问题,感兴趣的同学可以看阮一峰老师的日志 http://www.ruanyifeng.com/blog/2016/04/cors.html。

  下面贴出我在springboot项目中的跨域配置。

  1、CorsConfig

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

  2、CorsQusetionApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class CorsQusetionApplication extends SpringBootServletInitializer{

	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CorsQusetionApplication.class);
    }
	public static void main(String[] args) {
		SpringApplication.run(CorsQusetionApplication.class, args);
	}

	@GetMapping("/get")
	public Object getMethod() {
		return "Stirng";
	}

}

  3、测试网页

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<script src="jquery-1.7.2.min.js"></script>
	<script type="text/javascript">
		function crosRequest(){
			$.ajax({
				url:‘http://localhost:8080/get‘,
				type:‘get‘,
				dataType:‘text‘,
				success:function(data){
					alert(data);
				}
			});
		}
	</script>
</head>
<body>
	<button onclick="crosRequest()">请求跨域资源</button>
</body>
</html>

  pom文件添加上web启动包即可,启动项目,将测试网页test.html在电脑中任意位置放置,用浏览器打开,可以正常响应

  注释掉cors配置,重新启动项目,将测试网页test.html在电脑中任意位置放置,用浏览器打开,打开开发工具,发现控制台报错

原文地址:https://www.cnblogs.com/hhhshct/p/9249964.html

时间: 2024-08-27 21:48:54

springboot跨域配置的相关文章

跨域配置

SpringBoot跨域配置 我们的后端使用Spring Boot.Spring Boot跨域非常简单,只需书写以下代码即可. @Configuration public class CustomCORSConfiguration { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowe

asp.net Core 跨域配置

1.添加中间件 在ConfigureServices中添加 //跨域中间件服务 services.AddCors(); 在 Configure中添加 //跨域配置 app.UseCors(builder => builder.WithOrigins("http://example.com").AllowAnyHeader()); 原文地址:https://www.cnblogs.com/xuqp/p/9996687.html

Asp.net跨域配置

<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name=&quo

springboot跨域请求

首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 SpringBoot | 番外:使用小技巧合集 2018/09/17 | 分类: 基础技术 | 0 条评论 | 标签: spring boot 分享到: 原文出处: oKong 前言 最近工作比较忙,事情也比较多.加班回到家都十点多了,洗个澡就想睡觉了.所以为了不断更太多天,偷懒写个小技巧合集吧.之后有时间都会进行文章更新的.原创不易,码字不易,还希望大家多多支持!话不多说,开始今天的技巧合集吧~ 设置网站图标 原

SpringBoot跨域小结

前言:公司的SpringBoot项目出于某种原因,经常样处理一些跨域请求. 一.以前通过查阅相关资料自己写的一个处理跨域的类,如下. 1.1首先定义一个filter(拦截所有请求,包括跨域请求) public class CrossDomainFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) t

Springboot跨域 ajax jsonp请求

SpringBoot配置: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> @SuppressWarnings("deprecation") @Configuration public class CorsFilter ext

WCF ajax跨域配置

webconfig必须配置 binding="webHttpBinding" <service name="Hezi.MsgService.Send"> <endpoint address="" behaviorConfiguration="Hezi.MsgService.SendAspNetAjaxBehavior" binding="webHttpBinding" contract=&

springboot 跨域

1. 添加关于CORS的端点配置,默认情况下是禁用的,通过以下配置打开 endpoints.cors.allowed-origins=http://www.xxx.com endpoints.cors.allowed-methods=GET,POST,PUT,DELETE 2. 添加@CrossOrigin注解来实现跨域 3. 方法配置 import org.springframework.context.annotation.Configuration; import org.springfr

Spring boot2.0 与 2.0以前版本 跨域配置的区别

一·简介 spring boot升级到2.0后发现继承WebMvcConfigurerAdapter实现跨域过时了,那我们就紧随潮流. 二·全局配置 2.0以前 支持跨域请求代码: 1 import org.springframework.context.annotation.Configuration; 2 import org.springframework.web.servlet.config.annotation.CorsRegistry; 3 import org.springfram