网站首页 > 开源技术 正文
1.vue-admin-template中已经设置了cookies方法保存和删除cookie
@/utils/auth.
import Cookies from 'js-cookie'
const TokenKey = 'vue_admin_template_token'
export function getToken() {
return Cookies.get(TokenKey)
}
export function setToken(token) {
return Cookies.set(TokenKey, token)
}
export function removeToken() {
return Cookies.remove(TokenKey)
}
2.@/api/user设置request请求
import request from '@/utils/request'
export function login(data) {
return request({
url: '/employee/login',
method: 'post',
data
})
}
3.@/store/user设置登陆方法
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
const state = {
token: getToken()
}
const mutations = {
setToken(state, token) {
state.token = token
// 同步到缓存
setToken(token)
},
removeToken() {
// 删除vuex的token
state.token = null
removeToken()
}
}
const actions = {
async login(context, data) {
console.log(data)
// 调用登陆接口,返回一个token
const token = await login(data)
console.log('login-token,', token)
context.commit('setToken', token.token)
console.log(state.token)
}
}
export default {
namespaced: true, // 开启命名空间
state,
mutations,
actions
}
4.登陆界面调用登陆方法
@/views/login
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
const state = {
token: getToken()
}
const mutations = {
setToken(state, token) {
state.token = token
// 同步到缓存
setToken(token)
},
removeToken() {
// 删除vuex的token
state.token = null
removeToken()
}
}
const actions = {
async login(context, data) {
console.log(data)
// 调用登陆接口,返回一个token
const token = await login(data)
console.log('login-token,', token)
context.commit('setToken', token.token)
console.log(state.token)
}
}
export default {
namespaced: true, // 开启命名空间
state,
mutations,
actions
}
4.后端拦截器验证前端请求,否则返回401
package com.sky.interceptor;
import com.sky.constant.JwtClaimsConstant;
import com.sky.properties.JwtProperties;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
/**
* jwt令牌校验的拦截器
*/
@Component
@Slf4j
public class JwtTokenAdminInterceptor implements HandlerInterceptor {
@Autowired
private JwtProperties jwtProperties;
/**
* 校验jwt
*
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断当前拦截到的是Controller的方法还是其他资源
if (!(handler instanceof HandlerMethod)) {
//当前拦截到的不是动态方法,直接放行
return true;
}
//1、从请求头中获取令牌
log.info("登陆请求头");
// 遍历并打印所有头部信息
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
log.info("Header Name: " + name);
String value = request.getHeader(name);
log.info("Header Value: " + value);
}
log.info("------");
String token = request.getHeader(jwtProperties.getAdminTokenName());
token = token.replace("vue_admin_template_token=","");
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("Name2: " + name);
System.out.println("Value2: " + value);
if(name.contains("vue_admin_template_token"))
{
token = value;
System.out.println("get token");
}
}
} else {
System.out.println("No cookies found.");
}
//2、校验令牌
try {
log.info("jwt校验:{}", token);
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
log.info("当前员工id:", empId);
//3、通过,放行
return true;
} catch (Exception ex) {
//4、不通过,响应401状态码
response.setStatus(401);
return false;
}
}
}
猜你喜欢
- 2024-10-02 拉取远程项目(拉取远程仓库代码)
- 2024-10-02 开源软件分享-VUE后台管理模板(vue后端管理系统)
- 2024-10-02 vue-admin-template封装头像(vue template admin)
- 2024-06-22 9.7k star 开源免费且开箱即用的中后台管理模版vue-pure-admin
- 2024-06-22 基于 vue3+element-plus 前端后台管理系统SCUI Admin
- 2024-06-22 Layui Admin 后台模板
- 2024-06-22 基于 Bootstrap v3.3.7的后台管理模板 Light Year Admin
- 2024-06-22 清新优雅&高颜值!一个基于Vue3实现的后台管理模板
- 2024-06-22 Spring Boot + Vue + Shiro 实现前后端分离、权限控制
- 2024-06-22 【开源】后端开发也很容易上手的前端框架模板
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)