zoukankan      html  css  js  c++  java
  • 【笔记】总结Springboot和Vue前后端分离的跨域问题

    跨域一直是个很玄学的问题,SSM的时候又得前后端一起配置,sb的时候又不用。

    • 前端
      axios普通get请求
    submitForm() {
        var v=this;
        this.$axios({
            method: 'get',
            url: api.base_url+'/user/login',
        }).then(function(res){
            var json = res.data;
            console.log(json);
            v.$store.commit('ADD_COUNT', json.data.token);
            v.$message('登录成功');
        }).catch(function(err){
            v.$message('密码或用户名错误');
        })
    }
    
    • 后端
      加一个配置类
    package com.zxc.ticketsys.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
    import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    import java.util.concurrent.Executors;
    
    /**
     * 跨域请求支持
     */
    @Configuration
    public class WebConfiguration extends WebMvcConfigurationSupport {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowCredentials(true)
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .allowedOrigins("*");
        }
    
        @Override
        protected void configureAsyncSupport(AsyncSupportConfigurer configurer){
            configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
            configurer.setDefaultTimeout(30000);
        }
    }
    
  • 相关阅读:
    nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现
    常见的qq在线客服代码
    MongoDB数据库介绍及安装(一)
    Python 创建类
    Python backup脚本
    Python 类的初始化小案例
    Python 类实例化
    Python 类初始化__init__
    ObjC(ObjectiveC): NSString应该用initWithFormat? 还是 stringWithFormat?
    NSUserDefaults
  • 原文地址:https://www.cnblogs.com/zxcoder/p/11961069.html
Copyright © 2011-2022 走看看