管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-30 74c698fb0f094d7d322cfe6a218cf7c86e82a43f
src/main/java/com/lf/server/service/data/TokenService.java
@@ -1,14 +1,19 @@
package com.lf.server.service.data;
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;
/**
 * 令牌表
@@ -18,9 +23,14 @@
@Service
public class TokenService implements TokenMapper {
    @Autowired
    private RedisService redisService;
    @Autowired
    TokenMapper tokenMapper;
    @Autowired
    UsersService usersService;
    @Override
    public Integer selectCount(String token) {
@@ -29,7 +39,7 @@
    @Override
    public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset) {
        return tokenMapper.selectByPage(token,limit,offset);
        return tokenMapper.selectByPage(token, limit, offset);
    }
    @Override
@@ -63,15 +73,29 @@
    }
    @Override
    public TokenEntity selectOneByToken(String token) {
        return tokenMapper.selectOneByToken(token);
    }
    @Override
    public List<TokenEntity> selectTokenAll() {
        return tokenMapper.selectTokenAll();
    }
    /**
     * 获取新的令牌实体类
     *
     * @param userid
     * @param req
     * @return
     */
    public TokenEntity getNewToken(int userid, HttpServletRequest req) {
        TokenEntity te=new TokenEntity();
        int duration = 240;
        TokenEntity te = new TokenEntity();
        te.setToken(WebHelper.getGuid());
        te.setDuration(240);
        te.setExpire(WebHelper.getTimestamp(240));
        te.setDuration(duration);
        te.setExpire(WebHelper.getTimestamp(duration));
        te.setType(0);
        te.setIp(WebHelper.getIpAddress(req));
        te.setCreateUser(userid);
@@ -79,4 +103,112 @@
        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
     * @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;
    }
}