zoukankan      html  css  js  c++  java
  • Spring Boot 配置跨域

     用webstorm打开前端页面,报跨域了

     方式1:写个配置类

    package com.blogspring.config;
    
    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;
    
    /**
     * @Author: qiuj
     * @Description: 处理跨域
     * @Date: 2019/12/1 0001 9:38
     */
    @Configuration
    public class CORSConfig {
    
        @Bean
        public CorsFilter corsFilter(){
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            //  设置放行的地址
            corsConfiguration.addAllowedOrigin("http://localhost:63343");
            //  设置是否发送cookie 的信息
            corsConfiguration.setAllowCredentials(true);
            //  允许的请求头报文
            corsConfiguration.addAllowedHeader("*");
            //  允许的请求方法
            corsConfiguration.addAllowedMethod("*");
            //  设置映射路径
            UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
            //  请求进来适用于所有的路由
            urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
            return new CorsFilter(urlBasedCorsConfigurationSource);
        }
    
    }
    

     方式2:在controller 类上或者方法上配置  @CrossOrigin

    允许在特定处理程序类*和/或处理程序方法上进行跨域请求的注释。如果配置了适当的{@code HandlerMapping} *,则进行处理。

    package com.blogspring.controller;
    
    import com.baomidou.mybatisplus.extension.api.R;
    import org.springframework.beans.factory.annotation.Autowired;
    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;
    import com.blogspring.service.ICarouselService;
    
    /**
     * @Author: qiuj
     * @Description:
     * @Date: 2019/12/1 0001 9:05
     */
    @RestController
    @RequestMapping("/index")
    public class IndexController {
    
        @Autowired
        private ICarouselService iCarouselService;
        @CrossOrigin
        @GetMapping("/carousel")
        public R getCarousel(){
            return iCarouselService.getCarousel();
        }
    }
    

  • 相关阅读:
    lambda 是个啥玩意
    python中读写操作plist
    通过os中的os.path.basename获取路径中的文件名
    python遍历目录的两种方法
    mac下已有pyhon2.7,装了python3 之后,怎么调用python3啊
    Python: easy_install & pip 下载PyPi 公共资源库的工具
    安装python的图形处理库: pillow
    minSdkVersion, targetSdkVersion, targetApiLevel,compileSdkVersion,buildToolsVersion
    bat 批量修改文件名字
    a b两向量叉乘 <0说明a在b左边
  • 原文地址:https://www.cnblogs.com/blogspring/p/14191765.html
Copyright © 2011-2022 走看看