| | |
| | | package com.terra.common.service; |
| | | |
| | | import com.terra.common.entity.all.RedisCacheKey; |
| | | import com.terra.common.entity.all.SettingData; |
| | | import com.terra.common.entity.all.StaticData; |
| | | import com.terra.common.entity.lf.TokenPo; |
| | | import com.terra.common.entity.lf.UserPo; |
| | | import com.terra.common.helper.StringHelper; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 令牌帮助类 |
| | | * 通用服务类 |
| | | * @author WWW |
| | | */ |
| | | @Component |
| | | public class CommonService { |
| | | @Resource |
| | | RedisService redisService; |
| | | CommonMapper mapper; |
| | | |
| | | |
| | | @Resource |
| | | CommonMapper mapper; |
| | | RedisService redisService; |
| | | |
| | | public UserPo getCurrentUser(HttpServletRequest req) { |
| | | String token = WebHelper.getToken(req); |
| | |
| | | } |
| | | |
| | | public UserPo getUserByToken(String token) { |
| | | if (StringHelper.isNull(token)) { |
| | | return null; |
| | | } |
| | | if (StringHelper.isNull(token)) return null; |
| | | |
| | | // redis |
| | | String userKey = RedisCacheKey.signUserKey(token); |
| | | Object obj = redisService.get(userKey); |
| | | if (obj instanceof UserPo) { |
| | | return (UserPo) obj; |
| | | } |
| | | TokenPo tokenPo = getTokenPo(token); |
| | | if (null == tokenPo) return null; |
| | | |
| | | // db |
| | | UserPo po = mapper.selectByToken(token); |
| | | if (null != po) { |
| | | getEntityByToken(token); |
| | | } |
| | | |
| | | return po; |
| | | return getUser(token); |
| | | } |
| | | |
| | | public TokenPo getEntityByToken(String token) { |
| | | public TokenPo getTokenPo(String token) { |
| | | String tokenKey = RedisCacheKey.signTokenKey(token); |
| | | |
| | | // redis |
| | |
| | | } |
| | | |
| | | // db |
| | | TokenPo te = mapper.selectOneByToken(token); |
| | | if (null != te) { |
| | | long min = StringHelper.getMinuteDifference(te.getExpire()); |
| | | if (min > 0) { |
| | | redisService.put(tokenKey, te, min, TimeUnit.MINUTES); |
| | | } |
| | | TokenPo po = mapper.selectOneByToken(token); |
| | | if (null != po) { |
| | | long min = StringHelper.getMinuteDifference(po.getExpire()); |
| | | redisService.put(tokenKey, po, min, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return te; |
| | | return po; |
| | | } |
| | | |
| | | public static String getCurrentUserName() { |
| | | return null; |
| | | public UserPo getUser(String token) { |
| | | String userKey = RedisCacheKey.signUserKey(token); |
| | | |
| | | // redis |
| | | Object obj = redisService.get(userKey); |
| | | if (obj instanceof UserPo) { |
| | | return (UserPo) obj; |
| | | } |
| | | |
| | | // db |
| | | UserPo po = mapper.selectByToken(token); |
| | | if (null != po) { |
| | | redisService.put(userKey, po, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return po; |
| | | } |
| | | |
| | | /** |
| | | * 用户ID是/否禁用 |
| | | */ |
| | | public boolean isUidDisable(UserPo ue) { |
| | | String key = RedisCacheKey.signPwdError(ue.getUid()); |
| | | Object objCount = redisService.get(key); |
| | | |
| | | return null != objCount && (int) objCount >= SettingData.PWD_ERR_COUNT; |
| | | } |
| | | |
| | | public List<String> selectIpList(Integer type) { |
| | | String key = RedisCacheKey.blacklistKey(type.toString()); |
| | | |
| | | Object obj = redisService.get(key); |
| | | if (obj instanceof List<?>) { |
| | | return (List<String>) obj; |
| | | } |
| | | |
| | | List<String> list = mapper.selectIpList(type); |
| | | if (list != null && !list.isEmpty()) { |
| | | redisService.put(key, list, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | public List<String> selectPerms(String uid) { |
| | | String key = RedisCacheKey.permsPermsKey(uid); |
| | | Object obj = redisService.get(key); |
| | | if (obj instanceof List<?>) { |
| | | return (List<String>) obj; |
| | | } |
| | | |
| | | List<String> list = mapper.selectPerms(uid); |
| | | if (list != null && !list.isEmpty()) { |
| | | redisService.put(key, list, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | } |