1. 原理:使用众多的拦截器对url拦截,以此来管理权限。但是这么多拦截器,笔者不可能对其一一来讲,主要讲里面核心流程的两个。
首先,权限管理离不开登陆验证的,所以登陆验证拦截器AuthenticationProcessingFilter要讲;
还有就是对访问的资源管理吧,所以资源管理拦截器AbstractSecurityInterceptor要讲;
但拦截器里面的实现需要一些组件来实现,所以就有了AuthenticationManager、accessDecisionManager等组件来支撑。
现在先大概过一遍整个流程,用户登陆,会被AuthenticationProcessingFilter拦截,调用AuthenticationManager的实现,而且AuthenticationManager会调用ProviderManager来获取用户验证信息(不同的Provider调用的服务不同,因为这些信息可以是在数据库上,可以是在LDAP服务器上,可以是xml配置文件上等),如果验证通过后会将用户的权限信息封装一个User放到spring的全局缓存SecurityContextHolder中,以备后面访问资源时使用。
访问资源(即授权管理),访问url时,会通过AbstractSecurityInterceptor拦截器拦截,其中会调用FilterInvocationSecurityMetadataSource的方法来获取被拦截url所需的全部权限,在调用授权管理器AccessDecisionManager,这个授权管理器会通过spring的全局缓存SecurityContextHolder获取用户的权限信息,还会获取被拦截的url和被拦截url所需的全部权限,然后根据所配的策略(有:一票决定,一票否定,少数服从多数等),如果权限足够,则返回,权限不够则报错并调用权限不足页面。
直接上代码
1.springsecurity.xml
<!-- 用户的密码加密或解密 -->
<bean id="passwordEncoder" class="com.whggbd.bsz.secutiry.MyPasswordEncoder" />
<bean id="authenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService" />
<property name="hideUserNotFoundExceptions" value="false" />
<property name="passwordEncoder" ref="passwordEncoder" />
</bean>
<!-- 认证配置, 使用userDetailsService提供的用户信息 -->
<s:authentication-manager alias="authenticationManager">
<s:authentication-provider ref="authenticationProvider" />
</s:authentication-manager>
<!-- 访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源。 -->
<bean id="accessDecisionManager" class="com.whggbd.bsz.secutiry.MyAccessDecisionManager" />
<!-- 一个自定义的filter,必须包含authenticationManager, accessDecisionManager,securityMetadataSource三个属性。 -->
<bean id="myFilter" class="com.whggbd.bsz.secutiry.MyFilterSecurityInterceptor">
<!-- 资源数据源 -->
<property name="securityMetadataSource" ref="mySecurityMetadataSource" />
<!-- 认证管理器 -->
<property name="authenticationManager" ref="authenticationManager" />
<!-- 访问决策器 -->
<property name="accessDecisionManager" ref="accessDecisionManager" />
</bean>
<!-- 重写登陆验证 -->
<bean id="loginFilter"
class="com.whggbd.bsz.secutiry.MyUsernamePasswordAuthenticationFilter">
<!-- 处理登陆的Action -->
<property name="filterProcessesUrl" value="/j_spring_security_check"></property>
<!-- 验证成功后的跳转 -->
<property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"></property>
<!-- 验证失败后的处理 -->
<property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"></property>
<!-- <property name="sessionAuthenticationStrategy" ref="sessionAuthenticationStrategy" /> -->
<!-- 认证管理 -->
<property name="authenticationManager" ref="authenticationManager"></property>
</bean>
<!-- 登陆成功 -->
<bean id="loginLogAuthenticationSuccessHandler"
class="com.whggbd.bsz.secutiry.MySimpleUrlAuthenticationSuccessHandler">
<!--<property name="alwaysUseDefaultTargetUrl" value="true"/> <property
name="defaultTargetUrl" value="/views/desktop.jsp" /> <property name="targetUrlParameter"
value="redirectTo" /> -->
</bean>
<!-- 登陆失败 -->
<bean id="simpleUrlAuthenticationFailureHandler"
class="com.whggbd.bsz.secutiry.MySimpleUrlAuthenticationFailureHandler">
<!-- 可以配置相应的跳转方式。属性forwardToDestination为true采用forward false为sendRedirect -->
<!-- <property name="defaultFailureUrl" value="/failure"></property> -->
</bean>
<!-- Spring Security 认证切入点 -->
<bean id="loginUrlEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<constructor-arg name="loginFormUrl" value="/views/index.jsp" />
</bean>
<!-- 不要过滤图片等静态资源,其中**代表可以跨越目录,*不可以跨越目录。 -->
<s:http pattern="/**/*.ico" security="none" />
<s:http pattern="/**/*.svg" security="none" />
<s:http pattern="/**/*.jpg" security="none" />