龙空技术网

登录后生成token,前端token刷新检测

青山依旧337 119

前言:

眼前姐妹们对“php生成token并传给前端”大概比较珍视,大家都需要学习一些“php生成token并传给前端”的相关知识。那么小编在网络上汇集了一些有关“php生成token并传给前端””的相关内容,希望同学们能喜欢,同学们快快来学习一下吧!

1.登录后生成token

public Object loginbypc(HttpServletRequest request) {        String username = request.getParameter("username");	    String password =request.getParameter("password");	    System.out.println("username===="+username);        System.out.println("password===="+password);        if (username == null || password == null) {            return ResponseUtil.badArgument();        }        List<LitemallUser> userList = userService.queryByUsername(username);        LitemallUser user = null;        if (userList.size() > 1) {            return ResponseUtil.serious();        } else if (userList.size() == 0) {            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");        } else {            user = userList.get(0);        }        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();        if (!encoder.matches(password, user.getPassword())) {            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");        }        // 更新登录情况        user.setLastLoginTime(LocalDateTime.now());        user.setLastLoginIp(IpUtil.getIpAddr(request));        if (userService.updateById(user) == 0) {            return ResponseUtil.updatedDataFailed();        }        // userInfo        UserInfo userInfo = new UserInfo();        userInfo.setNickName(username);        userInfo.setAvatarUrl(user.getAvatar());        // token        String token = UserTokenManager.generateToken(user.getId());        Map<Object, Object> result = new HashMap<Object, Object>();        result.put("token", token);        result.put("userInfo", userInfo);        return ResponseUtil.ok(result);    }    

2.前端token刷新检测

$(function() {    	    var token    = window.localStorage.getItem("token")    	    console.log(token)    	    var userInfo = window.localStorage.getItem("userInfo")    	    //console.log(userInfo)    	    var Authorization=window.localStorage.getItem("Authorization")    	    //console.log(Authorization)	        var avatar=window.localStorage.getItem("avatar")	        //console.log(avatar)	        var nickName=window.localStorage.getItem("nickName")	        //console.log(nickName)	        if(token!=null){//成功	            	            	document.getElementById("avatar").src="/vue/"+avatar;        	    $("#nickName").append(nickName);        	    $("#avatarimg").show();        	    $("#nickName").show();        	    $("#logout").show();        	    $("#login").hide();        	    $("#register").hide();        	                }else{                //401            	//document.getElementById("avatar").src='';        	    //$("#nickName").append('');            	$("#avatarimg").hide();        	    $("#nickName").hide();        	    $("#logout").hide();        	    $("#login").show();        	    $("#register").show();            	            }

3.显示:

登入后:生成token

退出后:token为空

标签: #php生成token并传给前端