【筆記】總結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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM