⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ehcachebaseduserauthoritycache.java

📁 动态实现基于角色的权限管理Acegi+hibernate
💻 JAVA
字号:
package sample.auth.cache;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Element;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.util.Assert;

public class EhCacheBasedUserAuthorityCache implements AuthorityBasedUserCache,InitializingBean{

	private static final Log logger = LogFactory.getLog(EhCacheBasedUserAuthorityCache.class);
	
	private Cache cache;

    //~ Methods ================================================================

    public void setCache(Cache cache) {
        this.cache = cache;
    }

    public Cache getCache() {
        return cache;
    }
	
	public GrantedAuthority[] getAuthorityFromCache(String username) {
		 logger.info("get GrantedAuthority by username from cache");
		  Element element = null;

	        try {
	            element = cache.get(username);
	        } catch (CacheException cacheException) {
	            throw new DataRetrievalFailureException("Cache failure: "
	                + cacheException.getMessage());
	        }

	        if (logger.isDebugEnabled()) {
	            logger.debug("Cache hit: " + (element != null) + "; username: "
	                + username);
	        }

	        if (element == null) {
	            return null;
	        } else {
	            return (GrantedAuthority[]) element.getValue();
	        }
	}

	public void putAuthorityInCache(String username,GrantedAuthority[] grantedAuthority) {
		Element element = new Element(username,(Serializable)grantedAuthority);
		logger.info("put GrantedAuthority by username from cache  ");
        if (logger.isDebugEnabled()) {
            logger.debug("Cache put: " + element.getKey());
        }

        cache.put(element);
	}


	
	public void removeAuthorityFromCache(String username) {
		logger.info("remove GrantedAuthority by username from cache  ");
		cache.remove(username);	
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(cache, "cache mandatory");
	}



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -