我跟着网上视频做毕设,写到了用jwt设置token,然后登录进去和查询数据是可以的,但是我新写了一个请求,发起这个网络请求却显示404错误,我用postman接口测试,也把token放进去了的,后端sprintboot日志也打印出了我访问的信息(是用户的所有分组信息)请问怎么解决?
前端代码:(前端是想在侧边栏动态显示“我的分组”的选择项)
<template>
<el-menu :default-openeds="['1', '3']" style="min-height: 100%; overflow-x:hidden"
background-color="rgb(48,65,86)"
text-color="#fff"
active-text-color="#ffd04b"
:collapse-transition="false"
:collapse="isCollapse"
router
@select="handleSelect"
>
<div style="height: 30px; line-height: 30px; text-align: center">
<p style="color: white">欢迎您18722799479</p>
</div>
<el-menu-item index="/contacts">
<i class="el-icon-s-home"></i>
<span slot="title">联系人管理</span>
</el-menu-item>
<el-submenu index="/group">
<template slot="title">
<i class="el-icon-s-custom"></i>
<span slot="title">我的分组</span>
</template>
<el-menu-item
v-for="group in groups"
:key="group.id"
:index="group.id">
<i class="el-icon-s-custom"></i>
<span slot="title">{{ group.name }}</span>
</el-menu-item>
</el-submenu>
</el-menu>
</template>
<script>
data() {
return {
uid: null,
groups: []
}
},
created() {
this.findAllGroups();
},
mounted() {
const user = JSON.parse(localStorage.getItem("user"));
if (user && user.uid) {
this.uid = user.uid;
console.log("Aside获取的用户id为"+this.uid)
this.findAllGroups();
}
},
methods: {
handleSelect(index) {
console.log(index)
},
//查询该用户的所有分组信息
findAllGroups() {
// 确保获取到 uid 属性值
if (!this.uid) {
return;
}
this.request.get("/group/find",{
params: {uid: this.uid}
}).then(res => {
if (res.code === '200') {
console.log("Aside请求group的信息=========="+res)
this.groups = res.data;
this.$message.success("分组测试成功")
}else {
this.$message.error(res.msg)
}
})
}
}
}
</script>
后端接口为:
@Controller
@RequestMapping("/group")
public class GroupController {
@Autowired
private IGroupService groupService;
// private static final Log LOG = Log.get();
//查询当前用户所有的分组
@GetMapping("/find")
private Result getAllGroupByUid(@RequestParam Integer uid){
try {
QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("uid", uid);
List<Group> groupList = groupService.list(queryWrapper);
if (groupList.size() == 0){
return Result.error(Constants.CODE_400, "该用户没有分组");
} else {
return Result.success(groupList);
}
} catch (Exception e) {
e.printStackTrace();
return Result.error(Constants.CODE_500, "查询分组信息失败");
}
}
}
控制台报错为:
点开具体显示
这个提示是在设置token里面的
public class JwtInterceptor implements HandlerInterceptor {
@Autowired
private IUserService userService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String token = request.getHeader("token");
// 如果不是映射到方法直接通过
if(!(handler instanceof HandlerMethod)){
return true;
}
// 执行认证
if (StrUtil.isBlank(token)) { //如果是空字符串,抛出自定义异常
throw new ServiceException(Constants.CODE_401, "无token,请重新登录");
}
// 获取 token 中的 user id
String userId;
try {
userId = JWT.decode(token).getAudience().get(0);
} catch (JWTDecodeException j) {
throw new ServiceException(Constants.CODE_401, "token验证失败,请重新登录");
}
// 根据token中的userid查询数据库
User user = userService.getById(userId);
if (user == null) {
throw new ServiceException(Constants.CODE_401, "用户不存在,请重新登录");
}
// 最后用 用户密码加签验证 token
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPwd())).build();
try {
jwtVerifier.verify(token); // 验证token
} catch (JWTVerificationException e) {
throw new ServiceException(Constants.CODE_401, "token验证失败,请重新登录");
}
return true;
}
}
现在不知道怎么解决,求指教!!