package com.build.core.shiro.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.build.core.shiro.cache.ShiroCacheManager;

/**
 * ShiroHelper shiro加密 删除Session
 * @author suns suntion@yeah.net
 * @since 2017年3月3日下午2:20:37
 */
public class ShiroUtil {
    /** logger */
    private static Logger logger = LoggerFactory.getLogger(ShiroUtil.class);
    
    /**
     * 缓存
     */
    private static ShiroCacheManager cacheManager;
    
    /**随机数*/
    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();  
    
    /**
     * 随机一个盐值
     * @return 随机数
     */
    public static String randomSaltNumber() {
        return randomNumberGenerator.nextBytes().toHex();  
    }
    
    /**
     * 密码加密
     * @param password 加密前密码
     * @param salt 盐值
     * @return 加密后密码
     */
    public static String encryptPassword(String password, String salt) {
        String algorithmName = "SHA-256";
        int hashIterations = 2;
        String newPassword = new SimpleHash(algorithmName,password, ByteSource.Util.bytes(salt), hashIterations).toHex();  
        return newPassword;
    }
    
    /**
     * 获取当前请求对象
     * 
     * @return HttpServletRequest
     */
    public static String getRequestUrl() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getContextPath();
        } catch (Exception e) {
            return null;
        }
    }
    
    public static HttpServletRequest getRequest() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception e) {
            return null;
        }
    }
    
    
    /**
     * 删除缓存信息  最后调用 cache.remove(cacheName + ":" + username);  
     * @param cacheName 缓存名字
     * @param username 账户
     */
    public static void clearAuthenticationInfo(String cacheName, String username){
        if (StringUtils.isEmpty(cacheName) || StringUtils.isEmpty(username)) {
            throw new NullPointerException();
        }
        
        logger.debug("clear the " + username + "  " + cacheName);
        Cache<Object, Object> cache = cacheManager.getCache(cacheName);  
        cache.remove(cacheName + ":" + username);  
    }

    /**
     * @return the cacheManager
     */
    public static ShiroCacheManager getCacheManager() {
        return cacheManager;
    }

    /**
     * @param cacheManager the cacheManager to set
     */
    public static void setCacheManager(ShiroCacheManager cacheManager) {
        ShiroUtil.cacheManager = cacheManager;
    }

}
