Ejemplo n.º 1
0
 @RequestMapping("/login")
 @ResponseBody
 public Response login(@RequestBody Map<String, Object> map) {
   Response res = new Response();
   List<User> users = userService.login(map);
   if (users == null || users.size() == 0) {
     return res.failure("登陆失败");
   }
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     String skey = "session:" + users.get(0).getId();
     String token = UUID.randomUUID().toString();
     if (shardedJedis.exists(skey)) {
       String oldtoken = shardedJedis.get(skey);
       shardedJedis.del(skey);
       shardedJedis.del("token:" + oldtoken);
     }
     if (shardedJedis.setnx(skey, token) == 1) {
       shardedJedis.expire(skey, 3600 * 24);
       shardedJedis.hset("token:" + token, "name", users.get(0).getName());
       shardedJedis.expire("token:" + token, 3600 * 24);
     } else {
       token = shardedJedis.get(skey);
     }
     Map<String, Object> data = new HashMap<>();
     data.put("token", token);
     return new Response().success(data);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 2
0
 private void returnResource(ShardedJedis shardedJedis) {
   try {
     shardedJedis.close();
   } catch (Exception e) {
     logger.error("returnResource error.", e);
   }
 }
Ejemplo n.º 3
0
 public void delAuthCode(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.del(key);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 4
0
 public void addAccessToken(String key, String accessToken) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, accessToken);
     shardedJedis.expire(key, Constants.ACCESS_TOKEN_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 5
0
 public void addAuthCode(String key, String authCode) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   try {
     shardedJedis.set(key, authCode);
     shardedJedis.expire(key, Constants.AUTH_CODE_EXPIRE);
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 6
0
 public String getAccessTokenByKey(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   String accessToken = "";
   try {
     accessToken = shardedJedis.get(key);
     return accessToken;
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 7
0
 public String getAuthCodeByKey(String key) {
   ShardedJedis shardedJedis = shardedJedisPool.getResource();
   String authCode = "";
   try {
     authCode = shardedJedis.get(key);
     return authCode;
   } catch (Throwable e) {
     throw new RuntimeException(e);
   } finally {
     shardedJedis.close();
   }
 }
Ejemplo n.º 8
0
 @Override
 public Long hset(String key, String field, String value) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hset(key, field, value);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return null;
 }
Ejemplo n.º 9
0
 @Override
 public Long expire(String key, int seconds) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.expire(key, seconds);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return null;
 }
Ejemplo n.º 10
0
 @Override
 public List<String> hmget(String key, String... fields) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hmget(key, fields);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return new ArrayList<String>(0);
 }
Ejemplo n.º 11
0
 @Override
 public Boolean hexists(String key, String field) {
   ShardedJedis jedis = shardedJedisPool.getResource();
   try {
     return jedis.hexists(key, field);
   } catch (Exception e) {
     logger.error("Execute redis command failure", e);
   } finally {
     jedis.close();
   }
   return Boolean.FALSE;
 }