当前位置:   article > 正文

VUE之axios使用,跨域问题,拦截器添加Token_vue axios拦截器 动态设置token

vue axios拦截器 动态设置token

参考资料:

参考视频

视频资料及个人demo

Axios中文文档

VUE之基本部署及VScode常用插件

VUE之基本组成和使用

VUE之Bootstrap和Element-UI的使用


准备工作:

  • 关于SpringBoot和SpringCloud的搭建,以及mybatis-plus的整合见本人之前的CSDN博客,下面编写get请求和post请求
  • 编写Controller如下:
  1. package com.example.springbootjwtback.UserController;
  2. import com.example.springbootjwtback.entity.ResponseResult;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * @program: springboot-jwt-back
  13. * @description:
  14. * @author: wjl
  15. * @create: 2023-07-24 00:10
  16. **/
  17. @Controller
  18. @Slf4j
  19. @ResponseBody
  20. public class TestController {
  21. @GetMapping("testGet/{name}")
  22. public ResponseResult testGet(@PathVariable("name") String name){
  23. log.info(name);
  24. GetEntity entity = new GetEntity("后端返回name", "后端返回age");
  25. return new ResponseResult(ResponseResult.OK,entity);
  26. }
  27. @PostMapping("testPost")
  28. public ResponseResult testPost(@RequestBody PostEntity entity){
  29. log.info(entity.toString());
  30. List<PostEntity> list = new ArrayList<>();
  31. list.add(new PostEntity("2021-02-03","peter","山西省"));
  32. list.add(new PostEntity("2021-02-04","alise","河北省"));
  33. list.add(new PostEntity("2021-02-05","lisa","北京市"));
  34. return new ResponseResult(ResponseResult.OK,list);
  35. }
  36. }
  37. @Data
  38. @NoArgsConstructor
  39. @AllArgsConstructor
  40. class GetEntity{
  41. private String name;
  42. private String age;
  43. }
  44. @Data
  45. @NoArgsConstructor
  46. @AllArgsConstructor
  47. class PostEntity{
  48. private String date;
  49. private String name;
  50. private String address;
  51. }
  • 编写 返回类
  1. package com.example.springbootjwtback.entity;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. /**
  5. * @program: springboot-jwt-back
  6. * @description: 返回实体
  7. * @author: wjl
  8. * @create: 2023-07-09 15:52
  9. **/
  10. @Data
  11. @NoArgsConstructor
  12. public class ResponseResult {
  13. /**
  14. * 表明该请求被成功地完成,所请求的资源发送到客户端
  15. */
  16. public static final Integer OK = 200;
  17. /**
  18. * 请求要求身份验证,常见对于需要登录而用户未登录的情况。
  19. */
  20. public static final Integer UNAUTHORIZED = 401;
  21. /**
  22. * 服务器拒绝请求,常见于机密信息或复制其它登录用户链接访问服务器的情况。
  23. */
  24. public static final Integer FORBIDDEN = 403;
  25. /**
  26. * 服务器无法取得所请求的网页,请求资源不存在。
  27. */
  28. public static final Integer NOT_FOUND = 404;
  29. /**
  30. * 服务器内部错误。
  31. */
  32. public static final Integer SERVER_ERROR = 500;
  33. private Integer code;
  34. private String msg = "";
  35. private Object data = new int[0];
  36. public ResponseResult(Integer code) {
  37. this.code = code;
  38. }
  39. public ResponseResult(Integer code, Object data) {
  40. this.code = code;
  41. this.data = data;
  42. }
  43. public ResponseResult(Integer code, String msg, Object data) {
  44. this.code = code;
  45. this.msg = msg;
  46. this.data = data;
  47. }
  48. }

前后端分离跨域问题解决:

  • 跨域定义:当一个请求url的协议、域名、端口三者之间任意一个与当前页面url不同即为跨域。
  • 解决:当前后端分离,拥有各自的URL后,前端访问后端就形成了跨域问题;解决方法也很简单,只需要在Controller上添加注解:
@CrossOrigin(origins = {"*", "null"})//  解决跨域问题


Axios的使用:

1.官方文档介绍

 2.Axios安装和使用

  • 输入指令进行安装

npm install axios

  •  在Vue的<Script>部分中进行引入,而不是在main.js中使用
  1. import axios from "axios";

  •  在之前的例子基础上,修改  clicktest 方法,取后端数据进行更新
  1. clicktest() {
  2. alert("come in");
  3. this.selfName = "更新后张三";
  4. this.student = { name: "更新后name", age: "更新后age" };
  5. axios({
  6. url: "http://localhost:8888/testGet/张三",
  7. method: "GET",
  8. }).then(res => {
  9. console.log(res.data);
  10. this.student = res.data.data;
  11. })
  12. },

 

  •  在之前的基础,修改 testsubmit 方法,取后端数据进行更新
  1. testsubmit(){
  2. alert("come in");
  3. axios({
  4. url: "http://localhost:8888/testPost",
  5. method: "POST",
  6. data: {
  7. date: "2022-02-02",
  8. name: "张三",
  9. address: "hebei"
  10. }
  11. }).then(res => {
  12. this.tableData = res.data.data
  13. })
  14. }

 

 

  •  点击验证

 


Axios拦截器使用

1.官方文档介绍

  • Axios拦截器是Axios功能的一部分,可以在网络请求时,在请求前、后进行操作,如在请求前添加token,以及在请求后进行页面的跳转等操作
  • Axios拦截器官方文档,具体结构如下

 2.Axios拦截器的使用

1)、准备工作
  • 添加接口
  1. @PostMapping("interpost")
  2. public ResponseResult interpost(@RequestBody GetEntity entity,@RequestHeader("token") String token){
  3. log.info(entity.toString());
  4. log.info("token:"+token);
  5. List<PostEntity> list = new ArrayList<>();
  6. list.add(new PostEntity("2021-02-03","peter","山西省"));
  7. list.add(new PostEntity("2021-02-04","alise","河北省"));
  8. list.add(new PostEntity("2021-02-05","lisa","北京市"));
  9. return new ResponseResult(ResponseResult.OK,list);
  10. }
  11. @PostMapping("interget")
  12. public ResponseResult interget(@RequestBody GetEntity entity,@RequestHeader("token") String token){
  13. log.info(entity.toString());
  14. log.info("token:"+token);
  15. List<PostEntity> list = new ArrayList<>();
  16. list.add(new PostEntity("2021-02-03","peter","山西省"));
  17. list.add(new PostEntity("2021-02-04","alise","河北省"));
  18. list.add(new PostEntity("2021-02-05","lisa","北京市"));
  19. return new ResponseResult(ResponseResult.OK,list);
  20. }

2)、VUE项目中添加拦截器
  • 在上述配置好Axios的前提下
  • 在VUE项目中,src文件夹
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/715605
推荐阅读
相关标签