vue-admin-template 基础版本api接口配置和springboot跨域问题(学习记录)

一、vue-admin-template 代理设置

1,打开vue.config.js文件在overlay后新增

proxy: {   

      '/api': {

        // 后端地址

        target: 'http://localhost:8081/',

        // 允许跨域

        changeOrigin: true,

        // 路径重写:我们可以应/api,替代上述后端url

        pathRewrite: {

          '^/api': '/'

        }

      }

    },

2,自己开发时候找到 .env.development 文件设置 VUE_APP_BASE_API里的地址为你的后台api接口地址即可

VUE_APP_BASE_API = 'http://127.0.0.1:8081'

注:线上需要修改.env.production里的VUE_APP_BASE_API

二 、springboot 跨域

1,在controller同级创建config包,在config包里创建CorsConfig.java文件


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry){
        //设置允许跨域的路径

        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                //这里:是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
        System.out.println("跨域配置已执行 ");
    }
}

2,打开***Application文件,使其实现WebMvcConfigurer类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class KousuanApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(KousuanApplication.class, args);

    }

}

3,测试控制器

import com.example.kousuan.pojo.LoginModel;
import com.example.kousuan.pojo.Subject;
import com.example.kousuan.service.User.KSUserService;
import com.example.kousuan.until.ApiResUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
public class UserController {

   

    /**
     * 登录
     */
    @RequestMapping("/api/user/login")
    public void  login(@RequestBody LoginModel loginModel){

        System.out.println(loginModel.getUsername());

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值