com.github.kaizen4j.shiro.captcha.RedisCaptchaStore Maven / Gradle / Ivy
package com.github.kaizen4j.shiro.captcha;
import java.time.Duration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
/**
* @author liuguowen
*/
@SuppressWarnings("unchecked")
public class RedisCaptchaStore implements CaptchaStore {
private RedisTemplate redisTemplate;
public RedisCaptchaStore(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public void save(String key, String captcha, Long timeMillis) {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set(key, captcha, Duration.ofMillis(timeMillis));
}
@Override
public boolean validate(String key, String expected) {
ValueOperations valueOperations = redisTemplate.opsForValue();
boolean isEquals = StringUtils.endsWithIgnoreCase(valueOperations.get(key), expected);
if (isEquals) {
redisTemplate.delete(key);
}
return isEquals;
}
}