springboot 和vue前后端分离 跨域配置

全局跨域配置

package com.eaos.util.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 SMILE
 */
@Configuration
public class CorsConfig {  
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();  
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等) 
        return corsConfiguration;  
    }
  
    @Bean  
    public CorsFilter corsFilter() {  
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
        source.registerCorsConfiguration("/**", buildConfig()); // 4  //注册
        return new CorsFilter(source);  
    }  
}

配置前后端资源共享

session一致等
package com.eaos.util.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.*;

/**
 * 跨域保持session一致
 * 
 * @author ASUS
 *
 */
@Component
public class FilterConfig implements HandlerInterceptor {

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
		response.setHeader("Access-Control-Allow-Methods", "*");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Headers",
				"Authorization,token,Origin, X-Requested-With, Content-Type, Accept,Access-Token");// Origin,
																									// X-Requested-With,
																									// Content-Type,
																									// Accept,Access-Token
		response.setHeader("Access-Control-Max-Age", "3600");
		return true;
	}
}

  1. 注册
package com.eaos.util.filter;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.eaos.util.same.SameUrlDataInterceptor;

/**
 * 跨域保持session一致
 * 
 * @author ASUS
 *
 */
@SuppressWarnings("deprecation")
@SpringBootConfiguration
public class SpringMVCConfig extends WebMvcConfigurerAdapter {

	@Autowired
	private FilterConfig filterConfig;
	public void addInterceptors(InterceptorRegistry registry) {
		//跨域资源共享
		registry.addInterceptor(filterConfig).addPathPatterns("/**");
	}


}

vue前端配置

在axios配置文件里面配置
主要配置withCredentials = true; 或者withCredentials = false;自己测试

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
//跨域
axios.defaults.withCredentials = true;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值