package cn.itcast.cc.spring.security;
import java.util.LinkedHashMap; import java.util.Map; import javax.annotation.Resource; import org.springframework.beans.factory.FactoryBean; import org.springframework.security.ConfigAttributeDefinition; import org.springframework.security.ConfigAttributeEditor; import org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource; import org.springframework.security.intercept.web.RequestKey; import org.springframework.security.util.AntUrlPathMatcher; import org.springframework.security.util.UrlMatcher; import org.springframework.stereotype.Component;
@Component("objectDefinitionSource") public class DefaultFilterInvocationDefinitionSourceImpl implements FactoryBean {
@Resource ResourceDetailsService resourceDetailsService;
private UrlMatcher getUrlMatcher() { UrlMatcher urlMatcher = new AntUrlPathMatcher(); return urlMatcher; }
@Override public Object getObject() throws Exception { UrlMatcher urlMatcher = this.getUrlMatcher(); // 获取数据Map Map<String, String> srcMap = resourceDetailsService.buildRequestMap(); LinkedHashMap<RequestKey, Object> requestMap = new LinkedHashMap<RequestKey, Object>(); ConfigAttributeEditor editor = new ConfigAttributeEditor(); // 转换数据Map for (Map.Entry<String, String> entry : srcMap.entrySet()) { String url = entry.getKey(); String roles = entry.getValue(); if (roles != null) { editor.setAsText(roles); requestMap.put(new RequestKey(url), editor.getValue()); } else { requestMap.put(new RequestKey(url), ConfigAttributeDefinition.NO_ATTRIBUTES); } } // 生成并返回对象 return new DefaultFilterInvocationDefinitionSource(urlMatcher, requestMap); }
@Override public Class getObjectType() { return null; }
@Override public boolean isSingleton() { return false; }
} |