nuxt登录整合JWT

 

生成token

拷贝工具类,及其Properties类

 yml配置

 登录生成token

注入jwtproperties

 登录成功添加token

@Resource
private JwtProperties jwtProperties;

携带Token 

保存token

用户登录成功后,将token保存浏览器

localStorage.setItem('token',baseResult.other.token)

 携带token

对axios进行增强,需要每次都写到token

编写nuxt的插件

 

import { MessageBox, Message } from 'element-ui'

export default function ({ $axios, redirect }) {

$axios.onRequest(config => {

// 从 localStorage 获得token,并添加到头

let token = localStorage.getItem('token')

if(token) {

//Adds header: `Authorization: 123` to all requests

$axios.setToken(token)

}

})

$axios.onError(error => {

const code = parseInt(error.response && error.response.status)

if (code === 401) {

MessageBox.confirm('Token失效', '请确认重新登录', {

confirmButtonText: '确定',

cancelButtonText: '取消',

type: 'warning'

}).then(() => {

redirect('/user/login')

}).catch(() => {

Message({

type: 'info',

message: '已取消重新登录'

});

});

}

})

}

 注册插件

{ src: '~/plugins/axios.js', mode: 'client' }

 校验Token

拷贝工具类+配置类 + yml文件 :jwt相关、白名单相关

sc:
  jwt:
    secret: sc@Login(Auth}*^31)&czxy% # 登录校验的密钥
    pubKeyPath: D:/rsa/rsa.pub # 公钥地址
    priKeyPath: D:/rsa/rsa.pri # 私钥地址
    expire: 360 # 过期时间,单位分钟
  filter:
    allowPaths:
      - swagger
      - /api-docs
      - /user/login
      - /user/check

 编写登录过滤器:对白名单进行放行,其他的内容使用jwt进行处理

 

 获得Token

拷贝yml配置

 

 拷贝jwt工具类 + 拷贝对应Properties配置类

 编写代码:获得请求头,通过请求头获得token

@Resource
private JwtProperties jwtProperties;
@Resource
private HttpServletRequest request;

 

String token = request.getHeader("Authorization");
TbUser loginUser = JwtUtils.getObjectFromToken(token, jwtProperties.getPublicKey(), TbUser.class);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值