rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java
File was renamed from rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java @@ -1,13 +1,15 @@ package com.vincent.rsf.common.utils; package com.vincent.rsf.server.common.service; import com.vincent.rsf.common.utils.Serialize; import com.vincent.rsf.server.common.config.RedisProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Set; /** @@ -15,71 +17,38 @@ * Created by vincent on 2023-03-13 */ @Slf4j public class RedisSupport { private static final Map<String, RedisSupport> REDIS_CACHE = new HashMap<>(); public static final String DEFAULT_FLAG = "redis"; @Service public class RedisService { private static final String LINK = "."; public static RedisSupport defaultRedisSupport = RedisSupport.getRedis(DEFAULT_FLAG); protected JedisPool pool; protected JedisPoolConfig config; Integer index = 0; public Boolean initialize = false; public Boolean initialize = true; public synchronized static RedisSupport getRedis(String name){ RedisSupport redisSupport = REDIS_CACHE.get(name); if (null == redisSupport){ redisSupport = new RedisSupport(name); REDIS_CACHE.put(name, redisSupport); @Autowired private RedisProperties redisProperties; public JedisPool getPool() { if (null == this.pool) { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(false); this.index = redisProperties.getIndex(); this.pool = new JedisPool(config , redisProperties.getHost() , redisProperties.getPort() , redisProperties.getTimeout() , redisProperties.getPassword() ); } return redisSupport; } private RedisSupport(String name) { try { boolean bAll = true; String[] configKeys = {"host", "port", "max", "min", "timeout", "index"}; for(String key : configKeys){ if(!ConfigHelper.contains(name + "." + key)){ bAll = false; } } if (bAll) { config = new JedisPoolConfig(); config.setMaxIdle(ConfigHelper.getInteger(name + ".min")); config.setMaxWaitMillis(ConfigHelper.getLong(name + ".timeout")); // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; config.setTestOnBorrow(false); // 当前jedis连接的库号 index = ConfigHelper.getInteger(name + ".index"); // 实例化jedis池 String host = ConfigHelper.getString(name + ".host"); int port = ConfigHelper.getInteger(name + ".port"); int timeout = ConfigHelper.getInteger(name + ".timeout"); String password = ConfigHelper.getString(name + ".password"); pool = new JedisPool(config, host , port, timeout, password); Jedis jedis = this.getJedis(); this.pool.returnResource(jedis); initialize = true; } else { log.error("ERROR - 初始化Reids时出错,在配置文件中未找到{}.***属性(host,port,max,min,timeout,index)", name); } } catch (Exception e) { log.error(this.getClass().getSimpleName(), e); } return this.pool; } public Jedis getJedis(){ try{ Jedis jedis = pool.getResource(); Jedis jedis = this.getPool().getResource(); if(this.index != jedis.getDB().intValue()) { jedis.select(this.index); @@ -88,7 +57,6 @@ } catch (Exception e){ log.error(this.getClass().getSimpleName(), e); } return null; } @@ -518,10 +486,6 @@ log.error(this.getClass().getSimpleName(), e); } return null; } public static void main(String...strings){ RedisSupport redis = RedisSupport.defaultRedisSupport; } } rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
@@ -1,7 +1,6 @@ package com.vincent.rsf.server.system.controller; import com.vincent.rsf.common.enums.SystemModeType; import com.vincent.rsf.common.utils.RedisSupport; import com.vincent.rsf.common.utils.Utils; import com.vincent.rsf.framework.common.BaseRes; import com.vincent.rsf.framework.common.Cools; @@ -12,6 +11,7 @@ import com.vincent.rsf.server.common.security.JwtSubject; import com.vincent.rsf.server.common.service.EmailService; import com.vincent.rsf.server.common.utils.JwtUtil; import com.vincent.rsf.server.common.service.RedisService; import com.vincent.rsf.server.system.controller.param.LoginParam; import com.vincent.rsf.server.system.controller.param.RegisterParam; import com.vincent.rsf.server.system.controller.param.UpdatePasswordParam; @@ -45,8 +45,6 @@ @RestController public class AuthController extends BaseController { private final RedisSupport redis = RedisSupport.defaultRedisSupport; @Resource private ConfigProperties configProperties; @Resource @@ -59,6 +57,8 @@ private TenantService tenantService; @Autowired private EmailService emailService; @Autowired private RedisService redisService; @PostMapping("/login") public R login(@RequestBody LoginParam param, HttpServletRequest request) { @@ -90,12 +90,13 @@ if (null != userService.getByEmail(email, null)) { return R.error("Email Already Exist"); } if (redis.getExpire(EmailType.REGISTER_VERIFY.toString(), email) > (configProperties.getCodeTime() - 60)) { Long expire = redisService.getExpire(EmailType.REGISTER_VERIFY.toString(), email); if (expire > (configProperties.getCodeTime() - 60)) { return R.error("Please don't request code too frequently."); } String code = Utils.randomNumbers(configProperties.getCodeLength()); if (emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code))) { redis.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime()); redisService.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime()); } return R.ok(); } @@ -106,7 +107,7 @@ if (Cools.isEmpty(param.getUsername(), param.getPassword(), param.getEmail(), param.getCode())) { return R.parse(BaseRes.PARAM); } String cacheCode = redis.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); String cacheCode = redisService.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); if (Cools.isEmpty(cacheCode)) { return R.error("The verification code has expired."); } rsf-server/src/main/resources/application-dev.yml
@@ -73,7 +73,5 @@ host: 127.0.0.1 password: xltys1995 port: 6379 max: 30 min: 10 timeout: 5000 index: 11 index: 15 rsf-server/src/main/resources/application-prod.yml
@@ -43,12 +43,21 @@ maxRequestSize: 100MB jmx: enabled: false mail: from: whatsflow.team@gmail.com host: smtp.gmail.com port: 587 username: whatsflow.team@gmail.com password: elpc vfwk twnu uoyy properties: mail: smtp: auth: true starttls.enable: true redis: host: 127.0.0.1 password: xltys1995 port: 6379 max: 30 min: 10 timeout: 5000 index: 11