All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shoulder.code.store.impl.RedisValidateCodeRepository Maven / Gradle / Ivy

The newest version!
package org.shoulder.code.store.impl;

import org.shoulder.code.dto.ValidateCodeDTO;
import org.shoulder.code.store.ValidateCodeStore;
import org.shoulder.code.exception.ValidateCodeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.concurrent.TimeUnit;

/**
 * 基于redis的验证码存取器,避免由于没有session导致无法存取验证码的问题
 * @author lym
 */
public class RedisValidateCodeRepository implements ValidateCodeStore {

	private static final String DEFAULT_KEY_PREFIX = "CAPTCHA_CODE:";

	private RedisTemplate redisTemplate;

	@SuppressWarnings("unchecked")
	public RedisValidateCodeRepository(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public void save(ServletWebRequest request, ValidateCodeDTO code, String type) {
		redisTemplate.opsForValue().set(buildKey(request, type), code, 30, TimeUnit.MINUTES);
	}


	@Override
	public ValidateCodeDTO get(ServletWebRequest request, String type) {
		Object value = redisTemplate.opsForValue().get(buildKey(request, type));
		if (value == null) {
			return null;
		}
		return (ValidateCodeDTO) value;
	}


	@Override
	public void remove(ServletWebRequest request, String type) {
		redisTemplate.delete(buildKey(request, type));
	}

	/**
	 * 返回在 redis 中存储验证码的 key
	 * @param request request response
	 * @param type 类型
	 * @return 在 redis 中存储验证码的 key
	 */
	protected String buildKey(ServletWebRequest request, String type) {
		String deviceId = request.getHeader("deviceId");
		if (StringUtils.isBlank(deviceId)) {
			throw new ValidateCodeException("please add the parameter deviceId in your requests!");
		}
		return DEFAULT_KEY_PREFIX + deviceId + ":" + type;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy