iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >教你用Java SpringBoot如何解决跨域
  • 452
分享到

教你用Java SpringBoot如何解决跨域

2024-04-02 19:04:59 452人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录跨域什么是跨域CORSSpringBoot解决跨域方案1.使用@CrossOrigin注解2.spring框架全局配置CORS配置具体实现1.使用@CrossOrigin注解1.

跨域

什么是跨域

请求url的协议,域名,端口三者之间任意一个与当前页面url不同的即为跨域。

在这里插入图片描述

CORS

CORS(Cross-origin resource sharing-跨源资源共享)允许网页从其他域向浏览器请求额外的资源

SpringBoot解决跨域方案

1.使用@CrossOrigin注解

该注解添加在你想要让某接口允许跨域的的,类上面,或者实现方法上面。

该注解包含的属性:orgins,allowdHeaders,methods,exposedHeaders,allowCreden,maxAge。

在这里插入图片描述

2.Spring框架全局配置CORS配置

2.1Spring MVC CORS 使用WebMvcConfigurerAdapter配置!

2.2Spring Boot CORS 使用WebMvcConfigurer配置!

2.3CORS 使用Spring Security配置!

具体实现

1.使用@CrossOrigin注解

1.1目录结构

在这里插入图片描述

DemoApplication.java


package com.example.crossdomain.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

CorsTestController.java


package com.example.crossdomain.demo.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/demo")
@RestController
@CrossOrigin("https://blog.csdn.net") // 只有指定域名可以访问该类下所有接口
public class CorsTestController {
    @GetMapping("/sayHello")
    public String sayHello(){
        return "Hello world";
    }
}

1.2运行结果

在这里插入图片描述

在这里插入图片描述

2.使用@CrossOrigin注解

2.1Spring MVC CORS 使用WebMvcConfigurerAdapter配置!

2.1.1目录结构

在这里插入图片描述

2.2.2添加CorsConfiguration.java


package com.example.crossdomain.demo.config;
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.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class CorsConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "POST");
    }
}

2.2.3运行结果

在这里插入图片描述

在这里插入图片描述

2.3Spring Boot CORS 使用WebMvcConfigurer配置


package com.example.crossdomain.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
public class CorsConfiguration
{
    @Bean
    public WebMvcConfigurer corsConfigurer()
    {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
            }
        };
    }
}

2.4CORS 使用Spring Security配置

2.4.1目录结构

在这里插入图片描述

2.4.2添加WebSecurityConfig.java


package com.example.crossdomain.demo.config;

import org.springframework.context.annotation.Bean;
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;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();
    }
    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");	//同源配置,*表示任何请求都视为同源,若需指定ip和端口可以改为如“localhost:8080”,多个以“,”分隔;
        corsConfiguration.addAllowedHeader("*");//header,允许哪些header,本案中使用的是token,此处可将*替换为token;
        corsConfiguration.addAllowedMethod("*");	//允许的请求方法,PSOT、GET等
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return source;
    }
}

2.4.3运行结果

在这里插入图片描述

在这里插入图片描述

代码获取

码云

参考链接

什么是跨域?跨域解决方法

Spring boot 入门之CORS 跨域配置详解

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: 教你用Java SpringBoot如何解决跨域

本文链接: https://www.lsjlt.com/news/135487.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 教你用Java SpringBoot如何解决跨域
    目录跨域什么是跨域CORSSpringBoot解决跨域方案1.使用@CrossOrigin注解2.Spring框架全局配置CORS配置具体实现1.使用@CrossOrigin注解1....
    99+
    2024-04-02
  • SpringBoot解决跨域的方法详细教程
    Spring Boot提供了多种解决跨域问题的方法,以下是其中几种常用的方法: 使用@CrossOrigin注解是一种简单且快速的解决跨域问题的方法。在Spring Boot的Controller类或...
    99+
    2023-10-08
    spring boot java spring
  • 使用Java如何解决跨域问题
    本篇内容主要讲解“使用Java如何解决跨域问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“使用Java如何解决跨域问题”吧!跨域问题现在绝大多数公司的项目都是...
    99+
    2024-04-02
  • springboot 如何解决cross跨域请求的问题
    目录springboot 解决cross跨域请求1.使用ajax发送跨域请求接口时2.在被跨域请求的一方配置3.再次测试4.对于只有个别需要开放跨域请求的接口可以这样玩5.其它解决跨...
    99+
    2024-04-02
  • SpringBoot+Spring Security无法实现跨域如何解决
    本篇内容主要讲解“SpringBoot+Spring Security无法实现跨域如何解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot+Spring Security无法实...
    99+
    2023-07-06
  • SpringBoot中到底该如何解决跨域问题
    目录前言1、跨域访问报错2、同源定义3、跨域问题如何解决?4、CORS原理5、SpringMVC中如何解决跨域问题?6、方案1:方法或者类上标注@CrossOrigin注解7、方案2...
    99+
    2024-04-02
  • java中的跨域请求如何解决
    本篇文章为大家展示了java中的跨域请求如何解决 ,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。java 请求跨域问题解决方法实例详解新建Util类,在Util中添加下面方法: public s...
    99+
    2023-05-31
    java 跨域请求 ava
  • vue如何解决跨域
    这篇文章给大家分享的是有关vue如何解决跨域的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代...
    99+
    2024-04-02
  • 解决Vue+SpringBoot+Shiro跨域问题
    目录一、配置Vue前端1、开发跨域配置2、生产跨域配置二、配置spring boot相信大家刚开始做都会遇到这个问题,在网上找了好多也不管用,都写的不全, 在这里记录一下,希望对大家...
    99+
    2024-04-02
  • 怎么解决SpringBoot跨域问题
    这篇文章给大家分享的是有关怎么解决SpringBoot跨域问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。允许全部请求跨域许可的代码需要继承WebMvcConfigurerAdapter类。@Configura...
    99+
    2023-06-28
  • springboot怎么解决跨域问题
    在Spring Boot中解决跨域问题可以通过以下几种方式: 使用注解@EnableWebMvc和@CrossOrigin:在S...
    99+
    2023-10-25
    springboot
  • java重定向跨域问题如何解决
    在Java中,可以通过设置响应头来解决重定向跨域问题。在重定向时,可以设置Access-Control-Allow-Origin头,...
    99+
    2023-09-06
    java
  • Nodejs如何解决跨域(CORS)
    目录Nodejs解决跨域(CORS)手动配置CORS模块axiosNodejs CORS跨域问题总结Nodejs解决跨域(CORS) 前后端分离的大环境下,受制于同源策略,我们需要懂...
    99+
    2023-01-17
    Nodejs跨域 Nodejs解决跨域 Nodejs CORS
  • 解决SpringBoot跨域的三种方式
    目录一、什么是跨域1.1、为什么会出现跨域问题1.2、什么是跨域1.3、非同源限制1.4、如何解决跨域问题二、SpringBoot解决跨域问题2.1、配置CorsFilter(全局跨...
    99+
    2024-04-02
  • Apache如何解决跨域问题
    方案一:直接在域名配置中允许跨域 缺点:安全性缺失。公交车,谁都能访问。相当于完全放弃跨域控制。 且无法发送登陆凭证,发送cookie等依然会被拦截 修改apache/conf/httpd.conf 文件 找到   #LoadModule ...
    99+
    2023-09-01
    apache php 跨域
  • PHP跨域问题如何解决
    这篇文章主要讲解了“PHP跨域问题如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHP跨域问题如何解决”吧!设置允许访问的域名:允许全部的域名访问header("Acces...
    99+
    2023-07-05
  • Vue3跨域问题如何解决
    这篇文章主要介绍了Vue3跨域问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue3跨域问题如何解决文章都会有所收获,下面我们一起来看看吧。vue项目配置代理vue.config.jsconst&n...
    99+
    2023-07-05
  • SpringBoot+Spring Security无法实现跨域的问题如何解决
    本篇内容主要讲解“SpringBoot+Spring Security无法实现跨域的问题如何解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot+Spring Security...
    99+
    2023-06-20
  • ajax如何解决跨域问题
    小编给大家分享一下ajax如何解决跨域问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!跨域同源策略限制同源策略阻止从一个域上加...
    99+
    2024-04-02
  • springboot解决跨域的方式有哪些
    这篇文章主要介绍“springboot解决跨域的方式有哪些”,在日常操作中,相信很多人在springboot解决跨域的方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”springboot解决跨域的方式...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作