content: /**
   * 
   */
  package org.zeus.common.validator;
  
  import org.springframework.social.connect.web.HttpSessionSessionStrategy;
  import org.springframework.social.connect.web.SessionStrategy;
  import org.springframework.stereotype.Component;
  import org.springframework.web.context.request.ServletWebRequest;
  
  /**
   * 基于session的验证码存取器
   * 如果后续需要把验证码放到redis里面，重新实现validateCodeRepository就ok了
   *
   * @author lizhizhong
   *
   */
  @Component
  public class SessionValidateCodeRepository implements ValidateCodeRepository {
  
  	/**
  	 * 验证码放入session时的前缀
  	 */
  	String SESSION_KEY_PREFIX = "SESSION_KEY_FOR_CODE_";
  	
  	/**
  	 * 操作session的工具类
  	 */
  	private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
  
      /**
       * 把验证码存储到session
       * @param request
       * @param code
       * @param validateCodeType
       */
  	@Override
  	public void save(ServletWebRequest request, ValidateCode code, ValidateCodeType validateCodeType) {
  		sessionStrategy.setAttribute(request, getSessionKey(request, validateCodeType), code);
  	}
  	
  	/**
  	 * 构建验证码放入session时的key
  	 * 
  	 * @param request
  	 * @return
  	 */
  	private String getSessionKey(ServletWebRequest request, ValidateCodeType validateCodeType) {
  		return SESSION_KEY_PREFIX + validateCodeType.toString().toUpperCase();
  	}
  
      /**
       * 获取验证码
       * @param request
       * @param validateCodeType
       * @return
       */
  	@Override
  	public ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType) {
  		return (ValidateCode) sessionStrategy.getAttribute(request, getSessionKey(request, validateCodeType));
  	}
  
      /**
       * 移除验证码
       * @param request
       * @param codeType
       */
  	@Override
  	public void remove(ServletWebRequest request, ValidateCodeType codeType) {
  		sessionStrategy.removeAttribute(request, getSessionKey(request, codeType));
  	}
  
  }
  