Spring Security 的Callstack分析

栏目: 后端 · 发布时间: 8年前

内容简介:Spring Security 的Callstack分析

在web.xml中有如下的filter声明

本文由博主javacoder.cn整理,转载请注明出处

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
 
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

在这里,DelegatingFilterProxy的filter-name是有严格要求的,不能乱定义,默认的为springSecurityFilterChain,其实就是在spring context中FilterChainProxy bean的别名。

在spring context中有如下定义

<global-method-security pre-post-annotations="enabled" />
 <http pattern="/static/**" security="none"/>
 <http pattern="/loggedout.jsp" security="none"/>
 <http >
 <intercept-url pattern="/secure/extreme/**" access="hasAuthority('supervisor')"/>
 <intercept-url pattern="/secure/**" access="authenticated" />
 <intercept-url pattern="/**" access="permitAll" />
 <form-login />
 <logout logout-success-url="/loggedout.jsp" delete-cookies="JSESSIONID"/>
 <csrf disabled="true"/>
 <anonymous enabled="false"/>
 <session-management invalid-session-url="/timeout.jsp">
 <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
 </session-management>
 </http>
 
 <authentication-manager> <!--对应ProviderManager实例-->
 <authentication-provider> <!--对应DaoAuthenticationProvider实例-->
 <user-service><!--对应InMemoryUserDetailsManager实例-->
 <user name="rod" password="koala" authorities="supervisor" />
 <user name="dianne" password="emu" authorities="ROLE_user,ROLE_teller" />
 <user name="scott" password="wombat" authorities="ROLE_user" />
 <user name="peter" password="opal" authorities="ROLE_user" />
 </user-service>
 </authentication-provider>
 </authentication-manager>

前面提到的FilterChainProxy是对所有<http>元素封装,当请求到来时,比较请求的路径是否匹配<http>的pattern。如果匹配,就执行该<http>定义的filter链。具体的实现参考FilterChainProxy.doFilterInternal方法。

核心filter UsernamePasswordAuthenticationFilter 的作用是完成用户的认证,用spring security的官方说法应该是"authenticate Credential"。主要的逻辑是调用ProviderManager.authenticate方法,在该方法中调用合适的AuthenticationProvider来完成真正的鉴权,本例为<authentication-provider>对应的DaoAuthenticationProvider。

核心filter

FilterSecurityInterceptor

的作用是判断当前用户是否拥有被请求资源的访问权限,在FilterSecurityInterceptor的父类 AbstractSecurityInterceptor.beforeInvocation方法中,先获取请求资源匹配的<intercept-url>元素对应的access定义,用Collection<ConfigAttribute>表示,然后调用AccessDecisionManager.decide进行是否有访问权限的决策,基于namespace配置时AccessDecisionManager的默认实现为AffirmativeBased。具体的逻辑参考AffirmativeBased.decide方法

调用栈如下:

Spring Security 的Callstack分析

Posted in:Spring Security


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

尽在双11:阿里巴巴技术演进与超越

尽在双11:阿里巴巴技术演进与超越

阿里巴巴集团双11技术团队 / 电子工业出版社 / 2017-4 / 79

“双 11”,诞生于杭州,成长于阿里,风行于互联网,成就于新经济,贡献于全世界。 从 2009 年淘宝商城起,双 11 已历经八年。每年的双 11 既是当年的结束,又是走向未来的起点。技术的突破创新,商业模式的更替交互,推动着双 11 迈步向前。 《尽在双11——阿里巴巴技术演进与超越》是迄今唯一由阿里巴巴集团官方出品、全面阐述双 11 八年以来在技术和商业上演进和创新历程的书籍。内容......一起来看看 《尽在双11:阿里巴巴技术演进与超越》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具