另外一个文件
package com.commons.business.acegi;import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.intercept.web.FilterInvocation;
import org.acegisecurity.intercept.web.FilterInvocationDefinitionSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;/**
 * 
 * 返回RdbmsEntryHolder的迭代
 * 
 */
public class RdbmsFilterInvocationDefinitionSource extends JdbcDaoSupport
implements FilterInvocationDefinitionSource { protected static final Log log = LogFactory
.getLog(RdbmsFilterInvocationDefinitionSource.class); private RdbmsSecuredUrlDefinition rdbmsInvocationDefinition; private PathMatcher pathMatcher = new AntPathMatcher(); private Ehcache resourceCache; public ConfigAttributeDefinition getAttributes(Object object) {
if ((object == null) || !this.supports(object.getClass())) {
throw new IllegalArgumentException("抱歉,目标对象不是FilterInvocation类型");
}
String url = ((FilterInvocation) object).getRequestUrl(); // 抽取出待请求的URL
if (log.isDebugEnabled()) {
log.debug("待请求的url:" + url);
}
List list = this.getRdbmsEntryHolderList();
if (list == null || list.size() == 0) {
return null;
} int firstQuestionMarkIndex = url.indexOf("?");
if (firstQuestionMarkIndex != -1) {
url = url.substring(0, firstQuestionMarkIndex);
if (log.isDebugEnabled()) {
log.debug(url);
}
} Iterator iter = list.iterator();
while (iter.hasNext()) {
RdbmsEntryHolder entryHolder = (RdbmsEntryHolder) iter.next();
String value = entryHolder.getUrl();
boolean matched = pathMatcher.match(value, url);
if (log.isDebugEnabled()) {
log.debug("匹配到如下URL:" + url + ";模式为" + entryHolder.getUrl()
+ ";是否被匹配:" + matched);
}
if (matched) {
return entryHolder.getCad();
}
}
return null;
} public Iterator getConfigAttributeDefinitions() {
Set<ConfigAttributeDefinition> set = new HashSet<ConfigAttributeDefinition>();
Iterator iter = this.getRdbmsEntryHolderList().iterator();
while (iter.hasNext()) {
RdbmsEntryHolder entryHolder = (RdbmsEntryHolder) iter.next();
set.add(entryHolder.getCad());
}
return set.iterator();
} public boolean supports(Class clazz) {
if (FilterInvocation.class.isAssignableFrom(clazz)) {
return true;
} else {
return false;
}
} protected void initDao() {
this.rdbmsInvocationDefinition = new RdbmsSecuredUrlDefinition(this
.getDataSource());
if (this.resourceCache == null)
throw new IllegalArgumentException(
"必须为RdbmsFilterInvocationDefinitionSource配置一EhCache缓存");
} private List getRdbmsEntryHolderList() {
List list = new ArrayList();
Element element = this.resourceCache.get("resource");
if (element != null) {
list = (List) element.getValue();
} else {
list = this.rdbmsInvocationDefinition.execute();
Element elem = new Element("resource", list);
this.resourceCache.put(elem);
}
return list;
} public void setResourceCache(Ehcache resourceCache) {
this.resourceCache = resourceCache;
}}package com.commons.business.acegi;import java.sql.ResultSet;
import java.sql.SQLException;import javax.sql.DataSource;import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.SecurityConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.util.StringUtils;/**
 * 
 * @author worldheart
 * 
 */
public class RdbmsSecuredUrlDefinition extends MappingSqlQuery { protected static final Log log = LogFactory
.getLog(RdbmsSecuredUrlDefinition.class); protected RdbmsSecuredUrlDefinition(DataSource ds) {
super(ds, "select resource_name,resource_string from oa_resource");
log.info("进入到RdbmsInvocationDefinition构建器中.........");
compile();
} protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
log.info("抽取resource中的记录.........");
RdbmsEntryHolder rsh = new RdbmsEntryHolder();
String value = rs.getString("resource_name");
if (log.isDebugEnabled()) {
log.debug("资源名:" + value);
}
rsh.setUrl(value.trim()); // 设置URL ConfigAttributeDefinition cad = new ConfigAttributeDefinition(); String value1 = rs.getString("resource_string");
if (log.isDebugEnabled()) {
log.debug(value1);
}
String[] tokens = StringUtils.commaDelimitedListToStringArray(value1
.trim());
for (int i = 0; i < tokens.length; ++i) {
cad.addConfigAttribute(new SecurityConfig(tokens[i]));
rsh.setCad(cad);
} // 设置角色集合 return rsh;
}
}