管道基础大数据平台系统开发-【后端】-Server
src/main/java/com/lf/server/service/sys/TokenService.java
@@ -1,8 +1,9 @@
package com.lf.server.service.sys;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.sys.LoginEntity;
import com.lf.server.entity.sys.TokenEntity;
import com.lf.server.entity.sys.UsersEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.entity.all.RedisCacheKey;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
@@ -26,25 +27,25 @@
@Service
public class TokenService implements TokenMapper {
    @Autowired
    private RedisService redisService;
    public RedisService redisService;
    @Autowired
    TokenMapper tokenMapper;
    @Autowired
    UsersService usersService;
    UserService usersService;
    @Autowired
    LoginService loginService;
    @Override
    public Integer selectCount(String token) {
        return tokenMapper.selectCount(token);
    public Integer selectCount(String name, Integer type) {
        return tokenMapper.selectCount(name, type);
    }
    @Override
    public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset) {
        return tokenMapper.selectByPage(token, limit, offset);
    public List<TokenEntity> selectByPage(String name, Integer type, Integer limit, Integer offset) {
        return tokenMapper.selectByPage(name, type, limit, offset);
    }
    @Override
@@ -94,32 +95,21 @@
    /**
     * 获取新的令牌实体类
     *
     * @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.setDuration(SettingData.TOKEN_EXPIRE);
        te.setExpire(WebHelper.getTimestamp(SettingData.TOKEN_EXPIRE));
        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);
@@ -145,10 +135,6 @@
    /**
     * 登出
     *
     * @param token
     * @param req
     * @return
     */
    public Boolean logout(String token, HttpServletRequest req, HttpServletResponse res) {
        TokenEntity te = getEntityByToken(token);
@@ -160,7 +146,7 @@
        WebHelper.deleteAll(req, res);
        // 获取当前用户
        UsersEntity ue = getCurrentUser(req);
        UserEntity ue = getCurrentUser(req);
        if (ue == null) {
            return false;
        }
@@ -187,9 +173,6 @@
    /**
     * 根据令牌获取实体
     *
     * @param token
     * @return
     */
    public TokenEntity getEntityByToken(String token) {
        if (StringHelper.isNull(token)) {
@@ -215,12 +198,8 @@
    /**
     * 保存token
     *
     * @param te
     * @param req
     * @param res
     */
    public void saveToken(UsersEntity ue, TokenEntity te, HttpServletRequest req, HttpServletResponse res) {
    public void saveToken(UserEntity ue, TokenEntity te, HttpServletRequest req, HttpServletResponse res) {
        // 保存至Cookie
        WebHelper.saveToken2Cookie(te.getToken(), req, res);
@@ -239,7 +218,7 @@
     * @param req
     * @return
     */
    public UsersEntity getCurrentUser(HttpServletRequest req) {
    public UserEntity getCurrentUser(HttpServletRequest req) {
        String token = WebHelper.getToken(req);
        if (StringHelper.isNull(token)) {
            return null;
@@ -249,12 +228,12 @@
        // redis
        Object obj = redisService.get(userKey);
        if (obj != null && obj instanceof UsersEntity) {
            return (UsersEntity) obj;
        if (obj != null && obj instanceof UserEntity) {
            return (UserEntity) obj;
        }
        // db
        UsersEntity ue = usersService.selectByToken(token);
        UserEntity ue = usersService.selectByToken(token);
        if (ue != null) {
            TokenEntity te = getEntityByToken(token);
            if (te != null) {