Spring Security 配置详解

简介:
    <?xml version="1.0" encoding="UTF-8"?>   
    <beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">   
      
        <!--   
                FilterChainProxy会按顺序来调用这些filter,使这些filter能享用Spring Ioc的功能,   
                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 定义url比较前先转为小写   
                PATTERN_TYPE_APACHE_ANT 定义使用Apache ant的匹配模式   
             -->   
        <bean id="springSecurityFilterChain"  
              class="org.springframework.security.util.FilterChainProxy">   
            <property name="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 必须在其他 Acegi filter前使用   
        -->   
        <bean id="httpSessionContextIntegrationFilter"  
              class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>   
        <!--   
            退出(Logout)过滤器 退出登录操作   
        -->   
        <bean id="logoutFilter"  
              class="org.springframework.security.ui.logout.LogoutFilter">   
            <!-- 退出系统后系统跳转到此URL -->   
            <constructor-arg value="/login.action"/>   
            <!-- 退出系统后的操作(调用logout方法) -->   
            <constructor-arg>   
                <list>   
                    <!-- 实现了LogoutHandler接口(logout方法) -->   
                    <ref bean="rememberMeServices"/>   
                    <bean class="org.springframework.security.ui.logout.SecurityContextLogoutHandler"/>   
                </list>   
            </constructor-arg>   
        </bean>   
        <!--   
            处理表单认证filter:   
            1.authenticationManager     认证管理器   
            2.authenticationFailureUrl  定义登录失败时转向的页面   
            3.defaultTargetUrl        定义登录成功时转向的页面   
            4.filterProcessesUrl        定义登录请求的地址   
            5.rememberMeServices        在验证成功后添加cookie信息   
        -->   
        <bean id="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 这个证明信息进行认证。   
        -->   
        <bean id="authenticationManager"  
              class="org.springframework.security.providers.ProviderManager"  
              p:sessionController-ref="concurrentSessionController">   
            <property name="providers">   
                <list>   
                    <ref bean="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>   
        <!-- 阻止用户在成功登录之后再进行一次成功登录 -->   
        <bean id="concurrentSessionController"  
              class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl"  
              p:maximumSessions="1"  
              p:exceptionIfMaximumExceeded="true"  
              p:sessionRegistry-ref="sessionRegistry"  
              p:messageSource-ref="messageSource"/>   
      
        <bean id="sessionRegistry"  
              class="org.springframework.security.concurrent.SessionRegistryImpl"/>   
      
        <bean id="messageSource"  
              class="org.springframework.context.support.ReloadableResourceBundleMessageSource"  
              p:basename="/WEB-INF/classes/messages_zh_CN"/>   
      
        <bean id="securityContextHolderAwareRequestFilter"  
              class="org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter"/>   
        <!--   
           利用cookie自动登陆filter   
            当 SecurityContextHolder 中不存在 Authentication 用户授权信息,   
            rememberMeProcessingFilter 就会调用 autoLogin() 方法从cookie中获取用户信息,在验证 filter 之前使用   
        -->   
        <bean id="rememberMeProcessingFilter"  
              class="org.springframework.security.ui.rememberme.RememberMeProcessingFilter"  
              p:authenticationManager-ref="authenticationManager"  
              p:rememberMeServices-ref="rememberMeServices"/>   
        <!--   
            如果不存在任何授权信息时,自动添加匿名用户身份至 SecurityContextHolder 中   
        -->   
        <bean id="anonymousProcessingFilter"  
              class="org.springframework.security.providers.anonymous.AnonymousProcessingFilter"  
              p:key="springsecurity"  
              p:userAttribute="anonymousUser,ROLE_ANONYMOUS"/>   
        <!--   
           异常处理filter(异常转换过滤器),主要是处理 AccessDeniedException 和 AuthenticationException,   
            将给每个异常找到合适的"去向"  
        -->   
        <bean id="exceptionTranslationFilter"  
              class="org.springframework.security.ui.ExceptionTranslationFilter"  
              p:accessDeniedHandler-ref="accessDeniedHandler"  
              p:authenticationEntryPoint-ref="authenticationEntryPoint"/>   
      
        <!-- 处理 AccessDeniedException -->   
        <bean id="accessDeniedHandler"  
              class="org.springframework.security.ui.AccessDeniedHandlerImpl"  
              p:errorPage="/accessDenied.jsp"/>   
        <!-- 处理 AuthenticationException -->   
        <bean id="authenticationEntryPoint"  
              class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"  
              p:loginFormUrl="/login.action"  
              p:forceHttps="false"/>   
      
        <!--   
             使用过滤器安全拦截器保护资源   
             filterSecurityInterceptor 在执行转向目标 url 前检查 objectDefinitionSource 中设定的用户权限信息,   
              安全强制过滤器负责拦截请求,判断请求是否安全,并且给予认证和访问决策管理器一个机会来验证用户的身份和权限   
              过程:   
                 首先,过滤器安全拦截器使用 authenticationManager 调用自己的 provider 来对用户的认证信息进行验证并获取用户已有的权限。   
                 然后,使用访问决策管理器来判断用户是否拥用合适的授权来访问受保护的资源。   
                  (objectDefinitionSource 属性定义了访问 URL 需要的权限信息)   
                 最后,有投票者根据用户持有认证和访问 url 需要的属性,调用自己的 voter 来投票,决定是否允许访问。   
        -->   
        <bean id="filterSecurityInterceptor"  
              class="org.springframework.security.intercept.web.FilterSecurityInterceptor"  
              p:authenticationManager-ref="authenticationManager"  
              p:accessDecisionManager-ref="accessDecisionManager"  
              p:objectDefinitionSource-ref="objectDefinitionSource">   
        </bean>   
        <bean id="objectDefinitionSource"  
              class="com.shopin.modules.security.intercept.web.DataBaseFilterInvocationDefinitionSource"  
              p:convertUrlToLowercaseBeforeComprison="true"  
              p:useAntPath="true"  
              p:cacheManager-ref="securityCacheManager"/>   
        <!--   
             访问决策管理器   
               验证用户是否有权限访问相应的资源(filterSecurityInterceptor 中 objectDefinitionSource 属性定义的访问URL需要的属性信息)   
        -->   
        <bean id="accessDecisionManager"  
              class="org.springframework.security.vote.AffirmativeBased"  
              p:allowIfAllAbstainDecisions="false">   
            <property name="decisionVoters">   
                <list>   
                    <bean class="org.springframework.security.vote.RoleVoter"/>   
                    <bean class="org.springframework.security.vote.AuthenticatedVoter"/>   
                </list>   
            </property>   
        </bean>   
      
        <bean id="rememberMeServices"  
              class="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices"  
              p:key="springsecurity"  
              p:userDetailsService-ref="userDetailsService"/>   
      
        <bean id="daoAuthenticationProvider"  
              class="org.springframework.security.providers.dao.DaoAuthenticationProvider"  
              p:userCache-ref="userCache"  
              p:passwordEncoder-ref="passwordEncoder"  
              p:userDetailsService-ref="userDetailsService"/>   
        <bean id="passwordEncoder"  
              class="org.springframework.security.providers.encoding.Md5PasswordEncoder"/>   
      
        <!-- 缓存配置 -->   
        <bean id="resourceCache"  
              class="com.shopin.modules.security.resourcedetails.EhCacheResourceCache">   
            <property name="cache">   
                <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean"  
                      p:cacheManager-ref="cacheManager"  
                      p:cacheName="resourceCache"/>   
            </property>   
        </bean>   
        <bean id="userCache"  
              class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">   
            <property name="cache">   
                <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean"  
                      p:cacheManager-ref="cacheManager"  
                      p:cacheName="userCache"/>   
            </property>   
        </bean>   
        <bean id="cacheManager"  
              class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"  
              p:configLocation="classpath:ehcache-hibernate.xml">   
        </bean>   
      
        <bean id="userDetailsService" class="cn.shopin.miniweb.service.security.UserDetailServiceImpl"/>   
      
        <bean id="securityCacheManager"  
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
              p:transactionManager-ref="transactionManager"  
              p:proxyTargetClass="true">   
            <property name="target">   
                <bean class="com.shopin.modules.security.cache.SecurityCacheManagerImpl"  
                      p:sessionFactory-ref="sessionFactory"  
                      p:resourcCache-ref="resourceCache"/>   
            </property>   
            <property name="transactionAttributes">   
                <props>   
                    <prop key="init*">PROPAGATION_REQUIRED,readOnly</prop>   
                    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>   
                </props>   
            </property>   
        </bean>   
      
        <bean id="loggerListener"  
              class="org.springframework.security.event.authentication.LoggerListener"/>   
      

    </beans> 



本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/583737,如需转载请自行联系原作者

相关文章
|
6天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
13 0
|
2月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
48 0
|
1月前
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
52 0
|
28天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
34 1
|
10天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
2天前
|
安全 Java 数据安全/隐私保护
使用Spring Security进行Java身份验证与授权
【4月更文挑战第16天】Spring Security是Java应用的安全框架,提供认证和授权解决方案。通过添加相关依赖到`pom.xml`,然后配置`SecurityConfig`,如设置用户认证信息和URL访问规则,可以实现应用的安全保护。认证流程包括请求拦截、身份验证、响应生成和访问控制。授权则涉及访问决策管理器,如基于角色的投票。Spring Security为开发者构建安全应用提供了全面且灵活的工具,涵盖OAuth2、CSRF保护等功能。
|
3天前
|
安全 前端开发 Java
Spring Security的授权管理器实现
Spring Security的授权管理器涉及用户登录后的token验证和权限检查。当用户携带token访问时,框架会验证token合法性及用户访问权限。自定义授权管理器`TokenAuthorizationManager`需实现`AuthorizationManager&lt;RequestAuthorizationContext&gt;`接口,处理校验逻辑,包括解析token、判断用户角色与访问资源的匹配。配置中在`SecurityConfig`注册该管理器以生效。测试表明,具有不同角色的用户可访问相应权限的资源,否则返回403错误。
112 4
|
3天前
|
安全 Java Spring
Spring Security的授权&鉴权
该文档介绍了授权和鉴权的概念,主要分为Web授权和方法授权。Web授权通过URL拦截进行,而方法授权利用注解控制权限,粒度更细但耦合度高。在Web授权的案例中,展示了如何在Spring Security中对特定URL设置角色控制。此外,还列举了Spring Security内置的控制操作方法,如permitAll()、denyAll()和hasRole()等,用于定义不同类型的用户访问权限。
104 7
|
6天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
13 1
|
6天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
15 0

热门文章

最新文章