今天的主要内容包括:用户个人信息展示与保存功能的实现、SpringBoot集成JWT、使用拦截器实现拦截功能、权限不足提示功能、在后台获取当前用户信息等。废话不多说,下面就开始今天的学习!
<template>
<el-card style="width: 500px;">
<el-form label-width="80px" size="small">
<el-form-item label="用户名">
<el-input v-model="form.username" disabled autocomplete="off">el-input>
el-form-item>
<el-form-item label="昵称">
<el-input v-model="form.nickname" autocomplete="off">el-input>
el-form-item>
<el-form-item label="邮箱">
<el-input v-model="form.email" autocomplete="off">el-input>
el-form-item>
<el-form-item label="电话">
<el-input v-model="form.phone" autocomplete="off">el-input>
el-form-item>
<el-form-item label="地址">
<el-input v-model="form.address" autocomplete="off">el-input>
el-form-item>
<el-form-item>
<el-button type="primary" @click="save">保 存el-button>
el-form-item>
el-form>
el-card>
template>
<script>
export default {
name: "Person",
data() {
return {
form: {},
user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
}
},
created() {
this.request.get("/user/username/" + this.user.username).then(res => {
if (res.code == '200') {
this.form = res.data
}
})
},
methods: {
save() {
this.request.post("/user", this.form).then(res => {
if (res) {
this.$message.success("保存成功")
} else {
this.$message.error("保存失败")
}
})
}
}
}
script>
<style scoped>
style>
package com.ironmanjay.springboot.config.Interceptor;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.ironmanjay.springboot.common.Constants;
import com.ironmanjay.springboot.entity.User;
import com.ironmanjay.springboot.exception.ServiceException;
import com.ironmanjay.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JwtInterceptor implements HandlerInterceptor {
@Autowired
private IUserService userService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
// 如果不是映射到方法直接通过
if (!(handler instanceof HandlerMethod)) {
return true;
}
// 执行认证
if (StrUtil.isBlank(token)) {
throw new ServiceException(Constants.CODE_401, "无token,请重新登录");
}
// 获取 token 中的 userId
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.getPassword())).build();
try {
jwtVerifier.verify(token); // 验证token
} catch (JWTVerificationException e) {
throw new ServiceException(Constants.CODE_401, "token验证失败,请重新登录");
}
return true;
}
}
以上就是今天学习的全部内容了,下篇博文将给大家带来关于SpringBoot和Vue实现文件上传与下载的内容。明天见!