漏提了...

This commit is contained in:
dinggang 2022-11-15 16:52:20 +08:00
parent acdcf5b25c
commit 7e54dcebba
1 changed files with 57 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import io.renren.modules.security.entity.SysUserTokenEntity;
import io.renren.modules.security.oauth2.TokenGenerator; import io.renren.modules.security.oauth2.TokenGenerator;
import io.renren.modules.security.service.SysUserTokenService; import io.renren.modules.security.service.SysUserTokenService;
import io.renren.modules.sys.entity.SysOnlineEntity; import io.renren.modules.sys.entity.SysOnlineEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
@ -21,11 +22,17 @@ import java.util.Map;
@Service @Service
public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService { public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {
/** /**
* 12小时后过期 * 12小时后过期
*/ */
private final static int EXPIRE = 3600 * 12; private final static int EXPIRE = 3600 * 12;
@Value("${system.allowSimultaneousLogin}")
private int allowSimultaneousLogin;
/*
@Override @Override
public Result createToken(Long userId) { public Result createToken(Long userId) {
//用户token //用户token
@ -81,6 +88,56 @@ public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, Sy
map.put("expire", EXPIRE); map.put("expire", EXPIRE);
return new Result().ok(map); return new Result().ok(map);
} }
*/
@Override
public Result createToken(Long userId) {
//用户token
String token;
//当前时间
Date now = new Date();
//过期时间
Date expireTime = new Date(now.getTime() + EXPIRE * 1000);
//判断是否生成过token
SysUserTokenEntity tokenEntity = baseDao.getByUserId(userId);
if(tokenEntity == null){
//生成一个token
token = TokenGenerator.generateValue();
tokenEntity = new SysUserTokenEntity();
tokenEntity.setUserId(userId);
tokenEntity.setToken(token);
tokenEntity.setUpdateDate(now);
tokenEntity.setExpireDate(expireTime);
//保存token
this.insert(tokenEntity);
}else{
//判断是否允许同时登录,0不允许1允许
//允许同时在线时返回同一token且不校验过期时间
if (allowSimultaneousLogin == 0 && tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()) {
//token过期重新生成token
token = TokenGenerator.generateValue();
tokenEntity.setToken(token);
tokenEntity.setUpdateDate(now);
tokenEntity.setExpireDate(expireTime);
//更新token
this.updateById(tokenEntity);
} else {
token = tokenEntity.getToken();
}
}
Map<String, Object> map = new HashMap<>(2);
map.put(Constant.TOKEN_HEADER, token);
map.put("expire", EXPIRE);
return new Result().ok(map);
}
@Override @Override
public void logout(Long userId) { public void logout(Long userId) {