@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(); } }
private void returnResource(ShardedJedis shardedJedis) { try { shardedJedis.close(); } catch (Exception e) { logger.error("returnResource error.", e); } }
public void delAuthCode(String key) { ShardedJedis shardedJedis = shardedJedisPool.getResource(); try { shardedJedis.del(key); } catch (Throwable e) { throw new RuntimeException(e); } finally { shardedJedis.close(); } }
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(); } }
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(); } }
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(); } }
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(); } }
@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; }
@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; }
@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); }
@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; }