栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于vue-cli和spring boot的用户登录功能(含验证码)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于vue-cli和spring boot的用户登录功能(含验证码)

一、基础:vue、router、spring boot、mybatis、element ui、axios、jwt

二、创建一个空项目,分别创建后台模块和前端模块

后端模块

前端模块

 

 

三、编写spring boot核心配置文件

四、token生成工具类 JWTUtils.java

public class JWTUtils {

    private static final String SIGN = "oaoa";

    //获取token
    public static String getToken(Map map){

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE,30);  //设置30 分钟过期
        Date time = calendar.getTime();
        JWTCreator.Builder builder = JWT.create();

        //如果有效荷载不为空,循环赋值
        if(!map.isEmpty()){
            map.forEach((k,v)->{
                builder.withClaim(k,v);
            });
        }

        String  token = builder.withExpiresAt(time).sign(Algorithm.HMAC256(SIGN));
        return token;
    }

    //token验证
    public static boolean verify(String token){
        try {
            JWTVerifier build = JWT.require(Algorithm.HMAC256(SIGN)).build();
            DecodedJWT verify = build.verify(token);
            return true;
        }catch (Exception e){
            log.info("token验证失败,错误信息为========》"+e.getMessage());
            return false;

        }

    }


}


前端工具js:request.js

//配置请求拦截器,每一个请求携带token
instance.interceptors.request.use(config=>{
  console.log(config);

  //当前端缓存有token存在时,给每个请求携带相应的token信息
  if(localStorage.getItem('token')){
    config.headers.token = localStorage.getItem('token');
  }

  config.withCredentials=false;
  console.log("请求拦截器");
  return config;
});

//响应拦截器
instance.interceptors.response.use(response=>{
  console.log('响应拦截器');
  console.log(response);
  if(response.data.code == 'ERROR'){

    MessageBox.alert(response.data.msg,{
      callback:action => {
        window.location.href = "/"
      }
    })
  }
  return response;


});

//将instance暴露出去
export default instance;

 五、成品效果

由于代码比较多,暂时未上传完整代码!!!有需要的朋友可以私聊

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571334.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号