在学习java编写接口。写好之后用vue写了个前端进行测试,结果总是有Access-Control-Allow-Origin错误。
上网找,说在vue.config.js做配置,加上以下内容:
module.exports = {devServer: {port : 8089,proxy: {'/mytest': {target: 'http://192.168.0.183:8081',ws: true,changeOrigin: true,pathRewrite: {'^/mytest': '' //遇到接口路径有test的,就换成http://192.168.0.183:8081这个请求头,同时把test去掉}}}},
}
加了,好像也不起作用。又找,说在后端代码处理。要在Controller上处理,在写web接口方法前加入注解:
@RestController
@CrossOrigin //加这个解决跨域
public class test {@GetMapping(value="/mytest")public String Testing(){return "This is testing 4";}
}
在后端处理好像是可以了,但会不会引发后端代码不够安全?个人刚刚学习,不太了解spring boot,如果有大神知道,能帮我解答一下,那是万分感谢!