目前在做一个原型系统,其中涉及到权限管理部分,研究了一下
Spring Security,由于网上资料都是在配置文件里面定义url权限的,基本上没有存在数据库中的。在这个过程中我在网上找了很多资料,但是没有一个是完全能够解决问题的,acegi的例子springside倒是有一个。 而下面这段是一位网上朋友提供的,还不错,解析的清楚,大家可以参考
applicationContext-security.xml文件如下:
applicationContext-security.xml文件如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:security="http://www.springframework.org/schema/security"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
- <!--
- FilterChainProxy会按顺序来调用这些filter,使这些filter能享用SpringIoc的功能,
- CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定义url比较前先转为小写
- PATTERN_TYPE_APACHE_ANT定义使用Apacheant的匹配模式
- -->
- <beanid="springSecurityFilterChain"
- class="org.springframework.security.util.FilterChainProxy">
- <propertyname="filterInvocationDefinitionSource">
- <value><![CDATA[
- CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
- PATTERN_TYPE_APACHE_ANT
- /**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor
- ]]></value>
- </property>
- </bean>
- <!--
- 集成过滤器(HttpSessionContextIntegrationFilter是集成过滤器的一个实现)
- 每次request前HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完后
- 又把Authentication对象保存到Session中供下次request使用,此filter必须在其他Acegifilter前使用
- -->
- <beanid="httpSessionContextIntegrationFilter"
- class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>
- <!--
- 退出(Logout)过滤器退出登录操作
- -->
- <beanid="logoutFilter"
- class="org.springframework.security.ui.logout.LogoutFilter">
- <!--退出系统后系统跳转到此URL-->
- <constructor-argvalue="/login.action"/>
- <!--退出系统后的操作(调用logout方法)-->
- <constructor-arg>
- <list>
- <!--实现了LogoutHandler接口(logout方法)-->
- <refbean="rememberMeServices"/>
- <beanclass="org.springframework.security.ui.logout.SecurityContextLogoutHandler"/>
- </list>
- </constructor-arg>
- </bean>
- <!--
- 处理表单认证filter:
- 1.authenticationManager认证管理器
- 2.authenticationFailureUrl定义登录失败时转向的页面
- 3.defaultTargetUrl定义登录成功时转向的页面
- 4.filterProcessesUrl定义登录请求的地址
- 5.rememberMeServices在验证成功后添加cookie信息
- -->
- <beanid="authenticationProcessingFilter"
- class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"
- p:authenticationManager-ref="authenticationManager"
- p:authenticationFailureUrl="/login.action?login_error=1"
- p:defaultTargetUrl="/user.action"
- p:filterProcessesUrl="/j_spring_security_check"
- p:rememberMeServices-ref="rememberMeServices"/>
- <!--
- 认证管理器(org.springframework.security.AuthenticationManager接口)
- org.springframework.security.providers.ProviderManager是认证管理器的一个实现,
- ProviderManager通过遍历一个提供者的集合来实现身份验证,
- 直到某一个认证提供者能够成功地验证该用户的身份
- -->
- <!--
- 通过Providers提供认证者列表,如果一个认证提供者失败可以尝试另外一个认证提供者,以保证获取不同来源的身份认证,如
- DaoAuthenticationProvider从数据库中读取用户信息验证身份
- AnonymousAuthenticationProvider匿名用户身份认证
- RememberMeAuthenticationProvider已存cookie中的用户信息身份认证
- 其它的还有
- AuthByAdapterProvider使用容器的适配器验证身份
- CasAuthenticationProvider根据Yale中心认证服务验证身份,用于实现单点登陆
- JaasAuthenticationProvider从JASS登陆配置中获取用户信息验证身份
- RemoteAuthenticationProvider根据远程服务验证用户身份
- RunAsImplAuthenticationProvider对身份已被管理器替换的用户进行验证
- X509AuthenticationProvider从X509认证中获取用户信息验证身份
- TestingAuthenticationProvider单元测试时使用
- 每个认证者会对自己指定的证明信息进行认证,如DaoAuthenticationProvider仅对UsernamePasswordAuthenticationToken这个证明信息进行认证。
- -->
- <beanid="authenticationManager"
- class="org.springframework.security.providers.ProviderManager"
- p:sessionController-ref="concurrentSessionController">
- <propertyname="providers">
- <list>
- <refbean="daoAuthenticationProvider"/>
- <bean
- class="org.springframework.security.providers.anonymous.AnonymousAuthenticationProvider"
- p:key="springsecurity"/>
- <bean
- class="org.springframework.security.providers.rememberme.RememberMeAuthenticationProvider"
- p:key="springsecurity"/>
- </list>
- </property>
- </bean>
- <!--阻止用户在成功登录之后再进行一次成功登录-->
- <beanid="concurrentSessionController"
- class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl"
- p:maximumSessions="1"
- p:exceptionIfMaximumExceeded="true"
- p:sessionRegistry-ref="sessionRegistry"
- p:messageSource-ref="messageSource"/>
- <beanid="sessionRegistry"
- class="org.springframework.security.concurrent.SessionRegistryImpl"/>
- <beanid="messageSource"
- class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
- p:basename="/WEB-INF/classes/messages_zh_CN"/>
- <beanid="securityContextHolderAwareRequestFilter"
- class="org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter"/>
- <!--
- 利用cookie自动登陆filter
- 当SecurityContextHolder中不存在Authentication.用户授权信息,
- rememberMeProcessingFilter就会调用autoLogin()方法从cookie中获取用户信息,在验证filter之前使用
- -->
- <beanid="rememberMeProcessingFilter"
- class="org.springframework.security.ui.rememberme.RememberMeProcessingFilter"
- p:authenticationManager-ref="authenticationManager"
- p:rememberMeServices-ref="rememberMeServices"/>
- <!--
- 如果不存在任何授权信息时,自动添加匿名用户身份至SecurityContextHolder中
- -->
- <beanid="anonymousProcessingFilter"
- class="org.springframework.security.providers.anonymous.AnonymousProcessingFilter"
- p:key="springsecurity"
- p:userAttribute="anonymousUser,ROLE_ANONYMOUS"/>
- <!--
- 异常处理filter(异常转换过滤器),主要是处理AccessDeniedException和AuthenticationException,
- 将给每个异常找到合适的"去向"
- -->
- <beanid="exceptionTranslationFilter"
- class="org.springframework.security.ui.ExceptionTranslationFilter"
- p:accessDeniedHandler-ref="accessDeniedHandler"
- p:authenticationEntryPoint-ref="authenticationEntryPoint"/>
- <!--处理AccessDeniedException-->
- <beanid="accessDeniedHandler"
- class="org.springframework.security.ui.AccessDeniedHandlerImpl"
- p:errorPage="/accessDenied.jsp"/>
- <!---->
- <beanid="authenticationEntryPoint"
- class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"
- p:loginFormUrl="/login.action"
- p:forceHttps="false"/>
- <!--
- 使用过滤器安全拦截器保护资源
- filterSecurityInterceptor在执行转向目标url前检查objectDefinitionSource中设定的用户权限信息,
- 安全强制过滤器负责拦截请求,判断请求是否安全,并且给予认证和访问决策管理器一个机会来验证用户的身份和权限
- 过程:
- 首先,过滤器安全拦截器使用authenticationManager调用自己的provider来对用户的认证信息进行验证并获取用户已有的权限。
- 然后,使用访问决策管理器来判断用户是否拥用合适的授权来访问受保护的资源。
- (objectDefinitionSource属性定义了访问URL需要的权限信息)
- 最后,有投票者根据用户持有认证和访问url需要的属性,调用自己的voter来投票,决定是否允许访问。
- -->
- <beanid="filterSecurityInterceptor"
- class="org.springframework.security.intercept.web.FilterSecurityInterceptor"
- p:authenticationManager-ref="authenticationManager"
- p:accessDecisionManager-ref="accessDecisionManager"
- p:objectDefinitionSource-ref="objectDefinitionSource">
- </bean>
- <beanid="objectDefinitionSource"
- class="com.shopin.modules.security.intercept.web.DataBaseFilterInvocationDefinitionSource"
- p:convertUrlToLowercaseBeforeComprison="true"
- p:useAntPath="true"
- p:cacheManager-ref="securityCacheManager"/>
- <!--
- 访问决策管理器
- 验证用户是否有权限访问相应的资源(filterSecurityInterceptor中objectDefinitionSource属性定义的访问URL需要的属性信息)
- -->
- <beanid="accessDecisionManager"
- class="org.springframework.security.vote.AffirmativeBased"
- p:allowIfAllAbstainDecisions="false">
- <propertyname="decisionVoters">
- <list>
- <beanclass="org.springframework.security.vote.RoleVoter"/>
- <beanclass="org.springframework.security.vote.AuthenticatedVoter"/>
- </list>
- </property>
- </bean>
- <beanid="rememberMeServices"
- class="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices"
- p:key="springsecurity"
- p:userDetailsService-ref="userDetailsService"/>
- <beanid="daoAuthenticationProvider"
- class="org.springframework.security.providers.dao.DaoAuthenticationProvider"
- p:userCache-ref="userCache"
- p:passwordEncoder-ref="passwordEncoder"
- p:userDetailsService-ref="userDetailsService"/>
- <beanid="passwordEncoder"
- class="org.springframework.security.providers.encoding.Md5PasswordEncoder"/>
- <!--缓存配置-->
- <beanid="resourceCache"
- class="com.shopin.modules.security.resourcedetails.EhCacheResourceCache">
- <propertyname="cache">
- <beanclass="org.springframework.cache.ehcache.EhCacheFactoryBean"
- p:cacheManager-ref="cacheManager"
- p:cacheName="resourceCache"/>
- </property>
- </bean>
- <beanid="userCache"
- class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">
- <propertyname="cache">
- <beanclass="org.springframework.cache.ehcache.EhCacheFactoryBean"
- p:cacheManager-ref="cacheManager"
- p:cacheName="userCache"/>
- </property>
- </bean>
- <beanid="cacheManager"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:configLocation="classpath:ehcache-hibernate.xml">
- </bean>
- <beanid="userDetailsService"class="cn.shopin.miniweb.service.security.UserDetailServiceImpl"/>
- <beanid="securityCacheManager"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- p:transactionManager-ref="transactionManager"
- p:proxyTargetClass="true">
- <propertyname="target">
- <beanclass="com.shopin.modules.security.cache.SecurityCacheManagerImpl"
- p:sessionFactory-ref="sessionFactory"
- p:resourcCache-ref="resourceCache"/>
- </property>
- <propertyname="transactionAttributes">
- <props>
- <propkey="init*">PROPAGATION_REQUIRED,readOnly</prop>
- <propkey="get*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <beanid="loggerListener"
- class="org.springframework.security.event.authentication.LoggerListener"/>
- </beans>