本文最后更新于:2025年4月13日
                  
                
              
            
            
              
                
                在使用Apifox测试用户登录时,需要关闭验证码功能:点我查看
一、问题复现
假设我们在若依框架的ruoyi-admin模块中编写了一个controller代码,已知端口号是8080:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | package com.ruoyi.data.controller;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 
 
 
 
 
 
 @RestController
 @RequestMapping("/data")
 public class DataController extends BaseController {
 
 @GetMapping(value = "/getDataInfo")
 public AjaxResult getDataInfo() {
 return success("你好,我是iCode504,程序猿一枚,请多指教(●'◡'●)");
 }
 }
 
 | 
我们在Apifox中对该接口进行测试时,响应结果很有可能会出现401的情况:

二、问题分析
出现上述问题的主要原因是登录时返回的token并没有添加到接口实际的请求上:

三、解决方案
方式一:提取token(一劳永逸)
1. 我们先找到用户登录接口,按照下图所示找到接口的后置操作:

2. 添加后置操作中选择提取变量:

3. 按照下图填写相关信息,完成后按Ctrl+S键保存:

4. 在左侧找到根目录,按照下图所示填写全局Token,完成后按Ctrl+S键保存:

5. 此时重新登录,再次运行测试接口,此时我们就可以看到接口返回的数据了:

6. 此时接口的实际请求中也包含登录时返回的token信息:

方式二:在接口方法上添加@Anoymous注解(临时使用)
注意:该方式只适用于开发环境中,请勿用于生产环境!
要想避开登录访问,可以在controller方法中添加@Anoymous注解,即匿名访问:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | package com.ruoyi.data.controller;
 import com.ruoyi.common.annotation.Anonymous;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 
 
 
 
 
 
 @RestController
 @RequestMapping("/data")
 public class DataController extends BaseController {
 
 @Anonymous
 @GetMapping(value = "/getDataInfo")
 public AjaxResult getDataInfo() {
 return success("你好,我是iCode504,程序猿一枚,请多指教(●'◡'●)");
 }
 }
 
 | 
重新启动若依框架,此时到Apifox中对该接口进行测试,也可以看到返回结果:
