管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-30 c15739adf58aa87002516f151d6314b8caecbb29
src/main/java/com/lf/server/service/data/TokenService.java
@@ -1,11 +1,21 @@
package com.lf.server.service.data;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.LoginEntity;
import com.lf.server.entity.data.TokenEntity;
import com.lf.server.entity.data.UsersEntity;
import com.lf.server.entity.sys.RedisCacheKey;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
import com.lf.server.mapper.data.TokenMapper;
import com.lf.server.service.sys.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 令牌表
@@ -15,9 +25,17 @@
@Service
public class TokenService implements TokenMapper {
    @Autowired
    private RedisService redisService;
    @Autowired
    TokenMapper tokenMapper;
    @Autowired
    UsersService usersService;
    @Autowired
    LoginService loginService;
    @Override
    public Integer selectCount(String token) {
@@ -26,7 +44,22 @@
    @Override
    public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset) {
        return tokenMapper.selectByPage(token,limit,offset);
        return tokenMapper.selectByPage(token, limit, offset);
    }
    @Override
    public TokenEntity selectToken(int id) {
        return tokenMapper.selectToken(id);
    }
    @Override
    public TokenEntity selectOneByToken(String token) {
        return tokenMapper.selectOneByToken(token);
    }
    @Override
    public List<TokenEntity> selectTokenAll() {
        return tokenMapper.selectTokenAll();
    }
    @Override
@@ -55,12 +88,179 @@
    }
    @Override
    public TokenEntity selectToken(int id) {
        return tokenMapper.selectToken(id);
    public Integer updateTokenExpire(TokenEntity tokenEntity) {
        return tokenMapper.updateTokenExpire(tokenEntity);
    }
    @Override
    public List<TokenEntity> selectTokenAll() {
        return tokenMapper.selectTokenAll();
    /**
     * 获取新的令牌实体类
     *
     * @param userid
     * @param req
     * @return
     */
    public TokenEntity getNewToken(int userid, HttpServletRequest req) {
        int duration = 240;
        TokenEntity te = new TokenEntity();
        te.setToken(WebHelper.getGuid());
        te.setDuration(duration);
        te.setExpire(WebHelper.getTimestamp(duration));
        te.setType(0);
        te.setIp(WebHelper.getIpAddress(req));
        te.setCreateUser(userid);
        return te;
    }
    /**
     * 是/否登录
     *
     * @param req
     * @param res
     * @return
     */
    public Boolean isLogin(HttpServletRequest req, HttpServletResponse res) {
        String token = WebHelper.getToken(req);
        if (StringHelper.isNull(token)) {
            return false;
        }
        // redis
        String tokenKey = RedisCacheKey.signTokenKey(token);
        if (redisService.hasKey(tokenKey)) {
            return true;
        }
        // db
        TokenEntity te = selectOneByToken(token);
        if (te != null) {
            redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
            return true;
        }
        return false;
    }
    /**
     * 登出
     *
     * @param token
     * @param req
     * @return
     */
    public Boolean logout(String token, HttpServletRequest req) {
        TokenEntity te = getEntityByToken(token);
        if (te == null) {
            return false;
        }
        // 清除Cookie
        WebHelper.deleteCookie(StaticData.TOKEN_COOKIE_KEY, req);
        // 清除缓存
        String tokenKey = RedisCacheKey.signTokenKey(token);
        redisService.delete(tokenKey);
        // 获取当前用户
        UsersEntity ue = getCurrentUser(req);
        if (te == null) {
            return false;
        }
        // db,设置令牌过期
        te.setUpdateUser(ue.getId());
        Integer rows = updateTokenExpire(te);
        if (rows == 0) {
            return false;
        }
        // 写日志
        LoginEntity le = loginService.getNewLogin(ue.getId(), req);
        le.setType(3);
        rows = loginService.insertLogin(le);
        return rows > 0;
    }
    /**
     * 根据令牌获取实体
     *
     * @param token
     * @return
     */
    public TokenEntity getEntityByToken(String token) {
        if (StringHelper.isNull(token)) {
            return null;
        }
        String tokenKey = RedisCacheKey.signTokenKey(token);
        // redis
        Object obj = redisService.get(tokenKey);
        if (obj != null && obj instanceof TokenEntity) {
            return (TokenEntity) obj;
        }
        // db
        TokenEntity te = selectOneByToken(token);
        if (te != null) {
            redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
        }
        return te;
    }
    /**
     * 保存token
     *
     * @param te
     * @param req
     * @param res
     */
    public void saveToken(UsersEntity ue, TokenEntity te, HttpServletRequest req, HttpServletResponse res) {
        // 保存至Cookie
        WebHelper.saveToken2Cookie(te.getToken(), req, res);
        // 令牌保存至Redis
        String tokenKey = RedisCacheKey.signTokenKey(te.getToken());
        redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
        String userKey = RedisCacheKey.signUserKey(te.getToken());
        redisService.put(userKey, ue, te.getDuration(), TimeUnit.MINUTES);
    }
    /**
     * 获取当前用户
     *
     * @param req
     * @return
     */
    public UsersEntity getCurrentUser(HttpServletRequest req) {
        String token = WebHelper.getToken(req);
        if (StringHelper.isEmpty(token)) {
            return null;
        }
        String userKey = RedisCacheKey.signUserKey(token);
        // redis
        Object obj = redisService.get(userKey);
        if (obj != null && obj instanceof UsersEntity) {
            return (UsersEntity) obj;
        }
        // db
        UsersEntity ue = usersService.selectByToken(token);
        if (ue != null) {
            TokenEntity te = getEntityByToken(token);
            if (te != null) {
                redisService.put(userKey, ue, te.getDuration(), TimeUnit.MINUTES);
            }
        }
        return ue;
    }
}