Spring_Security源码分析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

SpringSecurity源码分析:1、ConfigAttributeDefinition和ConfigAttributeEditorConfigAttributeDefinition的对象中保存有了一组配置属性(ConfigAttributes),配置信息都以集合的方式存储在对象中。作用:信息的载体源码:importjava.util.Iterator;importjava.util.List;importorg.springframework.util.Assert;publicclassConfigAttributeDefinitionimplementsSerializable{publicstaticfinalConfigAttributeDefinitionNO_ATTRIBUTES=newConfigAttributeDefinition();privateListconfigAttributes;privateConfigAttributeDefinition(){this.configAttributes=Collections.EMPTY_LIST;}publicConfigAttributeDefinition(Stringattribute){this.configAttributes=newArrayList(1);this.configAttributes.add(newSecurityConfig(attribute));this.configAttributes=Collections.unmodifiableList(this.configAttributes);}publicConfigAttributeDefinition(ConfigAttributeattribute){this.configAttributes=newArrayList(1);this.configAttributes.add(attribute);this.configAttributes=Collections.unmodifiableList(this.configAttributes);}publicConfigAttributeDefinition(String[]attributeTokens){this.configAttributes=newArrayList(attributeTokens.length);for(inti=0;iattributeTokens.length;++i){this.configAttributes.add(newSecurityConfig(attributeTokens[i].trim()));}this.configAttributes=Collections.unmodifiableList(this.configAttributes);}publicConfigAttributeDefinition(ListconfigAttributes){Iteratorattributes=configAttributes.iterator();while(attributes.hasNext()){Assert.isInstanceOf(ConfigAttribute.class,attributes.next(),ListentriesmustbeoftypeConfigAttribute);}this.configAttributes=Collections.unmodifiableList(newArrayList(configAttributes));}publicstaticConfigAttributeDefinitioncreateFiltered(CollectionunfilteredInput){if(unfilteredInput==null){returnnull;}ListconfigAttributes=newArrayList();Iteratori=unfilteredInput.iterator();while(i.hasNext()){Objectelement=i.next();if(elementinstanceofConfigAttribute)configAttributes.add(element);}if(configAttributes.size()==0){returnnull;}returnnewConfigAttributeDefinition(configAttributes);}publicbooleancontains(ConfigAttributeconfigAttribute){returnthis.configAttributes.contains(configAttribute);}publicbooleanequals(Objectobj){if(!(objinstanceofConfigAttributeDefinition)){returnfalse;}ConfigAttributeDefinitiontest=(ConfigAttributeDefinition)obj;returnthis.configAttributes.equals(test.configAttributes);}publicCollectiongetConfigAttributes(){returnthis.configAttributes;}publicStringtoString(){returnthis.configAttributes.toString();}}作用:生成ConfigAttributeDefinition对象ConfigAttributeEditor类的源码packageorg.springframework.security;importjava.beans.PropertyEditorSupport;importorg.springframework.util.StringUtils;publicclassConfigAttributeEditorextendsPropertyEditorSupport{publicvoidsetAsText(Strings)throwsIllegalArgumentException{if(StringUtils.hasText(s))super.setValue(newConfigAttributeDefinition(StringUtils.commaDelimitedListToStringArray(s)));elsesuper.setValue(null);}}从源码我们可以看出ConfigAttributeEditor通过setAsText(Strings)的方法创建了一个ConfigAttributeDefinition对象,而ConfigAttributeDefinition对象中有一个List类型的configAttributes属性,configAttributes中保存的是ConfigAttribute类型的SecurityCofig对象,这样信息就保存在了ConfigAttributeDefinition对象中。从数据中查询的信息,实际上最终是以String[]方式存放的。2、DefaultFilterInvocationDefinitionSource、FilterInvocationDefinitonSource和ObjectDefinitionSourceDefaultFilterInvocationDefinitionSource的源码:packageorg.springframework.security.intercept.web;importjava.util.Arrays;importjava.util.Collection;importjava.util.Collections;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.Map.Entry;importjava.util.Set;importjavax.servlet.http.HttpServletRequest;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframework.security.ConfigAttributeDefinition;importorg.springframework.security.util.UrlMatcher;publicclassDefaultFilterInvocationDefinitionSourceimplementsFilterInvocationDefinitionSource{privatestaticfinalSetHTTP_METHODS=newHashSet(Arrays.asList(newString[]{DELETE,GET,HEAD,OPTIONS,POST,PUT,TRACE}));protectedfinalLoglogger;privateMaprequestMap;privateMaphttpMethodMap;privateUrlMatcherurlMatcher;privatebooleanstripQueryStringFromUrls;DefaultFilterInvocationDefinitionSource(UrlMatcherurlMatcher){this.logger=LogFactory.getLog(super.getClass());this.requestMap=newLinkedHashMap();this.httpMethodMap=newHashMap();this.urlMatcher=urlMatcher;}publicDefaultFilterInvocationDefinitionSource(UrlMatcherurlMatcher,LinkedHashMaprequestMap){this.logger=LogFactory.getLog(super.getClass());this.requestMap=newLinkedHashMap();this.httpMethodMap=newHashMap();this.urlMatcher=urlMatcher;Iteratoriterator=requestMap.entrySet().iterator();while(iterator.hasNext()){Map.Entryentry=(Map.Entry)iterator.next();RequestKeyreqKey=(RequestKey)entry.getKey();addSecureUrl(reqKey.getUrl(),reqKey.getMethod(),(ConfigAttributeDefinition)entry.getValue());}}voidaddSecureUrl(Stringpattern,ConfigAttributeDefinitionattr){addSecureUrl(pattern,null,attr);}voidaddSecureUrl(Stringpattern,Stringmethod,ConfigAttributeDefinitionattr){MapmapToUse=getRequestMap

1 / 74
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功