springboot vue线上部署项目注意跨域问题

springboot vue线上部署项目注意跨域问题

nginx配置

  server {
   
       listen 8080;
       server_name localhost;
       charset utf-8;
      location / {
   
        root /home/user/cf/vue/dist;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
 }
    error_page 405 = 200@405;
    location @405 {
   
        proxy_method GET;
        proxy_pass http://localhost:9090;
    }
}

vue中request.js

// 创建可一个新的axios对象
const request = axios.create({
   
    baseURL: 'http://xxx.xxx.xxx.xxx:9090',   // 后端的接口地址  ip:port
    timeout: 30000                          // 30s请求超时
})

vue中vue.config.js

const {
    defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
   
  transpileDependencies: true,
  devServer: {
   
    port: 8080
  },
  chainWebpack: config =>{
   
    config.plugin('html')
        .tap(args => {
   
          args[0].title = "管理系统";
          return args;
        })
  }
})

Springboot中config目录中 CorsConfig类

/**
 * 跨域配置
 */
@Configuration
public class CorsConfig {
   

    @Bean
    public CorsFilter corsFilter() {
   
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

Springboot中config目录中 WebConfig类

@Configuration
public class WebConfig implements  WebMvcConfigurer {
   

    @Resource
    private JwtInterceptor jwtInterceptor;

    // 加自定义拦截器JwtInterceptor,设置拦截规则
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
   
//        放行请求
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/")
                .excludePathPatterns("/login")
                .excludePathPatterns("/register")
                .excludePathPatterns("/files/**");
    }
}

重启nginx 命令

sudo nginx -s reload

相关推荐

  1. springboot vue线部署项目注意问题

    2024-01-21 06:46:10       49 阅读
  2. springbootv 2.4.0

    2024-01-21 06:46:10       55 阅读
  3. vue问题,请注意你的项目是vue2还是vue3

    2024-01-21 06:46:10       30 阅读
  4. @CrossOrigin注解解决问题

    2024-01-21 06:46:10       40 阅读
  5. 项目记录:问题解决方案

    2024-01-21 06:46:10       59 阅读
  6. SpringBoot+Vue项目问题

    2024-01-21 06:46:10       37 阅读
  7. 问题+解决express

    2024-01-21 06:46:10       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-21 06:46:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-21 06:46:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-21 06:46:10       82 阅读
  4. Python语言-面向对象

    2024-01-21 06:46:10       91 阅读

热门阅读

  1. Mac Could not find the GDAL library 问题解决

    2024-01-21 06:46:10       52 阅读
  2. 【WPF.NET开发】以编程方式打印XPS文件

    2024-01-21 06:46:10       47 阅读
  3. 数据结构---栈(Stack)

    2024-01-21 06:46:10       63 阅读
  4. TensorFlow人工智能开源深度学习框架简单认识

    2024-01-21 06:46:10       58 阅读
  5. Webpack打包简单的js文件

    2024-01-21 06:46:10       58 阅读
  6. axios封装-reques.js

    2024-01-21 06:46:10       53 阅读
  7. Mybatis的占位符中使用的名称

    2024-01-21 06:46:10       59 阅读
  8. 聊聊PowerJob的AliOssService

    2024-01-21 06:46:10       47 阅读
  9. centos 安装rabbitmq集群

    2024-01-21 06:46:10       46 阅读