package com.zysb.framework.security.intercept.web;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ConfigAttributeEditor;
import org.springframework.security.GrantedAuthority;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.PathMatcher;
import com.zysb.framework.security.cache.SecurityCacheManager;
import com.zysb.framework.security.resourcedetails.ResourcDetail;
/**
* @author yanwt
*
*/
public class DataBaseFilterInvocationDefinitionSource extends
AbstractFilterInvocationDefinitionSource implements InitializingBean {
private boolean convertUrlToLowercaseBeforeComprison = false;
private boolean useAntPath = false;
private PathMatcher pathMatcher = new AntPathMatcher();
private PatternMatcher matcher = new Perl5Matcher();
private SecurityCacheManager cacheManager;
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cacheManager, "com.zysb.framework.service.cache.SecurityCacheManager Object is required");
}
/* (non-Javadoc)
* @see com.zysb.framework.service.security.AbstractFilterInvocationDefinitionSource#lookupAttributes(java.lang.String)
*/
@Override
public ConfigAttributeDefinition lookupAttributes(String url) {
cacheManager.initResourceInCache();
if (isUseAntPath()) {
int firstQuestionMarkIndex = url.lastIndexOf("?");
if (firstQuestionMarkIndex != -1) {
url = url.substring(0, firstQuestionMarkIndex);
}
}
//��URL�ڱȽ�ǰ��ת��ΪСд
if (isConvertUrlToLowercaseBeforeComprison()) {
url = url.toLowerCase();
}
//��ȡ���е�URL
List<String> urls = cacheManager.getUrlResources();
//��������--��������������û�ʹ�������ĵ������߷���ҳ����ܳ�������
//���磺���ʱ��ܾ���û�ˢ��ҳ��
Collections.sort(urls);
Collections.reverse(urls);
GrantedAuthority[] authorities = new GrantedAuthority[0];
boolean isAuthResource = false;
//�������URL�����õ�URL��Դ����ƥ�䣬������ȷƥ���URL��Դ��Ӧ��Ȩ��
//ȡ��
for (String resourceName_url : urls) {
boolean matched = false;
//ʹ��antƥ��URL
if (isUseAntPath()) {
matched = pathMatcher.match(resourceName_url, url);
} else {//perl5����URL
Pattern compliedPattern = null;
Perl5Compiler compiler = new Perl5Compiler();
try {
compliedPattern = compiler.compile(resourceName_url, Perl5Compiler.READ_ONLY_MASK);
} catch (MalformedPatternException e) {
e.printStackTrace();
}
matched = matcher.matches(url, compliedPattern);
}
//ƥ����ȷ,��ȡ��ӦȨ��
if (matched) {
//��ȡ��ȷƥ��URL��Դ��Ӧ��Ȩ��
ResourcDetail detail = cacheManager.getResourcDetailFromCache(resourceName_url);
authorities = detail.getAuthorities();
isAuthResource = true;
break;
}
}
//��Ȩ��װ��ConfigAttributeDefinition���أ�ʹ��ConfigAttributeEditor��
if (authorities.length > 0) {
String authTemp = "";
for (GrantedAuthority grantedAuthority : authorities) {
authTemp += grantedAuthority.getAuthority() + ",";
}
String authority = authTemp.substring(0, (authTemp.length() - 1));
System.out.println(authority);
ConfigAttributeEditor attributeEditor = new ConfigAttributeEditor();
attributeEditor.setAsText(authority.trim());
return (ConfigAttributeDefinition)attributeEditor.getValue();
} else if(isAuthResource){
ConfigAttributeEditor attributeEditor = new ConfigAttributeEditor();
attributeEditor.setAsText("NEED");
return (ConfigAttributeDefinition)attributeEditor.getValue();
}
return null;
}
/* (non-Javadoc)
* @see com.shopin.modules.security.intercept.web.AbstractFilterInvocationDefinitionSource#getConfigAttributeDefinitions()
*/
@SuppressWarnings("unchecked")
@Override
public Collection getConfigAttributeDefinitions() {
return null;
}
/**
* @return the convertUrlToLowercaseBeforeComprison
*/
public boolean isConvertUrlToLowercaseBeforeComprison() {
return convertUrlToLowercaseBeforeComprison;
}
/**
* @param convertUrlToLowercaseBeforeComprison the convertUrlToLowercaseBeforeComprison to set
*/
public void setConvertUrlToLowercaseBeforeComprison(
boolean convertUrlToLowercaseBeforeComprison) {
this.convertUrlToLowercaseBeforeComprison = convertUrlToLowercaseBeforeComprison;
}
/**
* @return the useAntPath
*/
public boolean isUseAntPath() {
return useAntPath;
}
/**
* @param useAntPath the useAntPath to set
*/
public void setUseAntPath(boolean useAntPath) {
this.useAntPath = useAntPath;
}
/**
* @return the pathMatcher
*/
public PathMatcher getPathMatcher() {
return pathMatcher;
}
/**
* @param pathMatcher the pathMatcher to set
*/
public void setPathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher;
}
/**
* @return the matcher
*/
public PatternMatcher getMatcher() {
return matcher;
}
/**
* @param matcher the matcher to set
*/
public void setMatcher(PatternMatcher matcher) {
this.matcher = matcher;
}
/**
* @return the cacheManager
*/
public SecurityCacheManager getCacheManager() {
return cacheManager;
}
/**
* @param cacheManager the cacheManager to set
*/
public void setCacheManager(SecurityCacheManager cacheManager) {
this.cacheManager = cacheManager;
}
}