#
vincentlu
2025-02-13 e6df4123693fca526040115475701bcd3e373904
#
4个文件已修改
385 ■■■■ 已修改文件
rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java 337 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rsf-server/src/main/resources/application.yml 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java
@@ -45,7 +45,7 @@
    private RedisSupport(String name) {
        try {
            boolean bAll = true;
            String[] configKeys={"host","port","max","min","timeout","index"};
            String[] configKeys = {"host", "port", "max", "min", "timeout", "index"};
            for(String key : configKeys){
                if(!ConfigHelper.contains(name + "." + key)){
                    bAll = false;
@@ -53,19 +53,19 @@
            }
            if (bAll) {
                config = new JedisPoolConfig();
                config.setMaxIdle(ConfigHelper.getInteger(name+".min"));
                config.setMaxWaitMillis(ConfigHelper.getLong(name+".timeout"));
                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");
                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");
                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);
                pool = new JedisPool(config, host , port, timeout, password);
                Jedis jedis = this.getJedis();
                this.pool.returnResource(jedis);
                initialize = true;
@@ -92,7 +92,129 @@
        return null;
    }
    // key - value ----------------------------------------------------------------------------------------------------------
    // key - object ----------------------------------------------------------------------------------------------------------
    public String set(String flag, String key, Object value) {
        if(!this.initialize) {
            return null;
        }
        if(null == value) {
            this.delete(flag, key);
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            return jedis.set((flag + LINK + key).getBytes(), Serialize.serialize(value));
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public String set(String flag, String key, Object value, Integer seconds) {
        if(!this.initialize) {
            return null;
        }
        if (null == value) {
            this.delete(flag, key);
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            return jedis.setex((flag + LINK + key).getBytes(), seconds, Serialize.serialize(value));
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public <T> T get(String flag, String key) {
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            byte[] bytes = jedis.get((flag + LINK + key).getBytes());
            if(bytes == null || bytes.length == 0 ) {
                return null;
            }
            return (T) Serialize.unSerialize(bytes);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public Long delete(String flag, String key) {
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            return jedis.del((flag + LINK + key).getBytes());
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public Long clear(String flag) {
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        this.setValue(flag, "CLEARING", "true");
        try{
            Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,flag + LINK + "*");
            return Long.parseLong(String.valueOf(returnValue));
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    // 为已存在的key设置过期时间 - 秒
    public void setExpire(String flag, String key, int seconds){
        if(!this.initialize) {
            return;
        }
        Jedis jedis = this.getJedis();
        try{
            jedis.expire((flag + LINK + key).getBytes(), seconds);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
    }
    // 为已存在的key设置过期时间 - 具体到时间戳 (秒)
    public void setExpireAt(String flag, String key, Date toTime){
        if(!this.initialize) {
            return;
        }
        Jedis jedis = this.getJedis();
        try{
            jedis.expireAt((flag + LINK + key).getBytes(), toTime.getTime()/1000);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
    }
    // 获取过期剩余时间(秒) ttl == -1 没有设置过期时间; ttl == -2 key不存在
    public Long getExpire(String flag, String key) {
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            return jedis.ttl((flag + LINK + key).getBytes());
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    // key - string ----------------------------------------------------------------------------------------------------------
    public String setValue(String flag, String key, String value) {
        if(!this.initialize) {
@@ -107,7 +229,7 @@
        return null;
    }
    public String setValue(String flag, String key, String value,Integer seconds) {
    public String setValue(String flag, String key, String value, Integer seconds) {
        if(!this.initialize) {
            return null;
        }
@@ -180,7 +302,7 @@
        }
    }
    public void setValueExpireTime(String flag, String key,Date atTime){
    public void setValueExpireAt(String flag, String key,Date atTime){
        if(!this.initialize) {
            return;
        }
@@ -282,7 +404,7 @@
        }
    }
    public void setMapExpireTime(String name,Date atTime){
    public void setMapExpireAt(String name,Date atTime){
        if(!this.initialize) {
            return;
        }
@@ -358,7 +480,7 @@
        }
    }
    public void setListExpireTime(String name, Date atTime){
    public void setListExpireAt(String name, Date atTime){
        if(!this.initialize) {
            return;
        }
@@ -370,195 +492,36 @@
        }
    }
    public Object setObject(String flag,String key, Object value) {
        if(!this.initialize)return null;
        if(value==null){
            this.deleteObject(flag, key);
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            String result = jedis.set(("OBJ."+flag+"."+key).getBytes(), Serialize.serialize(value));
            return value;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return value;
    }
    public Object setObject(String flag,String key, Object value,Integer seconds) {
        if(!this.initialize)return null;
        if(value==null){
            this.deleteObject(flag, key);
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            String result = jedis.setex(("OBJ."+flag+"."+key).getBytes(),seconds,Serialize.serialize(value));
            return value;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return value;
    }
    public <T> T getObject(String flag,String key) {
        if(!this.initialize)return null;
        Jedis jedis = this.getJedis();
        try{
            byte[] bytes = jedis.get(("OBJ."+flag+"."+key).getBytes());
            if(bytes==null||bytes.length==0)
                return null;
            T returnValue = (T) Serialize.unSerialize(bytes);
            return (T) returnValue;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return null;
    }
    public <T> Long deleteObject(String flag, String key) {
        if(!this.initialize)return null;
        Jedis jedis = this.getJedis();
        try{
            Long returnValue = jedis.del(("OBJ."+flag+"."+key).getBytes());
            return returnValue;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return null;
    }
    public <T> Long clearObject(String flag) {
        if(!this.initialize)return null;
        this.setObject(flag, "CLEARING", "true");
        Jedis jedis = this.getJedis();
        try{
            //EVAL "return redis.call('del', unpack(redis.call('keys', 'OBJ.*')))" 0
            //EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 OBJ.*
            //Object returnValue = jedis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))",0,"OBJ."+flag+"."+"*");
            Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,"OBJ."+flag+"."+"*");
            return Long.parseLong(String.valueOf(returnValue));
        }catch(Exception ex){
            ////Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return null;
    }
    public void setObjectExpire(String flag, String key,int seconds){
        if(!this.initialize)return;
        Jedis jedis = this.getJedis();
        try{
            jedis.expire(("OBJ."+flag+"."+key).getBytes(), seconds);
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
    }
    public void setObjectExpireTime(String flag, String key,Date toTime){
        if(!this.initialize)return;
        Jedis jedis = this.getJedis();
        try{
            jedis.expireAt(("OBJ."+flag+"."+key).getBytes(), toTime.getTime()/1000);
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
    }
    public String[] getKeys(String pattern) {
        if(!this.initialize)return null;
        Jedis jedis = this.getJedis();
        try{
            return jedis.keys(pattern).toArray(new String[]{});
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
        }
        return null;
    }
    // count ----------------------------------------------------------------------------------------------------------
    public Long incr(String key) {
        if(!this.initialize)return null;
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            Long returnValue = jedis.incr("DI."+key);
            return returnValue;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
            return jedis.incr("COUNT." + key);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public Long decr(String key) {
        if(!this.initialize)return null;
        if(!this.initialize) {
            return null;
        }
        Jedis jedis = this.getJedis();
        try{
            Long returnValue = jedis.decr("DI."+key);
            return returnValue;
        }catch(Exception ex){
            //Utils.trace(ex.getMessage());
        }finally{
            if(jedis!=null)
                this.pool.returnResource(jedis);
            return jedis.decr("COUNT." + key);
        } catch (Exception e) {
            log.error(this.getClass().getSimpleName(), e);
        }
        return null;
    }
    public static void main(String...strings){
        Date start = new Date();
        RedisSupport r = RedisSupport.getRedis("lazy.redis.0");
        r.clearObject("aaa");
        RedisSupport redis = RedisSupport.defaultRedisSupport;
    }
}
rsf-server/src/main/java/com/vincent/rsf/server/common/config/ConfigProperties.java
@@ -60,6 +60,11 @@
     */
    private Integer codeLength = 4;
    /**
     * 验证码有效期 ( 秒 )
     */
    private Integer codeTime = 300;
    public List<String> getSuperUserList() {
        return Arrays.stream(superUsername.split(",")).collect(Collectors.toList());
    }
rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
@@ -1,10 +1,12 @@
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;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.annotation.OperationLog;
import com.vincent.rsf.server.common.config.ConfigProperties;
import com.vincent.rsf.server.common.security.JwtSubject;
@@ -31,6 +33,7 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@@ -41,6 +44,8 @@
 */
@RestController
public class AuthController extends BaseController {
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    @Resource
    private ConfigProperties configProperties;
@@ -82,13 +87,45 @@
        if (Cools.isEmpty(email)) {
            return R.parse(BaseRes.PARAM);
        }
        if (null != userService.getByEmail(email, null)) {
            return R.error("Email Already Exist");
        }
        if (redis.getExpire(EmailType.REGISTER_VERIFY.toString(), email) > (configProperties.getCodeTime() - 60)) {
            return R.error("Please don't request code too frequently.");
        }
        String code = Utils.randomNumbers(configProperties.getCodeLength());
        emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code));
        if (emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code))) {
            redis.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime());
        }
        return R.ok();
    }
    @OperationLog("Register")
    @PostMapping("/register")
    public R register(@RequestBody RegisterParam param, HttpServletRequest request) {
        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());
        if (Cools.isEmpty(cacheCode)) {
            return R.error("The verification code has expired.");
        }
        if (!cacheCode.equals(param.getCode())) {
            return R.error("The verification code is incorrect.");
        }
        User user = new User();
        user.setUsername(param.getUsername());
        user.setNickname(param.getUsername());
        user.setPassword(userService.encodePassword(param.getPassword()));
        user.setEmail(param.getEmail());
        user.setEmailVerified(1);
        user.setStatus(StatusType.ENABLE.val);
        user.setCreateTime(new Date());
        if (!userService.save(user)) {
            throw new CoolException("");
        }
        return R.ok();
    }
@@ -132,7 +169,6 @@
    @PostMapping("/auth/user")
    public R updateInfo(@RequestBody User user) {
        user.setId(getLoginUserId());
        // 不能修改的字段
        user.setUsername(null);
        user.setPassword(null);
        user.setEmailVerified(null);
rsf-server/src/main/resources/application.yml
@@ -33,4 +33,5 @@
  system-mode: OFFLINE
  token-key: KUHSMcYQ4lePt3r6bckz0P13cBJyoonYqInThvQlUnbsFCIcCcZZAbWZ6UNFztYNYPhGdy6eyb8WdIz8FU2Cz396TyTJk3NI2rtXMHBOehRb4WWJ4MdYVVg2oWPyqRQ2
  super-username: root
  code-length: 6
  code-length: 6
  code-time: 300